Linux部分命令

hostname

操作服务器的主机名(读取和设置)

语法1.

hostname :表示输出当前完整的主机名

1
2
root@firstpc:~# hostname
firstpc

hostname -f :表示输出当前主机名中的FQDN(全限定域名)

1
2
root@firstpc:~# hostname -f
firstpc

id

用于查看一个用户的一些基本信息

语法2.

id :默认当前用户的基本信息

id 用户名 :指定用户的基本信息

1
2
3
4
5
6
7
root@firstpc:~# id muchenH
uid=1000(muchenH) gid=1000(muchenH) groups=1000(muchenH),27(sudo)
root@firstpc:~# id root
uid=0(root) gid=0(root) groups=0(root)
root@firstpc:~# id
uid=0(root) gid=0(root) groups=0(root)
root@firstpc:~#

whoami

用于查询“我是谁” 显示当前登录的用户名

whoami &&who am i

1
2
3
4
root@firstpc:~# whoami
root
root@firstpc:~# who am i
root pts/0 2021-09-05 15:59 (223.104.35.20)

ps -ef

用于查看服务器的进程信息

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

-f :显示所有的列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
root@firstpc:~# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 00:14 ? 00:00:10 /sbin/init nopti nospectre_v2 nospec_store_bypass_disable
root 2 0 0 00:14 ? 00:00:00 [kthreadd]
root 235 2 0 00:14 ? 00:00:00 [scsi_tmf_31]
root 263 2 0 00:14 ? 00:00:01 [jbd2/vda1-8]
root 264 2 0 00:14 ? 00:00:00 [ext4-rsv-conver]
root 312 1 0 00:14 ? 00:00:02 /lib/systemd/systemd-journald
root 350 1 0 00:14 ? 00:00:00 /lib/systemd/systemd-udevd
systemd+ 450 1 0 00:14 ? 00:00:00 /lib/systemd/systemd-networkd
systemd+ 465 1 0 00:14 ? 00:00:01 /lib/systemd/systemd-resolved
root 471 2 0 00:14 ? 00:00:00 [cryptd]
root 501 2 0 00:14 ? 00:00:00 [nfit]
root 533 1 0 00:14 ? 00:00:01 /usr/lib/accountsservice/accounts-daemon
root 542 1 0 00:14 ? 00:00:00 /usr/sbin/cron -f
message+ 544 1 0 00:14 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork
_chrony 558 1 0 00:14 ? 00:00:00 /usr/sbin/chronyd -F -1
_chrony 559 558 0 00:14 ? 00:00:00 /usr/sbin/chronyd -F -1
root 571 1 0 00:14 ? 00:00:00 /opt/bcm-agent/var/lib/bcm-agent/bcm-agent.upgrader --upd
root 574 571 0 00:14 ? 00:00:09 ./bcm-agent
root 583 1 0 00:14 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-start
syslog 589 1 0 00:14 ? 00:00:00 /usr/sbin/rsyslogd -n -iNONE
root 592 1 0 00:14 ? 00:00:00 /lib/systemd/systemd-logind
root 595 1 0 00:14 ? 00:00:02 /opt/hosteye/bin/hosteye
root 616 1 0 00:14 ? 00:00:01 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 638 595 0 00:14 ? 00:07:35 /opt/hosteye/bin/hosteye --is_child_mode=true --is_consol
root 683 1 0 00:14 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; maste
www-data 685 683 0 00:14 ? 00:00:00 nginx: worker process
root 697 1 0 00:14 ? 00:00:00 /usr/lib/policykit-1/polkitd --no-debug
root 699 1 0 00:14 ttyS0 00:00:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 tt
root 705 1 0 00:14 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattende
root 710 1 0 00:14 tty1 00:00:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root 723 574 0 00:14 ? 00:00:38 ./bcm-si -N 3219cd74-0ca4-4822-8069-795585a5943f
root 1582 1 0 00:32 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd.conf
root 7824 2 0 13:26 ? 00:00:05 [kworker/0:1-events]
root 8978 616 0 15:59 ? 00:00:00 sshd: root@pts/0
root 8991 1 0 15:59 ? 00:00:00 /lib/systemd/systemd --user
root 8992 2 0 15:59 ? 00:00:00 [kworker/0:0-events]
root 8993 8991 0 15:59 ? 00:00:00 (sd-pam)
root 9017 8978 0 15:59 pts/0 00:00:00 -bash
root 9138 2 0 16:09 ? 00:00:00 [kworker/u2:2-events_power_efficient]
root 9322 2 0 16:19 ? 00:00:00 [kworker/u2:1-events_power_efficient]
root 9391 2 0 16:25 ? 00:00:00 [kworker/u2:0-events_unbound]
root 9405 9017 0 16:27 pts/0 00:00:00 ps -ef

UID:该进程执行的用户ID

PID:进程ID

PPID:该进程的父级进程ID ,如果一个程序的父进程找不到,该程序的进程被称为僵尸进程

