Linux的基本指令(三):需要掌握的高级指令

本文详述了Linux系统中的一些高级指令,包括hostname、id、whoami、ps-ef、top、du-sh、find、service、kill、ifconfig、reboot、shutdown、uptime、uname、netstat-tnlp以及man。每个命令的语法、作用和使用场景都有清晰的解释,并提供了实际操作示例,有助于理解并掌握这些常用命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里个人复习和巩固一下比较重要的一些高级指令,大家在看的时候也可以根据自己日常工作的需要结合起来思考一下,这些命令对于日常工作的帮助。

1、hostname指令

作用:操作服务器的主机名(读取、设置)

语法1:#hostname                    含义:表示输出完整的主机名

语法2:#hostname  -f                    含义:表示输出当前主机名中的FQDN(全限定域名)

[root@Peter ~]# hostname

Peter

[root@Peter ~]# hostname -f

Peter

[root@Peter ~]#

 

2、id指令

作用:查看一个用户的一些基本信息(包含用户id,用户组id,附加组id…),该指令如果不指定用户则默认当前用户。

语法1:#id             默认显示当前执行该命令的用户的基本信息

语法2:#id  用户名           显示指定用户的基本信息

 

[root@Peter ~]# id

uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[root@Peter ~]# id 1

uid=1(bin) gid=1(bin) 组=1(bin)

[root@Peter ~]# id 2

uid=2(daemon) gid=2(daemon) 组=2(daemon)

[root@Peter ~]#

验证上述信息是否正确?

验证用户信息:通过文件/etc/passwd

验证用户组信息:通过文件/etc/group

[root@Peter ~]# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

 

3、whoami指令

作用:“我是谁?”显示当前登录的用户名,一般用于shell脚本,用于获取当前操作的用户名方便记录日志。

语法:#whoami

[root@Peter ~]# whoami

root

[root@Peter ~]#

 

4、ps -ef指令(重点)

指令:ps   

作用:主要是查看服务器的进程信息

选项含义:

      -e:等价于“-A”,表示列出全部的进程

      -f:显示全部的列(显示全字段)

 

执行结果:

[root@Peter ~]# ps -ef

UID         PID   PPID  C STIME TTY          TIME CMD

root          1      0  0 16:36 ?        00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 22

root          2      0  0 16:36 ?        00:00:00 [kthreadd]

root          4      2  0 16:36 ?        00:00:00 [kworker/0:0H]

root          6      2  0 16:36 ?        00:00:00 [ksoftirqd/0]

root          7      2  0 16:36 ?        00:00:00 [migration/0]

root          8      2  0 16:36 ?        00:00:00 [rcu_bh]

root          9      2  0 16:36 ?        00:00:04 [rcu_sched]

root         10      2  0 16:36 ?        00:00:00 [lru-add-drain]

root         11      2  0 16:36 ?        00:00:00 [watchdog/0]

root         12      2  0 16:36 ?        00:00:00 [watchdog/1]

root         13      2  0 16:36 ?        00:00:00 [migration/1]

root         14      2  0 16:36 ?        00:00:00 [ksoftirqd/1]

root         16      2  0 16:36 ?        00:00:00 [kworker/1:0H]

root         17      2  0 16:36 ?        00:00:00 [watchdog/2]

root         18      2  0 16:36 ?        00:00:00 [migration/2]

root         19      2  0 16:36 ?        00:00:00 [ksoftirqd/2]

root         21      2  0 16:36 ?        00:00:00 [kworker/2:0H]

root         22      2  0 16:36 ?        00:00:00 [watchdog/3]

root         23      2  0 16:36 ?        00:00:00 [migration/3]

root         24      2  0 16:36 ?        00:00:00 [ksoftirqd/3]

列的含义:

UID:该进程执行的用户id;

PID:进程id;

PPID:该进程的父级进程id,如果一个程序的父级进程找不到,该程序的进程称之为僵尸进程(parent process ID);

