Linux筆記03(經常使用命令)

本次主要仍是弄一些基礎命令……node

一、系統狀態檢測命令

一、ifconifg

查看網卡信息linux

[root@mingmingxingforcomputer ~]# ifconfig 
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.15  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::3928:f622:d292:1173  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ba:95:53  txqueuelen 1000  (Ethernet)
        RX packets 61  bytes 8494 (8.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 8801 (8.5 KiB)
        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 48  bytes 5616 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48  bytes 5616 (5.4 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:a0:0a:88  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
參數 描述
ens160 網卡名稱
inet 192.168.0.15 IP地址
netmask 255.255.255.0 子網掩碼
ether 00:0c:29:ba:95:53 物理地址

二、uname

查看系統信息redis

[root@mingmingxingforcomputer ~]# uname 
Linux
[root@mingmingxingforcomputer ~]# uname -a
Linux mingmingxingforcomputer 4.18.0-80.el8.x86_64 #1 SMP Wed Mar 13 12:02:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

三、uptime

查看系統的負載信息 shell

[root@mingmingxingforcomputer ~]# uptime
 15:03:53 up 9 min,  1 user,  load average: 0.00, 0.11, 0.12

負載值越低越好指的是對運維人員來說的,越低越安全省心。可是公司購置的硬件設備若是長期處於空閒狀態,明顯是種資源浪費,老闆也不會開心。因此咱們建議是負載值保持在1左右,不要超過5就好。 數據庫

四、free

用於顯示當前系統中內存的使用量信息 安全

[root@mingmingxingforcomputer ~]# free 
              total        used        free      shared  buff/cache   available
Mem:        7977588      767364     6767808        9880      442416     6939484
Swap:       2097148           0     2097148
// -h 參數能夠更加直觀的看到內存使用狀況
[root@mingmingxingforcomputer ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          7.6Gi       749Mi       6.5Gi       9.0Mi       432Mi       6.6Gi
Swap:         2.0Gi          0B       2.0Gi

五、who

查看當前登入主機的用戶終端信息 bash

[root@mingmingxingforcomputer ~]# who
root     pts/0        2021-06-26 14:55 (192.168.0.9)

六、last

用於調取主機的被訪記錄 markdown

[root@mingmingxingforcomputer ~]# last
root     pts/0        192.168.0.9      Sat Jun 26 14:55   still logged in
reboot   system boot  4.18.0-80.el8.x8 Fri Jun 25 01:16   still running
root     pts/1        192.168.0.9      Sat Jun 26 21:25 - 14:53  (-6:32)
root     tty2         tty2             Sat Jun 26 18:33 - down   (-3:40)
reboot   system boot  4.18.0-80.el8.x8 Sat Jun 26 18:20 - 14:53  (-3:26)
root     tty2         tty2             Sat Jun 26 05:30 - 07:01  (01:30)
reboot   system boot  4.18.0-80.el8.x8 Sat Jun 26 04:51 - 07:01  (02:10)
root     pts/2        192.168.0.2      Thu Jan 21 21:24 - 21:39  (00:15)
root     pts/2        192.168.0.2      Wed Jan 20 05:28 - 05:29  (00:00)
root     pts/1        192.168.0.2      Wed Jan 20 05:25 - 21:39 (1+16:14)
root     pts/1        192.168.0.2      Wed Jan 20 05:24 - 05:25  (00:00)
root     tty2         tty2             Wed Jan 20 05:20 - 21:39 (1+16:19)
reboot   system boot  4.18.0-80.el8.x8 Wed Jan 20 05:20 - 21:40 (1+16:19)
root     tty2         tty2             Wed Jan 20 05:00 - 05:17  (00:17)
reboot   system boot  4.18.0-80.el8.x8 Wed Jan 20 05:00 - 05:17  (00:17)
reboot   system boot  4.18.0-80.el8.x8 Wed Jan 20 04:59 - 05:17  (00:17)
root     tty2         tty2             Wed Jan 20 04:44 - down   (00:14)
reboot   system boot  4.18.0-80.el8.x8 Wed Jan 20 04:44 - 04:59  (00:15)
root     tty2         tty2             Mon Jan  4 03:40 - 04:52  (01:11)
reboot   system boot  4.18.0-80.el8.x8 Mon Jan  4 03:37 - 04:52  (01:14)

wtmp begins Mon Jan  4 03:37:53 2021

七、ping

控制ping次數【-c】網絡

[root@mingmingxingforcomputer ~]# ping -c 3 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.601 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=6.33 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=1.15 ms

--- 192.168.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 28ms
rtt min/avg/max/mdev = 0.601/2.693/6.332/2.582 ms
參數 做用
-c 總共發送次數
-l 指定網卡名稱
-i 每次間隔時間(秒)
-W 最長等待時間(秒)

八、tracepath

顯示數據包達到目的主機途中所通過的路由信息 架構

[root@mingmingxingforcomputer ~]# tracepath www.baidu.com
 1?: [LOCALHOST]                      pmtu 1500
 1:  _gateway                                              0.952ms 
 1:  _gateway                                              0.664ms 
 2:  _gateway                                              0.567ms pmtu 1492
 2:  115.32.24.1                                           3.276ms 
 3:  no reply
 4:  no reply

 查看自家路由:
 [root@mingmingxingforcomputer ~]# tracepath 192.168.0.1
 1?: [LOCALHOST]                      pmtu 1500
 1:  _gateway                                              0.666ms reached
 1:  _gateway                                              0.646ms reached
     Resume: pmtu 1500 hops 1 back 1

九、netstat

顯示如網絡鏈接、路由表、接口狀態等的網絡相關信息

[root@mingmingxingforcomputer ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 mingmingxingforc:domain 0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhos:x11-ssh-offset 0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN     
tcp        0    208 mingmingxingforcomp:ssh 192.168.0.9:vrtl-vmf-sa ESTABLISHED
[root@mingmingxingforcomputer ~]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens160           1500     6837      0      0 0          2504      0      0      0 BMRU
lo              65536      190      0      0 0           190      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU

十、history

顯示執行過的命令歷史

[root@mingmingxingforcomputer ~]# history | head -n 5
    1  history 
    2  sosreport 
    3  cd /home/
    4  pwd
    5  cd /root

十一、sosreport

收集系統配置及架構信息並輸出診斷文檔

[root@mingmingxingforcomputer ~]# sosreport 

sosreport (version 3.6)

This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp/sos.r6jscjcn and may be provided to a Red Hat support
representative.

Any information provided to Red Hat will be treated in accordance with
the published support policies at:

  https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press ENTER to continue, or CTRL-C to quit.

Please enter the case id that you are generating this report for []: y

 Setting up archive ...
 Setting up plugins ...
 Running plugins. Please wait ...

  Finishing plugins              [Running: processor]                                     anager]
  Finished running plugins                                                               
Creating compressed archive...

Your sosreport has been generated and saved in:
  /var/tmp/sosreport-mingmingxingforcomputer-y-2021-06-26-udermtc.tar.xz

The checksum is: 8f5dcd82d59eeb0772bcf32d0688bc78

Please send this file to your support representative.

二、定位文件命令

一、pwd

顯示用戶當前所處的工做目錄

[root@mingmingxingforcomputer ~]# cd ..
[root@mingmingxingforcomputer /]# pwd
/
[root@mingmingxingforcomputer /]# cd /etc
[root@mingmingxingforcomputer etc]# pwd
/etc

二、cd

切換當前的工做路徑

[root@mingmingxingforcomputer ~]# cd ..
[root@mingmingxingforcomputer /]# pwd
/

三、ls

顯示目錄中的文件信息

[root@mingmingxingforcomputer etc]# ls
adjtime                     dracut.conf.d    krb5.conf.d               papersize               sgml
aliases                     egl              ksmtuned.conf             passwd                  shadow
alsa                        enscript.cfg     ld.so.cache               passwd-                 shadow-
alternatives                environment      ld.so.conf                pbm2ppa.conf            shells
參數 做用
-a 看到所有文件
-l 查看文件的屬性

四、tree

用於以樹狀圖的形式列出目錄內容及結構

[root@mingmingxingforcomputer ~]# tree
.
├── anaconda-ks.cfg
├── a.txt
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── linuxprobe
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

五、find

按照指定條件來查找文件所對應的位置

[root@mingmingxingforcomputer ~]# find / -name passwd
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/pam.d/passwd
/etc/passwd
/var/lib/sss/mc/passwd
/usr/bin/passwd
/usr/share/licenses/passwd
/usr/share/doc/passwd
/usr/share/bash-completion/completions/passwd

進階實驗:在整個文件系統中找出全部歸屬於linuxprobe用戶的文件並複製到/root/findresults目錄。

該實驗的重點是「-exec {} \;」參數,其中的{}表示find命令搜索出的每個文件,而且命令的結尾必須是「\;」。完成該實驗的具體命令以下:

[root@linuxprobe ~]# find / -user linuxprobe -exec cp -a {} /root/findresults/ \;

六、locate

按照名稱快速搜索文件所對應的位置

使用步驟:

  1. 生成數據庫updatedb
  2. locate 文件名稱
[root@mingmingxingforcomputer ~]# updatedb 
[root@mingmingxingforcomputer ~]# locate whereis
/usr/bin/whereis
/usr/share/bash-completion/completions/whereis
/usr/share/man/man1/whereis.1.gz
[root@mingmingxingforcomputer ~]# find / -name whereis
/usr/bin/whereis
/usr/share/bash-completion/completions/whereis
[root@mingmingxingforcomputer ~]#

七、whereis

用於按照名稱快速搜索二進制程序(命令)、源代碼以及幫助手冊文件所對應的位置

[root@mingmingxingforcomputer ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz

簡單來講,whereis命令也是基於了上述updatedb命令所生成出的索引庫文件進行搜索,區別是不關心那些相同名稱的文件,僅僅是快速找到對應的命令文件及其幫助文件所在位置。

八、which

which命令用於按照指定名稱快速搜索二進制程序(命令)所對應的位置

[root@mingmingxingforcomputer ~]# which whereis
/usr/bin/whereis
查詢命令 查詢速度 查詢準確度
which 1 4
whereis 2 3
locate 3 2
find 4 1

三、文本編輯命令

一、cat

用於查看純文本文件(內容較少的)

[root@mingmingxingforcomputer ~]# cat -n initial-setup-ks.cfg 
     1  #version=RHEL8
     2  # X Window System configuration information
     3  xconfig  --startxonboot
     4  # License agreement
     5  eula --agreed
     6  # Use graphical install
     7  graphical
     8  # Network information
     9  network  --bootproto=dhcp --device=ens160 --onboot=off --ipv6=auto --no-activate
    10  network  --bootproto=dhcp --hostname=localhost.localdomain
    11  repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
    12  ignoredisk --only-use=sda

二、more

用於查看純文本文件(內容較多的)

[root@mingmingxingforcomputer ~]# more -n initial-setup-ks.cfg 
more: unknown option -n
Try 'more --help' for more information.
[root@mingmingxingforcomputer ~]# more initial-setup-ks.cfg 
#version=RHEL8

# X Window System configuration information

xconfig  --startxonboot

# License agreement

eula --agreed

# Use graphical install

graphical

# Network information

network  --bootproto=dhcp --device=ens160 --onboot=off --ipv6=auto --no-activate
network  --bootproto=dhcp --hostname=localhost.localdomain
repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
ignoredisk --only-use=sda

# Use CDROM installation media

cdrom

# Run the Setup Agent on first boot

firstboot --enable

# System services

services --disabled="chronyd"

# Keyboard layouts

keyboard --vckeymap=us --xlayouts='us'

# System language

lang en_US.UTF-8

--More--(41%)

三、head

用於查看純文本文件的前N行

[root@mingmingxingforcomputer ~]# head -n 5 initial-setup-ks.cfg 
#version=RHEL8
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed

四、tail

用於查看純文本文件的後N行或持續刷新文件的最新內容

[root@mingmingxingforcomputer ~]# tail -n 5 initial-setup-ks.cfg 
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

tail持續刷新命令:tail -f /var/log/messages

[root@mingmingxingforcomputer ~]# tail -f /var/log/messages
Jun 26 19:55:22 mingmingxingforcomputer systemd[1]: Starting PackageKit Daemon...
Jun 26 19:55:22 mingmingxingforcomputer dbus-daemon[1020]: [system] Successfully activated service 'org.freedesktop.PackageKit'
Jun 26 19:55:22 mingmingxingforcomputer systemd[1]: Started PackageKit Daemon.
Jun 26 20:08:30 mingmingxingforcomputer systemd[1]: Starting dnf makecache...
Jun 26 20:08:30 mingmingxingforcomputer dnf[13946]: Updating Subscription Management repositories.
Jun 26 20:08:30 mingmingxingforcomputer dnf[13946]: Unable to read consumer identity
Jun 26 20:08:30 mingmingxingforcomputer dnf[13946]: This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Jun 26 20:08:30 mingmingxingforcomputer dnf[13946]: Failed determining last makecache time.
Jun 26 20:08:30 mingmingxingforcomputer dnf[13946]: There are no enabled repos.
Jun 26 20:08:30 mingmingxingforcomputer systemd[1]: Started dnf makecache.
……

五、tr

替換文本內容中的字符

語法格式爲:「tr [原始字符] [目標字符]」

[root@mingmingxingforcomputer ~]# tail -n 5 initial-setup-ks.cfg | tr [a-z] [A-Z]
%ANACONDA
PWPOLICY ROOT --MINLEN=6 --MINQUALITY=1 --NOTSTRICT --NOCHANGES --NOTEMPTY
PWPOLICY USER --MINLEN=6 --MINQUALITY=1 --NOTSTRICT --NOCHANGES --EMPTYOK
PWPOLICY LUKS --MINLEN=6 --MINQUALITY=1 --NOTSTRICT --NOCHANGES --NOTEMPTY
%END

六、wc

用於統計指定文本文件的行數、字數或字節數

語法格式爲:「wc [參數] 文件名稱」

[root@mingmingxingforcomputer ~]# wc -clw initial-setup-ks.cfg 
  50  137 1559 initial-setup-ks.cfg

七、stat

用於查看文件的具體存儲細節和時間等信息

語法格式爲:「stat 文件名稱」

[root@mingmingxingforcomputer ~]# stat initial-setup-ks.cfg 
  File: initial-setup-ks.cfg
  Size: 1559        Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 35268271    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2021-06-26 19:46:18.231530396 +0800
Modify: 2021-01-04 03:40:00.818112167 +0800
Change: 2021-01-04 03:40:00.818112167 +0800
 Birth: -
狀態 描述
Access Time 最後一次內容被訪問的時間
Modify Time 最後一次內容被修改的時間
Change Time 文件屬性最後一次被修改的時間

八、grep

grep命令用於按行提取文本內容

grep [參數] 文件名稱

[root@mingmingxingforcomputer ~]# cat initial-setup-ks.cfg | grep oo
xconfig  --startxonboot
network  --bootproto=dhcp --device=ens160 --onboot=off --ipv6=auto --no-activate
network  --bootproto=dhcp --hostname=localhost.localdomain
# Run the Setup Agent on first boot
firstboot --enable
# Root password
rootpw --iscrypted $6$989xgW64eEOskUcc$Oo1jfPK4FIVjw22Fvg5iQxx6mzQ7dw8hqdxjDDbq7LkZYWwB6LGYJfZHZNk6tYnE2HtiGiGhx22L.QK4uqLGe0
# System bootloader configuration
bootloader --location=none
kexec-tools
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty

九、cut

cut命令用於按列提取文本內容

cut [參數] 文件名稱

[root@mingmingxingforcomputer ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator

十、diff

diff命令用於比較多個文件之間內容的差別

diff [參數] 文件名稱A 文件名稱B

[root@mingmingxingforcomputer ~]# cat a.txt 
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@mingmingxingforcomputer ~]# cat b.txt 
Welcome tooo linuxprobe.com

Red Hat certified
Free Linux LeSSonS
////////.....////////
Professional guidance
Linux Course
[root@mingmingxingforcomputer ~]# diff --brief a.txt b.txt 
Files a.txt and b.txt differ
[root@mingmingxingforcomputer ~]# diff -c a.txt b.txt 
*** a.txt   2021-06-26 19:54:36.505573424 +0800
--- b.txt   2021-06-26 19:54:59.510575410 +0800
***************
*** 1,5 ****
! Welcome to linuxprobe.com
  Red Hat certified
! Free Linux Lessons
  Professional guidance
  Linux Course
--- 1,7 ----
! Welcome tooo linuxprobe.com
! 
  Red Hat certified
! Free Linux LeSSonS
! ////////.....////////
  Professional guidance
  Linux Course

十一、uniq

去除查看文件中重複行

uniq [參數] 文件名稱

[root@mingmingxingforcomputer ~]# cat c.txt 
aaaaaa
aaaaaa
aaaaaa
1
2
3
4
[root@mingmingxingforcomputer ~]# uniq c.txt 
aaaaaa
1
2
3
4

十二、sort

用於對文本內容進行再排序

sort [參數] 文件名稱

[root@mingmingxingforcomputer ~]# sort -n d.txt 
0
12
15
45
78
231
321
[root@mingmingxingforcomputer ~]# cat d.txt 
15
231
12
0
45
78
321

本節課學習命令量很大,須要多練習兩遍……不說了,練習去了

相關文章
相關標籤/搜索