本章節,是要講述如何查看網路設定與設定在主機上的網路資訊。
在一台主機上面,我們可以使用ip
這個指令來查看網路卡,IP位址之設定等相關的資訊,相關指令執行如下:
[rockylinux@workstation ~]$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:ce:5e:d6 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
link/ether 52:54:00:94:4c:d5 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
link/ether 52:54:00:94:4c:d5 brd ff:ff:ff:ff:ff:ff
[rockylinux@workstation ~]$
從上從上述的指令執行後的輸出結果可以知道,搭配link
參數則是顯示網路介面卡的名稱與資訊,像是MAC位址等,show
就是顯示的意思,若要顯示每一個網路介面卡資訊上面所設定的IP位址的話,則將link
換成addr
即可,若要指定網路介面,則是在show
後面再加上網路介面卡的名稱即可,相關執行指令的方式與輸出的訊息如下:
[rockylinux@workstation ~]$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ce:5e:d6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.21/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6293sec preferred_lft 6293sec
inet6 fe80::a00:27ff:fece:5ed6/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:94:4c:d5 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:94:4c:d5 brd ff:ff:ff:ff:ff:ff
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ ip addr show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ce:5e:d6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.21/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6273sec preferred_lft 6273sec
inet6 fe80::a00:27ff:fece:5ed6/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[rockylinux@workstation ~]$
從上述的指令輸出訊息可知道,首先先用ip addr show
將所有在這台主機上所設定的網路介面卡之有關於IP位址資訊都列出,接著若要只單獨印出enp0s3
之網路界網路介面卡,則執行ip addr show enp0s3
指令。
若要顯示某個網路介面卡之網路流量,則可以加上-s
參數,則執行之後,會顯示有多少bytes位元封包接收到,有多少bytes位元封包傳送出去,以及封包錯誤以及封包有多少被丟棄。相關的執行指令輸出的訊息如下:
[rockylinux@workstation ~]$ ip -s link show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:ce:5e:d6 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
80167 822 0 0 0 136
TX: bytes packets errors dropped carrier collsns
78114 506 0 0 0 0
[rockylinux@workstation ~]$
從上述的從上述的訊息來看,指的就是查看網路介面卡為enp0s3之名稱的網路流量分析資訊。
我們可以使用ping
這個指令來檢查主機之間的連線狀態,其原理為:使用ICMP並用於網際網路協定(IP)中傳送控制訊息,提供可能發生在通訊環境中的各種問題回饋。並且透過這些資訊,讓網路或是系統管理者可以對所發生的問題作出排錯,接著採取適當的方式解決,相關的指令之執行方式如下:
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=5.18 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.90 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.63 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=12.6 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=5.20 ms
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4062ms
rtt min/avg/max/mdev = 5.176/7.504/12.619/2.855 ms
[rockylinux@workstation ~]$
從上述的指令執行輸出的訊息來看,使用ping
指令去與8.8.8.8
之IP位址進行連線狀態間的測試,代表此台主機與此IP位址主機可以連線,而8.8.8.8
是Google之其中一個DNS服務,但是只用ping
指令的話,整個測試是不回中斷的,會一直無限循環的下去直到使用「ctrl」加上「c」進行訊號傳遞並讓指令中斷才有可能讓此ping
指令的動作停止,為了要限制使用ping
指令給指定主機IP位址的次數,可以加上-c
參數並加上要執行ping
多少次,假設要執行三次,則上述的指令可以改成如下:
[rockylinux@workstation ~]$ ping -c3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=11.1 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=6.14 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=7.12 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2024ms
rtt min/avg/max/mdev = 6.142/8.113/11.075/2.134 ms
[rockylinux@workstation ~]$
其中,-c
與指定的次數之間可以沒有空白沒有關係,當然這兩者之間有空白也是可以,相關執行指令的輸出如下:
[rockylinux@workstation ~]$ ping -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=5.33 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=14.1 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=6.81 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 5.328/8.750/14.110/3.838 ms
[rockylinux@workstation ~]$
以上是進行IPv4位址的ping測試,若要進行IPv6測試的話,則可以使用ping6
這個指令,相關的用法和ping
相同,執行之命令與輸出的訊息如下:
[rockylinux@workstation ~]$ ping6 -c 3 ::1
PING ::1(::1) 56 data bytes
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.073 ms
64 bytes from ::1: icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from ::1: icmp_seq=3 ttl=64 time=0.090 ms
--- ::1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2051ms
rtt min/avg/max/mdev = 0.073/0.083/0.090/0.007 ms
[rockylinux@workstation ~]$
從上述的執行指令,這指的是使用ping6
指令去對本地端IPv6位址進行測試3次並輸出分析的網路結果。
為了路由表故障排除,可以使用ip
指令並搭配route
參數進行使用,相關的執行指令所輸出的訊息如下:
[rockylinux@workstation ~]$ ip route
default via 192.168.0.1 dev enp0s3 proto dhcp metric 100
192.168.0.0/24 dev enp0s3 proto kernel scope link src 192.168.0.21 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
[rockylinux@workstation ~]$
從上述的指令可以得知,從此IP位址之路由資訊,因為筆者的虛擬機器是在一台網路路由器後面,因此在路由預設會到路由器的IP位址上,即gateway IP;並透過閘道器IP傳送到廣域網路並與外部網路進行連線的互動,以上檢查路由的資訊,這時候只需要加上-6
之參數在ip
指令後面即可,相關的指令如下:
[rockylinux@workstation ~]$ ip -6 route
::1 dev lo proto kernel metric 256 pref medium
fe80::/64 dev enp0s3 proto kernel metric 100 pref medium
[rockylinux@workstation ~]$
若要追蹤請求網路流量,或是檢查從本地端主機封包傳送到外部要重設,這時後可以使用tracepath
指令來做到,後面接著是網址,trace指令預設會先使用UDP指令進行封包傳遞,相關指令執行輸出之訊息如下:
[rockylinux@workstation ~]$ tracepath 8.8.8.8
1?: [LOCALHOST] pmtu 1500
1: _gateway 8.420ms asymm 35
1: _gateway 4.217ms asymm 35
2: _gateway 4.240ms pmtu 1492
2: no reply
3: tpe4-3301.hinet.net 19.111ms
4: no reply
5: pcpd-3211.hinet.net 28.178ms
6: no reply
7: no reply
8: no reply
9: no reply
10: no reply
11: no reply
12: no reply
13: no reply
14: no reply
15: no reply
16: no reply
17: no reply
18: no reply
19: no reply
20: no reply
21: no reply
22: no reply
23: no reply
24: no reply
25: no reply
26: no reply
27: no reply
28: no reply
29: no reply
30: no reply
Too many hops: pmtu 1492
Resume: pmtu 1492
透過上述的tracepath
指令可以知道,這個指令可以用來追蹤從本地主機到8.8.8.8之過程,有no reply
部分則是可能對方不支援回應或是被防火牆擋住了,但不影響連線測試的結果,我們也可以使用traceroute
指令來追蹤一個指定的IP位址或是網域的名稱,在RockyLinux上若沒有安裝的話,則可以使用下列的指令進行安裝:
[rockylinux@workstation ~]$ sudo yum install -y traceroute
Last metadata expiration check: 21:30:42 ago on Fri 08 Oct 2021 12:09:38 AM CST.
Dependencies resolved.
============================================================================================================
Package Architecture Version Repository Size
============================================================================================================
Installing:
traceroute x86_64 3:2.1.0-6.el8 baseos 66 k
Transaction Summary
============================================================================================================
Install 1 Package
Total download size: 66 k
Installed size: 101 k
Downloading Packages:
traceroute-2.1.0-6.el8.x86_64.rpm 957 kB/s | 66 kB 00:00
------------------------------------------------------------------------------------------------------------
Total 83 kB/s | 66 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : traceroute-3:2.1.0-6.el8.x86_64 1/1
Running scriptlet: traceroute-3:2.1.0-6.el8.x86_64 1/1
Verifying : traceroute-3:2.1.0-6.el8.x86_64 1/1
Installed products updated.
Installed:
traceroute-3:2.1.0-6.el8.x86_64
Complete!
[rockylinux@workstation ~]$
安裝完成之後,接著就可以使用下列的指令進行追蹤指定的網域或是位址:
[rockylinux@workstation ~]$ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 _gateway (192.168.0.1) 11.917 ms 11.805 ms 11.766 ms
2 * * *
3 tpe4-3301.hinet.net (168.95.22.110) 13.358 ms 29.627 ms 32.212 ms
4 * * *
5 pcpd-3211.hinet.net (220.128.12.241) 34.033 ms pcpd-3211.hinet.net (220.128.12.189) 36.336 ms pcpd-3211.hinet.net (220.128.12.241) 39.666 ms
6 * * *
7 * * *
8 dns.google (8.8.8.8) 17.705 ms 20.760 ms 25.441 ms
[rockylinux@workstation ~]$
從上述指令輸出的訊息可以知道,此台虛擬主機到8.8.8.8之IP位址之間經過了多少的路由位址,若是遇到有路由顯示:* * *
,則表示這裡的路由位址被擋住或是沒有回應,但是不影響此台虛擬主機到8.8.8.8位址,下列是利用網域名稱的方式進行連線間的追蹤路由:
[rockylinux@workstation ~]$ traceroute ithome.com.tw
traceroute to ithome.com.tw (220.130.119.130), 30 hops max, 60 byte packets
1 _gateway (192.168.0.1) 6.835 ms 8.883 ms 8.852 ms
2 * * *
3 tpe4-3301.hinet.net (168.95.84.6) 20.129 ms 23.730 ms 22.462 ms
4 * * *
5 tpdb-3315.hinet.net (220.128.1.5) 35.493 ms tpdb-3316.hinet.net (220.128.1.213) 32.540 ms tpdb-3316.hinet.net (220.128.1.117) 38.239 ms
6 tpdt-3307 (168.95.210.57) 38.211 ms h13.s229.ts.hinet.net (168.95.229.13) 15.599 ms h13.s228.ts.hinet.net (168.95.228.13) 25.093 ms
7 * * *
8 203.69.80.89 (203.69.80.89) 24.895 ms 18.930 ms 16.051 ms
9 211.72.249.229 (211.72.249.229) 16.394 ms 211.72.249.225 (211.72.249.225) 12.984 ms 14.729 ms
10 * * *
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
[rockylinux@workstation ~]$
8.8.4.4
之IP位址之間所經過的路由位址,並一一的列出來。