C:CPU的占用率,形式为百分数

STIME:进行的启动时间

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

TIME:进程的执行时间

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

ps -ef|grep ftp 例如查看ftp的进程

1
2
3
4
root@firstpc:~# ps -ef|grep ftp
root 1582 1 0 00:32 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd.conf
root 9505 9017 0 16:37 pts/0 00:00:00 grep --color=auto ftp
root@firstpc:~#

top

用于查看服务器进程占用的资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@firstpc:~# top
top - 16:42:29 up 16:27, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 143 total, 1 running, 142 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.0 us, 0.7 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
MiB Mem : 981.2 total, 226.3 free, 128.0 used, 626.9 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 704.2 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
638 root 20 0 836052 44876 21956 S 0.3 4.5 7:42.58 hosteye
1 root 20 0 102596 12396 8420 S 0.0 1.2 0:10.30 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-kblockd
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
10 root 20 0 0 0 0 S 0.0 0.0 0:05.38 ksoftirqd/0
11 root 20 0 0 0 0 I 0.0 0.0 0:02.59 rcu_sched
12 root rt 0 0 0 0 S 0.0 0.0 0:00.31 migration/0
13 root -51 0 0 0 0 S 0.0 0.0 0:00.00 idle_inject/0
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
16 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_kthre
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kauditd
19 root 20 0 0 0 0 S 0.0 0.0 0:00.02 khungtaskd
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 oom_reaper
21 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 writeback
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kcompactd0
23 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
24 root 39 19 0 0 0 S 0.0 0.0 0:00.00 khugepaged
70 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kintegrityd

q :退出

PID:进程ID

USER:该进程对应的用户

PR:优先级

NI:用户进程空间优先级的进程占用CPU百分比

VIRT:虚拟内存

RES:常驻内存

SHR:共享内存    实际使用内存=常驻内存-共享内存

S:睡眠进程  R表示运行

TIME+:执行的时间

COMMAND:进程的名称或者路径

top 指令快捷键

M:将结果按照从高到低降序排列

P:奖结果按照CPU的使用率从高到低排列

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

du -sh

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

du -sh 路径

选项含义:
-s:只显示汇总的大小

-h:以人性化形式进行显示
1
2
3
root@firstpc:/# du -sh /etc
5.2M /etc
root@firstpc:/#

find

用于查找文件

find 路径 选项 选项的值

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

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

        文档类型:“-” 表示文件 在find时需要使用f来替换 “d”表示文件夹
1
2
3
root@firstpc:/home/muchenH# find /home/muchenH/ -name *.txt
/home/muchenH/token.txt
root@firstpc:/home/muchenH#

service

服务 用于控制某些软件的启动、重启和停止

service 服务名 start/restart/stop

1
2
3
root@firstpc:/home/muchenH# service vsftpd restart
root@firstpc:/home/muchenH# service vsftpd stop
root@firstpc:/home/muchenH# service vsftpd start

kill

杀死进程

kill 进程PID

killall 进程名称

ifconfig

查看ip相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@firstpc:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.4 netmask 255.255.240.0 broadcast 192.168.15.255
inet6 fe80::f827:ff:fe07:8c82 prefixlen 64 scopeid 0x20<link>
ether fa:27:00:07:8c:82 txqueuelen 1000 (Ethernet)
RX packets 141045 bytes 18719637 (18.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 141517 bytes 43201291 (43.2 MB)
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 57230 bytes 24052983 (24.0 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 57230 bytes 24052983 (24.0 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

reboot

重新启动计算机

shutdown

关机 (服务器慎用)

shutdown -h now & shutdown -h xx:xx “关机提示”

shutdown -c 取消关机命令

ps:其他关机命令

init 0

halt

poweroff

uptime

作用 :输出计算机的持续在线时间

uptime

1
2
3
root@firstpc:~# uptime
19:23:17 up 19:08, 3 users, load average: 0.00, 0.00, 0.00
root@firstpc:~#

uname

获取操作系统相关信息

1
2
muchenH@firstpc:~$ uname
Linux

uname -a 表示获取完整的系统信息(类型 全部主机名字 内核版本 发布时间 开源计划)

1
2
muchenH@firstpc:~$ uname -a
Linux firstpc 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

netstat -tnlp

查看网络的连接状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
muchenH@firstpc:~$ netstat -tnlp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
tcp6 0 0 :::21 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:6010 :::* LISTEN -
tcp6 0 0 ::1:6011 :::* LISTEN -
tcp6 0 0 :::781 :::* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -

选项说明:

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

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

-l 表示过滤出 “State”列中其值为LISTEN的连接

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

man

命令手册

删除光标前后的命令

CTRL +u 删除光标前面的命令

CTRL +k 删除光标后面的命令


Linux部分命令
http://example.com/2021/09/05/Linux部分命令/
作者
muchenH
发布于
2021年9月5日
许可协议