C:Cpu的占用率,其形式是百分数;

STIME:进行的启动时间;

TTY:终端设备,发起该进程的设备识别符号,如果显示“?”则表示该进程并不是由终端设备发起;

TIME:进程的执行时间;

CMD:该进程的名称或者对应的路径;

 

案例:(100%使用的命令)在ps的结果中过滤出想要查看的进程状态

#ps -ef|grep “进程名称”

[root@Peter ~]# ps -ef |grep watchdog/2

root         17      2  0 16:36 ?        00:00:00 [watchdog/2]

root       8401   8277  0 22:02 pts/3    00:00:00 grep --color=auto watchdog/2

[root@Peter ~]#

 

再例如查看火狐浏览器的进程:

[root@Peter ~]# ps -ef |grep firefox

root       8403   8277  0 22:03 pts/3    00:00:00 grep --color=auto firefox

[root@Peter ~]# ps -ef |grep firefox

root       8405   8277  0 22:03 pts/3    00:00:00 grep --color=auto firefox

[root@Peter ~]#

 

5、top指令(重点)

作用:查看服务器的进程占的资源(100%使用)

语法:

      进入命令:#top                     (动态显示)

      退出命令:按下q键

输出的结果:

top - 22:05:59 up  5:29,  5 users,  load average: 0.05, 0.04, 0.05

Tasks: 242 total,   1 running, 241 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.1 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

KiB Mem :  1863028 total,   293052 free,  1063220 used,   506756 buff/cache

KiB Swap:  2097148 total,  1881340 free,   215808 used.   578284 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                         

  8416 root      20   0  162292   2352   1584 R   0.7  0.1   0:00.68 top                                                                            

   793 root      20   0  295564   2816   2368 S   0.3  0.2   0:24.37 vmtoolsd                                                                        

   800 root      20   0  397456   4064   2756 S   0.3  0.2   0:00.65 accounts-daemon

表头含义:

PID:进程id;

USER:该进程对应的用户;

PR:优先级;

VIRT:虚拟内存;

RES:常驻内存;

SHR:共享内存;

      计算一个进程实际使用的内存 = 常驻内存(RES)- 共享内存(SHR)

S:表示进程的状态status(sleeping,其中S表示睡眠,R表示运行);

%CPU:表示CPU的占用百分比;

%MEM:表示内存的占用百分比;

TIME+:执行的时间;

COMMAND:进程的名称或者路径;

 

在运行top的时候,可以按下方便的快捷键:

M:表示将结果按照内存(MEM)从高到低进行降序排列;

P:表示将结果按照CPU使用率从高到低进行降序排列;

1:当服务器拥有多个cpu的时候可以使用“1”快捷键来切换是否展示显示各个cpu的详细信息;

 

6、du -sh指令

作用:查看目录的真实大小

语法:#du -sh 目录路径

选项含义:

      -s:summaries,只显示汇总的大小

      -h:表示以高可读性的形式进行显示

 

案例:统计“/root/doc/”目录的实际大小

[root@Peter ~]# du -sh doc/

2.5G     doc/

[root@Peter ~]#

 

案例:统计“/etc”目录实际大小

[root@Peter ~]# du -sh /etc/

60M      /etc/

[root@Peter ~]#

 

7、find指令

作用:用于查找文件(其参数有55个之多)

语法:#find 路径范围 选项 选项的值

选项:

      -name:按照文档名称进行搜索(支持模糊搜索)

      -type:按照文档的类型进行搜索

             文档类型:“-”表示文件(在使用find的时候需要用f来替换),“d”表示文件夹

 

案例:使用find来搜索CentOS-Base.repo

 

[root@Peter ~]# find /etc/ -name CentOS-Base.repo*

/etc/yum.repos.d/CentOS-Base.repo.bak

/etc/yum.repos.d/CentOS-Base.repo.bak1

/etc/yum.repos.d/CentOS-Base.repo.bak2

