Vim
Process inspection:
ps -ef | grep PID
Delete text:
- Ctrl+U: Remove all characters before cursor
- Ctrl+K: Remove all characters after cursor
Batch replacement in Vim:
:%s/old/new/g
Example:
:%s/docker\.io/registry.cn-hangzhou.aliyuncs.com\/acs-sample/g
Paste formatted code:
:set paste
Show line numbers:
:set nu
Delete all content:
:%d
System
Check Linux distribution:
cat /etc/*release
uname -a
The presence of "el7" indicates CentOS (Enterprise Linux 7).
Change hostname:
vim /etc/hostname
vim /etc/hosts
Reboot if changes don't take effect.
Create command alias:
echo $SHELL # Check if using bash or zsh
vim ~/.bashrc # or ~/.zshrc
Add the alias definition:
alias kb='kubectl'
Apply changes:
source ~/.bashrc # or source ~/.zshrc
Restart the terminal (not the server).
Security
Configure passwrodless SSH:
ssh-keygen -t rsa -b 4096 # Generate key pair
cd ~/.ssh
Copy the contents of id_rsa.pub (public key) to the target machine.
On the target machine:
vim ~/.ssh/authorized_keys
Paste the public key.
Monitor file in real-time:
watch -n 1 cat filename
Use exclamation mark without bash history error:
set +H
echo "<h1>Welcome!</h1>" > index.html
set -H
Network
Check listening ports:
netstat -ntpl
Find process by name:
ps -ef | grep [process_name]
Run process in background:
nohup ./myapp > output.log 2>&1 &
nohup java -jar application.jar > output.log 2>&1 &
Configure firewall (RHEL/CentOS/Fedora):
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --list-ports
firewall-cmd --reload
firewall-cmd --zone=public --query-port=8081/tcp
Storage
Upload files via SSH:
rz
Download files via SSH:
sz filename
Transfer files through bastion host:
scp /local/path/file user@bastion:/remote/path/
Download file from remote server:
scp user@server_ip:/tmp/heapdump.hprof /local/path/
Note: Ensure proper permissions when accessing files outside your home directory.
Move files:
mv /path/source/file.txt /path/destination/file.txt
Batch move files:
# Move all .yaml files from source to destination
find /root/user/ -maxdepth 1 -name "*.yaml" -exec mv {} /root/user/k8s \;
Check disk usage:
df -h # Total disk space
du -h filename # Detailed breakdown
du -sh filename # Single file size
Count files in directory:
ls -l | wc -l
Memory
Batch terminate processes:
kill $(ps aux | grep 'process_name' | grep -v grep | awk '{print $2}')
Check memory status:
free -h
Shell
Make script eexcutable:
chmod +x script.sh
Permission inheritance: When running a script with elevated privileges, all child processes inherit those permissions.
Example: A cron job running via sudo crontab -e executes scripts with root privileges.
Example: Running sudo ./script1.sh grants sudo permissions to all nested script calls.
Utilities
Find files modified before a specific time:
find /path -type f ! -newermt "2025-02-25 17:00" -ls | grep filename
Force kill process by name:
sudo kill -9 $(ps -ef | grep process_name | grep -v grep | awk '{print $2}')
Invert match:
grep -v pattern
Field extraction with awk:
awk '{print $0}' # Full line
awk '{print $1}' # First field
awk '{print $2}' # Second field
Build commands with xargs:
command | xargs target_command
xargs passes output from the previous command as arguments to the next, handling empty values gracefully.