常用命令
可能需要用到的 Linux 命令(来源:linux_cheat_sheet)
版本
# Debian/Ubuntu
uname -a
lsb_release -a
# CentOS/RedHat
cat /etc/centos-release
cat /etc/redhat-release
# This will provide a lot more information
cat /etc/os-release
系统
# 重启服务器
reboot
# 查看内存使用
free
# 查看硬盘使用
df -hl
# 查看 Docker 容器
sudo docker ps -a
# 查看端口
netstat -tunlp
# 查看所有环境变量
env
文件管理
# create a new directory and all subdirectories
mkdir -p dir/dir2/dir3
# Send a command's output to file.txt, no STDOUT
ls > file.txt
# Send a command's output to file.txt AND see it in STDOUT
ls | tee /tmp/file.txt
# Search and Replace within a file
sed -i 's/original-text/new-text/g' <filename>
# 下载url对应的文件
wget url
# 解压xx.zip文件到当前目录
unzip xx.zip
# 进入指定目录
cd /data/wwwroot
# 修改wwwroot文件夹所属的用户和用户组为nginx
chown -R nginx.nginx /data/wwwroot
# 分别修改文件和文件夹的读、写、执行权限
chmod -R 0750 /data/apps
find /data/wwwroot/default -type f -exec chmod 640 {} \;
find /data/wwwroot/default -type d -exec chmod 750 {} \;
# 修改文件权限
chmod u+x <file>
# Add groupnamehere to an exist directory /base/path/
sudo setfacl -Rm g:groupnamehere:rwx /base/path/
# Add groupnamehere to an exist directory /base/path/
sudo setfacl -Rm g:groupnamehere:rwx /base/path/
# Add groupnamehere to an new directory /base/path/
sudo setfacl -Rdm g:groupnamehere:rwx /base/path/
搜索
# search for a file in a filesystem
find . -name 'filename.rb' -print
# locate a file
locate <filename>
# see command history
history
# search CLI history
<ctrl>-R
# -B/A = show 2 lines before/after search_term
grep -B 2 -A 2 search_term <filename>
# -<number> shows both before and after
grep -2 search_term <filename>
# Search on all files in directory (recursively)
grep -r search_term <directory>
# search through *.gz files is the same except with zgrep
zgrep search_term <filename>
# Fast grep printing lines containing a string pattern
fgrep -R string_pattern <filename or directory>
# View command history
history
# Run last command that started with 'his' (3 letters min)
!his
# Search through command history
<ctrl>-R
# Execute last command with sudo
sudo !!
运维命令
# 重启Nginx服务,重启php-fpm
systemctl restart nginx
systemctl restart php-fpm
# 重启Apache服务
systemctl restart apache
# 系统升级
yum update -y //升级所有包同时也升级软件和系统内核,-y当安装过程提示选择全部为"yes"
yum upgrade -y //只升级所有包,不升级软件和系统内核,-y当安装过程提示选择全部为"yes"
# 重启所有容器
docker restart $(docker ps -a | awk '{ print $1}' | tail -n +2)
# 显示 tcp,udp 的端口和进程等相关情况。
netstat -tunlp
netstat -tunlp | grep 端口号
# 查看服务器 22 端口的占用情况
lsof -i:22
# kill 端口对应的进程
kill -9 PID
# Print last lines in log file where 'n'
# is the number of lines to print
tail -n /path/to/log/file