本文主要是介绍Linux笔记03,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
***> 本次主要还是弄一些基础命令……***
### 1、系统状态检测命令
#### 1、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
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
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 | 物理地址 |
#### 2、uname
> *查看系统信息*
```linux
[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
```
#### 3、uptime
> *查看系统的负载信息*
```
[root@mingmingxingforcomputer ~]# uptime
15:03:53 up 9 min, 1 user, load average: 0.00, 0.11, 0.12
```
> 负载值越低越好指的是对运维人员来讲的,越低越安全省心。但是公司购置的硬件设备如果长期处于空闲状态,明显是种资源浪费,老板也不会开心。所以我们建议是负载值保持在1左右,不要超过5就好。
#### 4、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
```
#### 5、who
> *****查看当前登入主机的用户终端信息*****
```
[root@mingmingxingforcomputer ~]# who
root pts/0 2021-06-26 14:55 (192.168.0.9)
```
#### 6、last
> *用于调取主机的被访记录*
```
[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
```
#### 7、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 | 最长等待时间(秒) |
#### **8、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
```
#### **9、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
```
#### **10、history**
> *显示执行过的命令历史*
```
[root@mingmingxingforcomputer ~]# history | head -n 5
1 history
2 sosreport
3 cd /home/
4 pwd
5 cd /root
```
#### **11、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.
```
### 2、定位文件命令
#### **1、pwd**
> *显示用户当前所处的工作目录*
```
[root@mingmingxingforcomputer ~]# cd ..
[root@mingmingxingforcomputer /]# pwd
/
[root@mingmingxingforcomputer /]# cd /etc
[root@mingmingxingforcomputer etc]# pwd
/etc
```
#### **2、cd**
> *切换当前的工作路径*
```
[root@mingmingxingforcomputer ~]# cd ..
[root@mingmingxingforcomputer /]# pwd
/
```
#### **3、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 | 查看文件的属性 |
#### **4、tree**
> *用于以树状图的形式列出目录内容及结构*
```
[root@mingmingxingforcomputer ~]# tree
.
├── anaconda-ks.cfg
├── a.txt
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── linuxprobe
├── Music
├── Pictures
├── Public
├── Templates
└── Videos
```
#### **5、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/ \;
#### **6、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 ~]#
```
#### **7、whereis**
> 用于按照名称快速搜索二进制程序(命令)、源代码以及帮助手册文件所对应的位置
```
[root@mingmingxingforcomputer ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
```
> 简单来说,whereis命令也是基于了上述***updatedb***命令所生成出的索引库文件进行搜索,区别是不关心那些相同名称的文件,仅仅是快速找到对应的命令文件及其帮助文件所在位置。
#### **8、which**
> *which命令用于按照指定名称快速搜索二进制程序(命令)所对应的位置*
```
[root@mingmingxingforcomputer ~]# which whereis
/usr/bin/whereis
```
| *查询命令* | *查询速度* | 查询准确度 |
| ---------- | ---------- | ---------- |
| *which* | *1* | 4 |
| *whereis* | *2* | 3 |
| *locate* | *3* | 2 |
| *find* | *4* | 1 |
### 3、文本编辑命令
#### **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
```
#### **2、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%)
```
#### **3、head**
> *用于查看纯文本文件的前N行*
```
[root@mingmingxingforcomputer ~]# head -n 5 initial-setup-ks.cfg
#version=RHEL8
# X Window System configuration information
xconfig --startxonboot
# License agreement
eula --agreed
```
#### **4、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.
……
```
#### **5、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
```
#### **6、wc**
> ***用于统计指定文本文件的行数、字数或字节数***
>
> **语法格式为:“wc [参数] 文件名称”**
```
[root@mingmingxingforcomputer ~]# wc -clw initial-setup-ks.cfg
50 137 1559 initial-setup-ks.cfg
```
#### **7、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 | 文件属性最后一次被修改的时间 |
#### **8、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
```
#### **9、cut**
> ***cut命令用于按列提取文本内容***
>
> **cut [参数] 文件名称**
```
[root@mingmingxingforcomputer ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
```
#### **10、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
```
#### **11、uniq**
> ***去除查看文件中重复行***
>
> **uniq [参数] 文件名称**
```
[root@mingmingxingforcomputer ~]# cat c.txt
aaaaaa
aaaaaa
aaaaaa
1
2
3
4
[root@mingmingxingforcomputer ~]# uniq c.txt
aaaaaa
1
2
3
4
```
#### **12、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
```
### 总结
**第三课学习命令量很大,需要多练习两遍……不说了,练习去了**
这篇关于Linux笔记03的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!