Linux
open-source operation system
Why Linux?
Ansible do not run on Windows
Kubernete do not plan to run on Windows
Distro
KALI LINUX ™, Ubuntu, and Parrot all come from Debian.
CentOS comes from Red Hat.
Shell
The shell is the command-line interpreter.
Type of shell:
Bourne-Again Shell (bash)
C Shell (csh)
Korn Shell (ksh)
Enhanced C shell (tcsh)
Z Shell (zsh)
ksh and bash use the dollar sign ($) to indicate where users type in their commands. Other shells, such as zsh, use the percent sign (%) for this purpose.
// or to check which type of shell, using
echo $SHELL
CLI
Basic commands
the ./
# indicate the command should be executed in the current directory
# instead of searching for it in system's path
./gcloud --version
mkdir
# -p: make entire directory hierarchy
mkdir -p /tmp/subdir/work/202304
rm
# -r: recursively
# remove all folder content & sub-folder
rm -r /tmp/subdir
mv
# default: -f
# move but do not overwrite
mv -n
cat
# add content to file
cat > contents_file.txt
This is some content text
# number the output lines
cat -n [filename]
less
# show contain with line number
less -B [filename]
# when in less, using /word to find [word]
/word
grep
# grep 'keyword' [target_file]
grep 'temporary password' /var/log/mysqld.log
# with pipe |
# list all .php file
ls -la | grep ".php"
apropos
# open manual of a command
man tail
# one-line description of the command
whatis nano
# search the manual page for specific description
# -a, --and option to search for multiple keywords.
apropos [keyword]
apropos -a change password
history
# to view all the history commands
history
Vi editor
Command mode & Insert mode. When open a VI editor, the default mode is COMMAND MODE.
open VI editor
vi index.html
switch to INSERT mode
using i key to enter insert mode.
using esc key to return to command mode.
navigate in COMMAND mode
there are 2 ways
using arrow keys
using K (up) -J (down) -H (left) -L (right)
find stuff
# for example, you want to find "of"
/of
copy, paste, delete
save, quit
do not save, quit
Esc > :q!
User accounts
# check which user
whoami
# check id
id
# using system user
# if no username was specified, it will switch to root user
su [username]
Password: *****
# using ssh
ssh [username]@192.168.1.2
# using root permission
# ex: list content of /root folder, but the current user do not have the permission
sudo ls /root
Download files
# download some-file.txt
curl http://www.example.com/some-file.txt -O
# another way is using wget
# -O: specify the name of the local file you want to store
wget http://www.example.com/some-file.txt -O some-file.txt
Tips
check OS version
# using wild-card to check OS version
ls /etc/*release*
$ ls /etc/*release*
/etc/lsb-release /etc/os-release
check details of OS version
cat /etc/*release*
$ cat /etc/*release*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
SSH
Connecting
# connect to a server with default port 22
ssh root@192.168.1.5
# connect with specific port
ssh root@192.168.1.5 -p 6222
# connect via pem file (400 permission)
ssh -i /path/file.pem root@192.168.1.5
ping
command to send network ICMP (Internet Control Message Protocol) echo requests to a specific IP address.
# -c option: specify the number of echo request send
ping -c 3 10.154.0.2
Package manager
A package manager is a tool that helps users install, manage, and remove packages or applications. Linux uses multiple package managers.
.rpm
RPM (Redhat Package Manager) - Redhat
# install package
rpm -i telnet.rpm
# query the details of the package
rpm -q telnet.rpm
# get exact package name
rpm -qa | grep ftp
# uninstall package
rpm -e telnet.rpm
YUM (Yellowdog Updater Modified) - CentOS
# install package
yum install ansible
# install package-version
yum install ansible-2.8.11-1.el7
# uninstall package
yum remove ansible
# list all YUM repos
yum repolist
# show all duplicated packages
yum --showduplicates list ansible
# show configuration of the repos
ls /etc/yum.repos.d/
# search for particular package
yum list ansible
.deb
APT (Advanced Package Tool) - Ubuntu/Debian
APT is a tool used with Debian-derived distributions.
# install app: suricata
sudo apt install suricata
# remove app
sudo apt remove suricata
# list all installed app
apt list --installed
Services
# start a service, e.g: httpd
service httpd start
# or
systemctl start httpd
systemctl stop httpd
# check status
# which file the service configured
systemclt status httpd
thor@host01 ~$ systemctl status app
● app.service - My python web application
Loaded: loaded (/usr/lib/systemd/system/app.service; disabled; vendor preset: disabled)
Active: inactive (dead)
# enable, disable after configuration
# when enable, the service start automatically when system boots up
systemctl enable httpd
systemctl disable httpd
[app].service
thor@host01 ~$ cat /usr/lib/systemd/system/app.service
[Unit]
Description=My python web application
[Service]
ExecStart=/usr/bin/python3 /opt/code/my_app.py
ExecStartPre=/bin/bash /opt/code/configure_db.sh
ExecStartPost=/bin/bash /opt/code/email_status.sh
Restart=always
[Install]
WantedBy=multi-user.target
Last updated