1. User and Permissions:#
- View current user
whoami
- Switch user, adding '-' is equivalent to logging in again with the username to be switched, and the environment variables and working directory will change. Switching users is generally used for services that need to be started by specific users, such as Elasticsearch.
su username
su - username
- Add a user and generate a directory in the home directory
useradd -m username
- Change the user of a file, quite commonly used
chown -R username:groupname filename
- Grant execute permission to a file, very commonly used
chmod +x filename
2. Network Related#
- Change hostname
hostnamectl set-hostname hostname
- Check network connectivity, such as checking whether you can access the Internet, whether servers can communicate with each other, etc.
ping ip address or domain name
#ping baidu.com
- View network-related information
ifconfig or ip a
- Check if a port is open
telnet host port
# telnet 172.24.1.1 8080
- Display port usage
netstat -lntp
ss -lntp
- ssh and scp, often used to transfer files between servers, faster
ssh user@host
# ssh [email protected]
scp file user@host:path
# scp 1.txt [email protected]:/opt
3. Process Related#
- It's just the ps command
ps -ef
# View process information
ps -ef|grep process_name
# Find process
ps -ef|grep -v grep |grep process_name
# Only display the process name to be searched, do not display the grep process
ps -ef|grep -v grep|grep process_name|awk '{print $2}'
# Print the process ID of the process to be searched
ps -ef|grep -v grep|grep process_name|awk '{print $2}'|xargs kill -9
# Kill the process to be searched
- Kill the process by process name
pkill process_name
4. Text Related#
- We don't need to mention using cat to view files, you can also append content to a file
cat <<EOF>>1.txt
Hello
World
EOF
# Replace >> with > to overwrite the content of the file
- View the first few lines of a file, generally not used much
head -n 100 filename
- View the last few lines of a file, very commonly used, for dynamically viewing logs
tail -n 100 -f xxx.log
# View the last 100 lines of the file and refresh in real time
- Then there are the three musketeers, here are just some simple and commonly used usage examples
sed -n "/keyword/p" filename
# Output lines containing the keyword, not commonly used
sed -i "s/keyword1/keyword2/g" filename
# Replace keyword1 with keyword2 in the file, commonly used, for example, changing IP addresses
grep -n 'keyword' filename
# Same as sed, output lines containing the keyword
grep -rl keyword ./
# Find files in the current directory that contain the keyword, commonly used, for example, finding configuration files containing a certain IP address
awk is more complex, the one I often use is to split text, for example
awk -F 'delimiter' '{print $1}' filename
# Split the text according to the delimiter and output the first column
5. Disk Related#
- View disk partition information
lsblk -f
- List disks
fdisk -l
- Format disk as ext4 format
mkfs -t ext4 diskname
- Mount and unmount
mount diskname directory
# mount /dev/sdb /opt
umount directory
- View file systems
df -h
- View the space occupied by the current directory
du -sh
du -h -d 1
# I prefer this command, it can easily find large files in directories