旧墨

Linux常用操作命令

2025/12/26
40
0

1️⃣ 文件与目录操作

# 查看目录大小(一级目录)
du -h --max-depth=1 .

# 查看配置文件,去掉注释和空行
cat xxxx.conf | grep -v '#' | grep -v '^$'

# 解压 tar.gz 文件
tar zxvf FileName.tar.gz

# 打包目录为 tar.gz
tar zcvf FileName.tar.gz DirName

# zsh 用户解压文件
x FileName

# 安装 expect 工具
yum install expect

# 生成随机密码
mkpasswd -l 20 -s 0

2️⃣ 快速组合命令

# 文本搜索
grep "关键字" 文件名
grep -R "关键字" /path/to/dir

# 实时查看日志并过滤关键字
tail -f /var/log/nginx/access.log | grep "404"

# 查看占用最多内存的前 10 个进程
ps aux --sort=-%mem | head -n 10

# 查看占用最多 CPU 的前 10 个进程
ps aux --sort=-%cpu | head -n 10

# 查看某目录下最近修改的文件
ls -lt /path/to/dir

# 查找大文件
find / -type f -size +100M

# 查看网络连接与端口使用
netstat -anp | grep ESTABLISHED

# 查看指定进程的打开文件
lsof -p PID

3️⃣ 网络相关

# 查看所有网络端口
netstat -nultp

# 查看指定端口
netstat -anp | grep 端口号
lsof -i:端口号

# 查看 IP 地址信息
ip addr
ifconfig

4️⃣ 系统信息

# 系统监控
top           # 实时进程监控
htop          # 可视化监控
vmstat 1      # CPU 内存状态
iostat 1      # 磁盘 IO 状态

# 查看操作系统版本信息
cat /proc/version

# 查看当前操作系统内核信息
uname -a

# 查看发行版信息
cat /etc/issue
cat /etc/centos-release

# 查看 CPU 信息
lscpu

# 查看硬盘和分区信息
lsblk
fdisk -l

# 查看 PCI 总线设备信息
lspci

# 查看主板及系统信息
dmidecode | grep -A16 "System Information$"

5️⃣ 服务管理

# 启动服务
systemctl start nginx.service

# 停止服务
systemctl stop postfix.service

# 重启服务
systemctl restart nginx.service

# 查看服务状态
systemctl status postfix.service

# 设置服务开机自启
systemctl enable nginx.service

# 禁用服务开机启动
systemctl disable nginx.service

# 查看服务是否开机自启
systemctl is-enabled nginx.service

# 查看已启用开机启动的服务
systemctl list-unit-files | grep enabled