/etc/yum.repos.d/CentOS-Base.repo

[root@Peter ~]#

案例:搜索etc目录下所有的conf后缀文件

[root@Peter ~]# find /etc/ -name *.conf

/etc/resolv.conf

/etc/fonts/conf.d/61-urw-c059.conf

/etc/fonts/conf.d/31-cantarell.conf

/etc/fonts/conf.d/66-sil-nuosu.conf

/etc/fonts/conf.d/59-liberation-sans.conf

/etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf

/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf

/etc/fonts/conf.d/61-urw-d050000l.conf

/etc/fonts/conf.d/57-dejavu-sans.conf

/etc/fonts/conf.d/65-0-ttf-arphic-uming.conf

/etc/fonts/conf.d/10-hinting-slight.conf

/etc/fonts/conf.d/59-liberation-serif.conf

/etc/fonts/conf.d/10-scale-bitmap-fonts.conf

/etc/fonts/conf.d/65-wqy-microhei.conf

/etc/fonts/conf.d/20-unhint-small-vera.conf

/etc/fonts/conf.d/65-0-lohit-marathi.conf

/etc/fonts/conf.d/25-no-bitmap-fedora.conf

/etc/fonts/conf.d/67-lohit-malayalam.conf

/etc/fonts/conf.d/25-unhint-nonlatin.conf

 

案例:使用find来搜索/etc/sane.d/目录下所有的文件

[root@Peter ~]# find /etc/sane.d/ -type f

/etc/sane.d/abaton.conf

/etc/sane.d/ricoh.conf

/etc/sane.d/agfafocus.conf

/etc/sane.d/apple.conf

/etc/sane.d/artec.conf

/etc/sane.d/rts8891.conf

/etc/sane.d/artec_eplus48u.conf

 

案例:使用find来搜索/usr/bin/目录下所有的文件夹

[root@Peter ~]# find /usr/bin/ -type d

/usr/bin/

[root@Peter ~]#

 

8、service指令(重点)

作用:用于控制一些软件的服务启动/停止/重启

语法:#service 服务名 start/stop/restart

 

例如:需要启动本机安装的Apache(网站服务器软件),其服务名httpd

[root@Peter ~]# service httpd start

Redirecting to /bin/systemctl start httpd.service

Failed to start httpd.service: Unit not found.

[root@Peter ~]#

通过ps命令来检查httpd服务是否启动:

[root@Peter ~]# ps -ef | grep httpd

root       9334   8277  0 22:21 pts/3    00:00:00 grep --color=auto httpd

[root@Peter ~]#

 

9、kill指令(重点)

作用:表示杀死进程            (当遇到僵尸进程或者出于某些原因需要关闭进程的时候)

语法:#kill  进程PID          (语法需要配合ps一起使用)

 

案例:需要kill掉httpd的进程

[root@Peter ~]# ps -ef |grep httpd

root       9361   8277  0 22:22 pts/3    00:00:00 grep --color=auto httpd

[root@Peter ~]# kill 9361

-bash: kill: (9361) - 没有那个进程

[root@Peter ~]# kill 8277

[root@Peter ~]# kill 8277

[root@Peter ~]#

与kill命令作用相似但是比kill更加好用的杀死进程的命令:killall

语法:#killall 进程名称

[root@Peter ~]# killall httpd

httpd: no process found

[root@Peter ~]#

 

10、ifconfig指令(重点)

作用:用于操作网卡相关的指令。

简单语法:#ifconfig             (获取网卡信息)

