Kiryu

Kiryu

做一个善于思考的普通人

Some common commands

1. User and Permissions:#

  1. View current user
whoami
  1. 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
  1. Add a user and generate a directory in the home directory
useradd -m username
  1. Change the user of a file, quite commonly used
chown -R username:groupname filename
  1. Grant execute permission to a file, very commonly used
chmod +x filename
  1. Change hostname
hostnamectl set-hostname hostname
  1. 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
  1. View network-related information
ifconfig or ip a
  1. Check if a port is open
telnet host port
# telnet 172.24.1.1 8080
  1. Display port usage
netstat -lntp
ss -lntp
  1. 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
  1. 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
  1. Kill the process by process name
pkill process_name
  1. 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
  1. View the first few lines of a file, generally not used much
head -n 100 filename
  1. 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
  1. 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
  1. View disk partition information
lsblk -f
  1. List disks
fdisk -l
  1. Format disk as ext4 format
mkfs -t ext4 diskname
  1. Mount and unmount
mount diskname directory
# mount /dev/sdb /opt

umount directory
  1. View file systems
df -h
  1. 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
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.