Linux 服务器常用命令速查表

一、系统信息

uname -a              # 内核版本、系统架构
hostname              # 主机名
uptime                # 运行时间、负载
whoami                # 当前用户
id                    # 当前用户 UID/GID
lsb_release -a        # 发行版信息
cat /etc/os-release   # 系统版本(通用)
date                  # 当前时间

二、文件与目录

ls -lah               # 详细列表,含隐藏文件和大小
cd -                  # 回到上次目录
pwd                   # 当前路径
mkdir -p a/b/c        # 递归创建目录
cp -r src dst         # 递归复制
mv old new            # 移动/重命名
rm -rf dir            # 递归删除(谨慎)
ln -s target link     # 软链接
touch file            # 新建空文件
tree -L 2             # 树形展示目录(需安装)

三、查看文件内容

cat file              # 全部输出
less file             # 分页查看(/搜索,q退出)
head -n 20 file       # 前 20 行
tail -n 50 file       # 后 50 行
tail -f access.log    # 实时跟踪日志
tail -F file          # 文件轮转也跟踪
grep "error" log      # 搜索关键词
grep -rn "TODO" .     # 递归搜索当前目录
grep -v "debug" log   # 反向过滤(排除)

四、查找

find / -name "nginx.conf" 2>/dev/null   # 按文件名查找
find . -type f -mtime -7                # 7天内修改的文件
find . -size +100M                      # 大于 100M 的文件
which python3                           # 命令路径
whereis nginx                           # 命令及相关文件

五、进程管理

ps aux                          # 查看所有进程
ps -ef | grep nginx             # 过滤特定进程
top                             # 实时进程监控
htop                            # 增强版 top(需安装)
kill -9 12345                   # 强制杀进程
pkill -f "python app.py"        # 按名称杀进程
jobs                            # 当前 shell 后台任务
nohup ./app &                   # 后台运行,挂断不退出

六、网络

ip a                        # 查看 IP 地址
ip route                    # 路由表
ss -tlnp                    # 监听端口及进程
netstat -tlnp               # 同上(旧版)
curl -I http://localhost    # 查看响应头
curl -v url                 # 详细请求过程
wget url                    # 下载
ping baidu.com              # 连通性测试
telnet host 80              # 端口连通性
ssh user@host               # 远程登录
scp file user@host:/path    # 远程复制

七、磁盘与内存

df -h                       # 磁盘使用情况
du -sh dir                  # 目录占用大小
du -sh * | sort -h          # 排序查看当前目录
free -h                     # 内存使用
fdisk -l                    # 磁盘分区
mount                       # 挂载情况

八、权限

chmod 755 script.sh         # 修改权限
chmod +x script.sh         # 加执行权限
chown user:group file       # 修改属主
sudo command                # 以 root 执行
visudo                      # 编辑 sudoers(用这个,不要直接 vi)

九、压缩解压

tar -zcvf out.tar.gz dir/   # 打包压缩
tar -zxvf out.tar.gz        # 解压
zip -r out.zip dir/         # zip 压缩
unzip file.zip              # zip 解压
gzip file                   # gzip 压缩
gunzip file.gz              # gzip 解压

十、文本处理

echo "hello" > file         # 覆盖写入
echo "world" >> file        # 追加写入
sort file                   # 排序
uniq file                   # 去重(需先 sort)
wc -l file                  # 统计行数
awk '{print $1}' file       # 取第一列
cut -d: -f1 /etc/passwd     # 按分隔符取列
sed -i 's/old/new/g' file   # 替换并写回

十一、系统服务(systemd)

systemctl status nginx     # 查看服务状态
systemctl start nginx      # 启动
systemctl stop nginx       # 停止
systemctl restart nginx    # 重启
systemctl enable nginx     # 开机自启
systemctl disable nginx    # 取消自启
journalctl -u nginx        # 查看服务日志
journalctl -f              # 实时跟踪日志
journalctl -xe             # 最近错误

十二、用户与组

useradd -m alice           # 新建用户(含家目录)
passwd alice               # 设置密码
userdel -r alice           # 删除用户及家目录
groupadd dev               # 新建组
usermod -aG dev alice      # 将用户加入组
su - alice                 # 切换用户
exit                       # 退出当前用户

十三、其他常用

history                    # 命令历史
!100                       # 执行第 100 条历史命令
alias ll='ls -lah'         # 别名
man command                # 查看手册
command --help             # 简要帮助
clear                      # 清屏

速记

场景 首选命令
服务挂了 systemctl status + journalctl -xe
磁盘满了 df -h → du -sh *
内存爆了 free -h + ps aux --sort=-%mem
端口被占 ss -tlnp
日志报错 tail -f + grep -i error
找不到文件 find / -name "xxx" 2>/dev/null
远程传文件 scp