[root@Peter ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.14.222  netmask 255.255.255.0  broadcast 192.168.14.255

        inet6 fe80::6b09:18ce:699e:bfce  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:c6:12:84  txqueuelen 1000  (Ethernet)

        RX packets 24769  bytes 16148382 (15.4 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 12610  bytes 6435699 (6.1 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        loop  txqueuelen 1000  (Local Loopback)

        RX packets 64  bytes 5396 (5.2 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 64  bytes 5396 (5.2 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500

        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255

        ether 52:54:00:fb:0b:83  txqueuelen 1000  (Ethernet)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

[root@Peter ~]#

Eth0表示Linux中的一个网卡,eth0是其名称。Lo(loop,本地回还网卡,其ip地址一般都是127.0.0.1)也是一个网卡名称。

注意:inet addr就是网卡的ip地址。

 

11、reboot指令

作用:重新启动计算机       

语法1:#reboot            重启

语法2:#reboot  -w   模拟重启,但是不重启(只写关机与开机的日志信息)

 

12、shutdown指令

作用:关机                     (慎用)

语法1:#shutdown -h now       “关机提示”  或者  #shutdown  -h 15:25  “关机提示”

案例:设置Linux系统关机时间在半夜12:00

[root@Peter ~]# reboot -w

[root@Peter ~]# shutdown -h 24:00

Failed to parse time specification: 24:00

[root@Peter ~]#

 

如果想要取消关机计划的话,则可以按照以下方式去尝试:

①针对于centos7.x之前的版本:ctrl+c

②针对于centos7.x(包含)之后的版本:#shutdown  -c

 

除了shutdown关机以外,还有以下几个关机命令:

#init 0

#halt

#poweroff

 

13、uptime指令

作用:输出计算机的持续在线时间(计算机从开机到现在运行的时间)

语法:#uptime

[root@Peter ~]# uptime

 22:25:54 up  5:49,  5 users,  load average: 0.00, 0.01, 0.05

[root@Peter ~]#

 

14、uname指令

作用:获取计算机操作系统相关信息

语法1:#uname                   获取操作系统的类型

语法2:#uname  -a           all,表示获取全部的系统信息(类型、全部主机名、内核版本、发布时间、开源计划)

[root@Peter ~]# uname

Linux

[root@Peter ~]# uname -a

Linux Peter 3.10.0-1160.6.1.el7.x86_64 #1 SMP Tue Nov 17 13:59:11 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

[root@Peter ~]#

15、netstat -tnlp指令

作用:查看网络连接状态

语法:#netstat -tnlp

[root@Peter ~]# netstat -tnlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      770/rpcbind        

tcp        0      0 0.0.0.0:8787            0.0.0.0:*               LISTEN      1584/rserver       

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2107/dnsmasq       

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1423/sshd          

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1424/cupsd         

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1652/master        

tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      3379/sshd: root@pts

tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      8273/sshd: root@pts

tcp6       0      0 :::3306                 :::*                    LISTEN      1680/mysqld        

tcp6       0      0 :::111                  :::*                    LISTEN      770/rpcbind        

tcp6       0      0 :::22                   :::*                    LISTEN      1423/sshd          

tcp6       0      0 ::1:631                 :::*                    LISTEN      1424/cupsd          

tcp6       0      0 ::1:25                  :::*                    LISTEN      1652/master        

tcp6       0      0 ::1:6010                :::*                    LISTEN      3379/sshd: root@pts

tcp6       0      0 ::1:6011                :::*                    LISTEN      8273/sshd: root@pts

[root@Peter ~]#

选项说明:

      -t:表示只列出tcp协议的连接;

      -n:表示将地址从字母组合转化成ip地址,将协议转化成端口号来显示;

      -l:表示过滤出“state(状态)”列中其值为LISTEN(监听)的连接;

      -p:表示显示发起连接的进程pid和进程名称;

 

 

16、man指令

作用:manual,手册(包含了Linux中全部命令手册,英文)

语法:#man 命令                (退出按下q键)

案例:通过man命令查询cp指令的用法

[root@Peter ~]# man cp

CP(1)                                                         General Commands Manual                                                         CP(1)

NAME

       cp - 复制文件和目录

总览

       cp [选项] 文件路径

       cp [选项] 文件...目录

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上花开香正浓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值