時(shí)間:2023-06-23|瀏覽:246
今天是第五篇,介紹Linux下基礎(chǔ)的網(wǎng)絡(luò)操作命令。
網(wǎng)卡信息
```
ubuntu@ubuntu-virtual-machine:~$ ifconfig
ens33: flags=4163 網(wǎng)絡(luò)配置
主要列舉幾個(gè)我們?cè)诠ぷ髦锌赡軙?huì)用到的操作:
關(guān)閉指定網(wǎng)卡
```
#ifconfig eth0 down #關(guān)閉eth0
``` 開(kāi)啟指定網(wǎng)卡
```
#ifconfig eth0 up #開(kāi)啟eth0
``` 修改指定網(wǎng)卡信息
```
#ifconfig eth0 down #關(guān)閉網(wǎng)卡
#ifconfig eth0 hw ether 00:11:22:33:44:55 #修改eth0網(wǎng)卡MAC地址
#ifconfig eth0 up #啟動(dòng)網(wǎng)卡
```
```
#ifconfig eth0 192.168.3.178 #給eth0網(wǎng)卡配置IP地址
#ifconfig eth0 192.168.3.178 netmask 255.255.255.0 #給eth0網(wǎng)卡配置IP地址,并加上子掩碼
#ifconfig eth0 192.168.3.178 netmask 255.255.255.0 broadcast 192.168.3.255 #給eth0網(wǎng)卡配置IP地址,加上子掩碼,加上個(gè)廣播地址
``` 啟動(dòng)/關(guān)閉ARP協(xié)議
如果對(duì)網(wǎng)絡(luò)不熟悉可能不了解什么是ARP,但是這個(gè)協(xié)議在開(kāi)發(fā)中是經(jīng)常被用到的,可以先了解一下。
```
#ifconfig eth0 arp #開(kāi)啟eth0網(wǎng)卡ARP
#ifconfig eth0 -arp #關(guān)閉eth0網(wǎng)卡ARP
``` 設(shè)置MTU
MTU是包或幀的最大長(zhǎng)度,一般單位是字節(jié)。如果MTU過(guò)大,在碰到路由器時(shí)會(huì)被拒絕轉(zhuǎn)發(fā),因?yàn)樗荒芴幚磉^(guò)大的包。如果太小,因?yàn)閰f(xié)議一定要在包(或幀)上加上包頭,那實(shí)際傳送的數(shù)據(jù)量就會(huì)過(guò)小,這樣也劃不來(lái)。大部分操作系統(tǒng)會(huì)提供給用戶一個(gè)默認(rèn)值,該值一般對(duì)用戶來(lái)說(shuō)是比較合適的。
```
#ifconfig eth0 mtu 1500 #設(shè)置MTU1500字節(jié)
``` ping命令
ping命令一般用來(lái)測(cè)試網(wǎng)絡(luò)連通狀態(tài),但是一定要注意,并不是說(shuō)沒(méi)有ping通就表示網(wǎng)絡(luò)不通,因?yàn)榫W(wǎng)絡(luò)可以配置ping的功能,但是大多數(shù)情況下都有這個(gè)服務(wù)。 使用非常簡(jiǎn)單,ping命令緊接著一個(gè)IP或者域名地址,需要手動(dòng)輸入CTRL+C結(jié)束。
```
ubuntu@ubuntu-virtual-machine:~$ ping 192.168.3.178
PING 192.168.3.178 (192.168.3.178) 56(84) bytes of data.
64 bytes from 192.168.3.178: icmp_seq=1 ttl=128 time=4.90 ms
64 bytes from 192.168.3.178: icmp_seq=2 ttl=128 time=5.08 ms
64 bytes from 192.168.3.178: icmp_seq=3 ttl=128 time=5.26 ms
^C
--- 192.168.3.178 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 4.904/5.081/5.260/0.145 ms ubuntu@ubuntu-virtual-machine:~$ ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=34.2 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=33.5 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=33.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=128 time=36.1 ms
^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 33.524/34.393/36.068/0.992 ms
``` 介紹幾個(gè)常用的參數(shù):
-c 指定ping的包數(shù)。
```
ubuntu@ubuntu-virtual-machine:~$ ping -c 5 www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=35.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=36.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=128