centos常用命令速查

磁盘相关

查看已安装软件包的大小并按由大到小排序并显示前面20位

rpm -qa --queryformat '%10{size} - %-25{name} \t %{version}\n' | sort -nr | head -20

查看磁盘空间利用率

df 

查看文件夹空间占用较大的前10位

du / | sort -nr | head -10

查找文件名

find  | grep [regular expression]

查找文件内容(遍历子文件夹)

grep -r [regular expression]

列出当前每个子文件夹的磁盘空间使用量

du -sch *

打包文件夹

tar cvzf <tar filename> <folder>
示例
tar cvzf temp.tar  temp

解压文件夹

tar xvf <tar filename> -C <output folder>
示例
tar xvf temp.tar -C myfolder

验证打包文件是否破坏

tar -tzf <tar file> >/dev/null
如果没有输出表示有效,否则会输出错误信息

 

系统相关

查看linux 发行套件和版本

cat /etc/os-release

 输出结果类似

PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

 

查看linux的可执行程序的绝对路径

whereis [command]
示例
whereis docker
输出类似
/usr/bin/docker /etc/docker /usr/libexec/docker /usr/share/man/man1/docker.1.gz

 

安全相关

添加用户

useradd [username]
默认会同时添加同名的组

修改用户名密码
passwd [username]

验证已添加用户
cat /etc/passwd | grep  [username]

验证已添加组
cat /etc/group  | grep  [groupname]

修改文件的所有者

chown [owner's username] [file/folder]
示例:
chown test test.txt

禁止某用户账号登录系统

usermod -s /sbin/nologin [username]

验证
cat /etc/passwd | grep [username]
查看是否有nologin标识

如何查看文件的权限

ls -al

结果类似:

-rw-r--r--.  1 test                root                    0 4月  23 02:09 test.txt

左侧第一串字符表示权限,一共10个字符
 d: 表示目录
 -:  无
 r:  读权限
 w:  写权限
 x:  执行权限

第1个字符表示是目录还是文件
第2-4个:  表示所有者的权限
第5-7个: 表示所属组的权限
第8-10个: 表示其他人的权限

给用户添加sudo权限

以root身份将用户添加到wheel组
usermod -aG wheel <username>

示例: 添加test用户
usermod -aG wheel test

然后使用test用户登录就可以使用sudo方式执行命令了
sudo <command>

注:如果用户在添加到wheel组前未注销,需要先注销在登录才能生效

网络相关

解析主机名到IP

getent hosts [hostname]
示例
getent hosts www.baidu.com

查看路由或IP地址

查看ip地址
ip address

查看路由
ip route

查看进程监听的端口

sudo ss -tulpn
或
sudo netstat -tulpn (已过时)

显示结果类似

Netid State      Recv-Q Send-Q                                          Local Address:Port                                                         Peer Address:Port              
udp   UNCONN     0      0                                                   127.0.0.1:323                                                                     *:*                   users:(("chronyd",pid=884,fd=5))
udp   UNCONN     0      0                                                       [::1]:323                                                                  [::]:*                   users:(("chronyd",pid=884,fd=6))
tcp   LISTEN     0      100                                                 127.0.0.1:25                                                                      *:*                   users:(("master",pid=1551,fd=13))
tcp   LISTEN     0      128                                                         *:22                                                                      *:*                   users:(("sshd",pid=1238,fd=3))
tcp   LISTEN     0      100                                                     [::1]:25                                                                   [::]:*                   users:(("master",pid=1551,fd=14))
tcp   LISTEN     0      128                                                      [::]:5100                                                                 [::]:*                   users:(("docker-proxy",pid=19765,fd=4))
tcp   LISTEN     0      128                                                      [::]:2288                                                                 [::]:*                   users:(("docker-proxy",pid=19754,fd=4))
tcp   LISTEN     0      128                                                      [::]:22                                                                   [::]:*                   users:(("sshd",pid=1238,fd=4))

 

内存相关

查看内存的使用情况

free 

输出类似如下:
              total        used        free      shared  buff/cache   available
Mem:       10479448     8828880       82432        9144     1568136     1547876
Swap:       1048572           0     1048572

查看进程内存的使用情况

top 

输出类似如下:
VIRT 表示虚拟内存空间
RES  表示常驻内存空间
SHR  表示使用的共享进程的空间

top - 13:29:07 up  1:51,  1 user,  load average: 0.00, 0.01, 0.15
Tasks:  99 total,   1 running,  98 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 10479448 total,    81940 free,  8829172 used,  1568336 buff/cache
KiB Swap:  1048572 total,  1048572 free,        0 used.  1547584 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                          
28524 test      20   0  162004   2212   1544 R  0.3  0.0   0:00.07 top                                                                                              
    1 root      20   0  128620   7160   4088 S  0.0  0.1   0:01.71 systemd                                                                                          
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                                                                         
    4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                                                                     
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.80 ksoftirqd/0                                                                                      
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                                                                      
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                                                                           
    9 root      20   0       0      0      0 S  0.0  0.0   0:07.64 rcu_sched                                                                                        
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain                                                                                    
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.08 watchdog/0                                                                                       
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                                                                        
   14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                                                                                            
   15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd                                                                                       
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback                                                                                        
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd                                                                                      
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                                                                                           
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                                                                                           
   20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                                                                                           
   21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd                                                                                          
   22 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md                                                                                               
   23 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 edac-poller                                                                                      
   24 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 watchdogd                                                                                        
   30 root      20   0       0      0      0 S  0.0  0.0   0:00.08 kswapd0                                                                                          
   31 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd                                                                                             
   32 root      39  19       0      0      0 S  0.0  0.0   0:00.66 khugepaged                                                                                       
   33 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto                                   

服务相关

自动启动服务

sudo systemctl enable application.service

禁用自动启动服务

sudo systemctl disable application.service

启动、停止、重启服务

sudo systemctl start|stop|restart application.service

如果修改了配置文件,需要重新加载后,再重启服务

sudo systemctl daemon-reload
sudo systemctl restart application.service

列出所有的服务

systemctl list-unit-files

 

posted on 2020-03-08 17:35  omage  阅读(90)  评论(0编辑  收藏  举报