常用的Linux命令
系统常用命令
比较常用的一些Linux命令(基于CentOS 7.X),希望对大家有所帮助!
cd
- 改变当前操作目录:
cd /usr/local
ls
- 列出指定目录下所有文件,列出根目录文件:
ls -l /
pwd
- 获取当前目录绝对路径:
pwd
date
- 显示或者修改系统时间与日期:
date '+%Y-%m-%d %H:%M:%S'
clear
- 清除当前屏幕信息
clear
su
- 切换到超级用户
su -
passwd
- 设置用户密码:
passwd root
ps
- 显示系统进程运行动态:
ps -ef
- 查看某个进程的运行动态:
ps -ef | grep java
free
- 显示系统内存状态(单位MB):
free -m
who
- 查询系统处于什么运行级别:
who -r
man
- 显示指定命令的帮助信息:
man ls
mkdir
- 创建目录:
mkdir /temp
top
- 查看即时活跃进程,类似Windows的任务管理器。
top
more
- 分页查看文件,例如每页10行查看boot.log文件:
more -c -10 /var/log/boot.log
cat
- 查看文件,例如查看Linux启动日志文件文件,并标明行号:
cat -Ab /var/log/boot.log
touch
- 创建文件,例如创建touch_info.txt文件:
touch touch_info.txt
rm
- 删除文件:
rm touch_info.txt
- 强制删除某个目录及其子目录:
rm -rf test/
cp
- 用于拷贝文件,例如将testA目录复制到testB目录
cp -r /temp/testA /temp/testB
mv
- 移动或覆盖文件:
mv text1.txt text2.txt
压缩与解压
tar
- 将/etc文件夹中的文件归档到文件etc.tar(并不会进行压缩):
tar -cvf /temp/etc.tar /etc
- 用gzip压缩文件夹/etc中的文件到文件etc.tar.gz:
tar -zcvf /temp/etc.tar.gz /etc
- 用bzip2压缩文件夹/etc到文件/etc.tar.bz2:
tar -jcvf /temp/etc.tar.bz2 /etc
- 分页查看压缩包中内容(gzip):
tar -ztvf /temp/etc.tar.gz |more -c -10
- 解压文件到当前目录(gzip):
tar -zxvf /temp/etc.tar.gz
- 解压文件到指定目录(gzip):
tar -zxvf /temp/etc.tar.gz -C /temp/etc
磁盘和网络管理
df
- 查看磁盘空间占用情况:
df -hT
dh
- 查看当前目录下的文件及文件夹所占大小:
du -h --max-depth=1 ./*
ifconfig
- 显示当前网络接口状态:
ifconfig
netstat
- 查看当前路由信息:
netstat -rn
- 查看所有有效TCP连接:
netstat -an
- 查看系统中启动的监听服务:
netstat -tulnp
- 查看处于连接状态的系统资源信息:
netstat -atunp
wget
- 查看当前路由信息:
wget http://***
系统服务管理
systemctl
- 输出系统中各个服务的状态:
systemctl list-units --type=service
- 查看服务的运行状态:
systemctl status firewalld
- 关闭服务:
systemctl stop firewalld
- 启动服务:
systemctl start firewalld
- 重新启动服务(不管当前服务是启动还是关闭):
systemctl restart firewalld
- 重新载入配置信息而不中断服务:
systemctl reload firewalld
- 禁止服务开机自启动:
systemctl disable firewalld
- 设置服务开机自启动:
systemctl enable firewalld
软件的安装与管理
rpm
- 安装软件包:
rpm -ivh nginx-1.12.2-2.el7.x86_64.rpm
- 模糊搜索软件包:
rpm -qa | grep nginx
- 精确查找软件包:
rpm -qa nginx
- 查询软件包的安装路径:
rpm -ql nginx-1.12.2-2.el7.x86_64
- 查看软件包的概要信息:
rpm -qi nginx-1.12.2-2.el7.x86_64
- 验证软件包内容和安装文件是否一致:
rpm -V nginx-1.12.2-2.el7.x86_64
- 更新软件包:
rpm -Uvh nginx-1.12.2-2.el7.x86_64
- 删除软件包:
rpm -e nginx-1.12.2-2.el7.x86_64
yum
- 安装软件包:
yum install nginx
- 检查可以更新的软件包:
yum check-update
- 更新指定的软件包:
yum update nginx
- 在资源库中查找软件包信息:
yum info nginx*
- 列出已经安装的所有软件包:
yum info installed
- 列出软件包名称:
yum list nginx*
- 模糊搜索软件包:
yum search nginx
用户管理
用户信息查看
- 查看用户信息:
cat /etc/passwd
- 用户信息格式如下(密码已过滤):
# 用户名:密码:用户标识号:组标识号:组注释性描述:主目录:默认shell
root:x:0:0:root:/root:/bin/bash
jason:x:1000:982:jason:/home/jason:/bin/bash
- 查看用户组信息:
cat /etc/group
- 用户组信息格式如下:
# 组名:密码:组标识号:组内用户列表
root:x:0:
docker:x:982:jason,andy
passwd
- 用于设置用户密码:
passwd root
su
- 改变用户身份(切换到超级用户):
# 切换到root用户
su -
# 切换到jason用户
su jason
groupadd
- 添加用户组,使用-g可以设置用户组的标志号:
groupadd -g 1024 jasonbo
groupdel
- 删除用户组:
groupdel jasonbo
useradd
- 添加用户,-u设置标志号,-g设置主用户组:
useradd -u 1024 -g jasonbo jason
usermod
- 修改用户所属用户组:
usermod -g docker jason
userdel
- 删除用户,使用-r可以删除用户主目录:
userdel jason -r
Iptables
安装
由于CenterOS7.0以上版本并没有预装Iptables,我们需要自行装。
- 安装前先关闭firewall防火墙
- 安装iptables:
yum install iptables
- 安装iptables-services:
yum install iptables-services
使用
- 开启防火墙:
systemctl start iptables.service
- 关闭防火墙:
systemctl stop iptables.service
- 查看防火墙状态:
systemctl status iptables.service
- 设置开机启动:
systemctl enable iptables.service
- 禁用开机启动:
systemctl disable iptables.service
- 查看filter表的几条链规则(INPUT链可以看到开放了哪些端口):
iptables -L -n
- 查看NAT表的链规则:
iptables -t nat -L -n
- 清除防火墙所有规则:
iptables -F
iptables -X
iptables -Z
- 给INPUT链添加规则(开放8080端口):
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
- 查找规则所在行号:
iptables -L INPUT --line-numbers -n
- 根据行号删除过滤规则(关闭8080端口):
iptables -D INPUT 1

浙公网安备 33010602011771号