本次博客介紹了linux操作系統中常用的網路管理工具和相關配置文件。 ...
當我們擁有一個嶄新的電腦的時候,第一步恐怕都是迫不及待的下載各種軟體,看視頻,聽音樂等,這裡的關鍵的一點是要有網路。現在的個人電腦大部分都是windows操作系統的,接入網路網路很簡單,插上網線也許就能上網了,最麻煩不過找到網路和共用中心手動配置一個ip地址、網關地址和dns伺服器就能上網了,當然這都是得益於圖形化界面。但是現在很多的伺服器都是linux操作系統且沒有圖形化的界面,這時候我們就需要瞭解怎樣正確的給linux主機配置網路屬性了,本次博客就來說說以正確的方式打開linux操作系統的網路屬性配置(本次博客基於的發行版和版本:centOS7、centOS6.5)。
接入網路的兩種方式
一、手動的指定
1.使用命令
一般是臨時的修改,重啟後失效,如:ifconfig、route、ip addr等。
2.修改配置文件
修改文件配置,永久有效,但是可能不能立即生效,需要重啟服務。
二、動態獲取
如果本地網路中存在DHCP服務,可以動態的獲取到ip地址、網關地址、DNS伺服器地址等。
常用的管理和配置命令
一、ifconfig
1.不加任何選項,查看處於活動狀態下的介面,-a選項顯示所有介面,包括inactive狀態的介面:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2168 errors:0 dropped:0 overruns:0 frame:0 TX packets:404 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:149715 (146.2 KiB) TX bytes:29251 (28.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@localhost ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2223 errors:0 dropped:0 overruns:0 frame:0 TX packets:425 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:153927 (150.3 KiB) TX bytes:32473 (31.7 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)View Code
介面的命名方式:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1.傳統的命令方式: 乙太網:eth1,eth2...ethn #centos6較常見的命名方式 PPP網路:ppp1,ppp2...pppn 2.可預測的命令方案 如果固件或者BIOS為主板上集成的設備提供的索引信息可以用,根據此索引進行命令,例如:eno1,eno2....enon; 如果固件或者BIOS為PCI-E擴展槽上所提供的索引信息可以用,根據此索引進行命令,例如:ens1,ens2...ensn; 如果硬體介面的物理位置信息可用,則根據此信息命令,如enp2s0 如果用戶顯示定義,也可以根據MAC地址命令,例如enx1232131231 3.命名格式的組成 en:乙太網 wl:無線網 ww:廣域網 4.名稱類型 o<index>:集成設備的設備索引號 s<slot>:擴展槽的索引號 x<MAC>:基於MAC地址的命名 p<bus>s<slot>:基於匯流排槽的拓撲結構進行命名介面命名方式
2. 配置介面ip地址掩碼的兩種方式:
ifconfig INTERFACE IP/MASK [up|down] #up和down為可選項 ifconfig INTERFACE IP netmask NETMASK [up|down]
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ifconfig eth1 192.168.1.1/24 up [root@localhost ~]# [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2392 errors:0 dropped:0 overruns:0 frame:0 TX packets:510 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:168403 (164.4 KiB) TX bytes:43303 (42.2 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:5 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:398 (398.0 b) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)View Code
3.開啟介面的雜合模式,這裡就不先做太多的說明,後面博客中會說明:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ifconfig eth1 promisc [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 #這裡多了一個PROMISC RX packets:29 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2985 (2.9 KiB) TX bytes:468 (468.0 b)View Code
4.新增和刪除IPv6地址
ifconfig INTERFACE add addr/prefixlen ifconfig INTERFACE del addr/prefixlen
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ifconfig eth1 add 2001:0:0:12::1/64 #新增一個IPV6地址 [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: 2001:0:0:12::1/64 Scope:Global inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:57 errors:0 dropped:0 overruns:0 frame:0 TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:6604 (6.4 KiB) TX bytes:836 (836.0 b) [root@localhost ~]# ifconfig eth1 del 2001:0:0:12::1/64 #刪除ipv6地址 [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:62 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7468 (7.2 KiB) TX bytes:1016 (1016.0 b)View Code
如果在沒有配置ipv6地址的情況下,我們會發現每個介面都有一個開頭為fe80的ipv6地址,這個地址被稱為鏈路本地的地址,其前64為都是固定的格式,後64是位是基於EUI-64生成的,通俗的來說就是基於MAC地址生成的,鏈路本地地址只能用戶本地的通信,如果想要實現上網的功能,就必須要給介面配置一個全局的地址,也就是全球可聚合單播地址,配置的方式就如上,顯示為Global。
可以給介面起別名配置多個IP地址:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ifconfig ens34:1 192.168.179.130 netmask 255.255.255.0 [root@localhost ~]# [root@localhost ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.172.134 netmask 255.255.255.0 broadcast 192.168.172.255 inet6 fe80::4:ed31:efad:25d0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:db:7f:47 txqueuelen 1000 (Ethernet) RX packets 25157 bytes 12826364 (12.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11651 bytes 2175604 (2.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.179.128 netmask 255.255.255.0 broadcast 192.168.179.255 ether 00:0c:29:db:7f:51 txqueuelen 1000 (Ethernet) RX packets 666 bytes 91944 (89.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 220 bytes 29356 (28.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens34:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.179.130 netmask 255.255.255.0 broadcast 192.168.179.255 ether 00:0c:29:db:7f:51 txqueuelen 1000 (Ethernet)View Code
然後為別名添加配置文件即可,記得DEVICE為別名名稱且BOOTPROTO:不支持動態獲取地址。
註意:執行ifconfig的時候,命令執行後立即生效的,但是重啟後失效!
二、route
1.查看路由條目
route -n #用數字表示,不加-n選項,可能會以主機名和其他的符號來顯示地址
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.172.0 * 255.255.255.0 U 0 0 0 eth0 link-local * 255.255.0.0 U 1002 0 0 eth0 default 192.168.172.2 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.172.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0View Code
2.添加路由條目
route add [-net|-host] target [netmask Nm] [gw GW] [[dev] If]
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# route add -net 100.100.100.0/24 gw 192.168.1.1 #介面可省略 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 100.100.100.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# route add default gw 192.168.1.1 dev eth1 #使用default參數創建預設路由 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 100.100.100.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0View Code
3.刪除路由
route del [-net|-host] target [gw GW] [netmask Nm] [[dev] If]
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# route del -net 100.100.100.0/24 [root@localhost ~]# route del -net 0.0.0.0/0 gw 192.168.1.1 #如果目的IP地址存在多個,可以指定網關地址,避免誤刪 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0View Code
三、netstat命令
專門用來顯示命令
1.顯示路由表
netstat -rn -r:顯示內核路由表 -n:數字格式
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0View Code
2.顯示網路連接
netstat [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]] [--program|-p] -t:TCP的相關連接 -u:UDP相關連接 -w:raw socket相關連接 -l:處於監聽狀態的連接 -a:所有狀態 -n:以數字格式顯示ip和埠 -e:擴展格式 -p:顯示相關進程及PID 常用組合: -tan -uan -tnl -unl -tunlp
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 64 192.168.172.136:22 192.168.172.1:60645 ESTABLISHED tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# netstat -uan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:68 0.0.0.0:* [root@localhost ~]# netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# netstat -unl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:68 0.0.0.0:* [root@localhost ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1008/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1084/master tcp 0 0 :::22 :::* LISTEN 1008/sshd tcp 0 0 ::1:25 :::* LISTEN 1084/master udp 0 0 0.0.0.0:68 0.0.0.0:* 904/dhclientView Code
3.顯示介面的統計數據
netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--verbose|-v] [--program|-p] [--numeric|-n] 所有介面的信息:netstat -i 指定的介面:netstat -I<INTERFACE>
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 2288 0 0 0 1099 0 0 0 BMRU eth1 1500 0 8 0 0 0 6 0 0 0 BMRU lo 16436 0 0 0 0 0 0 0 0 0 LRU [root@localhost ~]# netstat -Ieth1 Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth1 1500 0 8 0 0 0 6 0 0 0 BMRUView Code
四、ifup和ifdown
開啟或者關閉介面,通過配置文件來識別介面並完成配置,如果介面沒有配置文件,則不能生效。
ifup INTERFACE
ifdown INTERFACE
五、ip
ip的功能很強大,這裡只做一些常用的用法的介紹,具體可以見man手冊。
NAME:show / manipulate routing, devices, policy routing and tunnels ip [ OPTIONS ] OBJECT { COMMAND | help } OBJECT := { link | addr | route | netns } #註意,在寫命令的時候,OBJECT可以簡寫,各個OBJECT的子命令也可以簡寫
1. ip link:網路設備管理
ip link set:修改設備的屬性 dev NAME:預設參數,指明要管理的設備,關鍵字dev可以省略; {up|down}:開啟或關閉設備 {multicast on|multicast off}:開啟或關閉多播功能 {name NAME}:重命名介面,必須先將設備關閉 netns PID|netnsname:netns為名稱空間,將介面移動到指定的網路名稱空間 ip link show:查看設備的狀態 ip link help:顯示幫助信息
舉例:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# ip link set ens34 down #關閉ens34 [root@localhost ~]# ip link set ens34 name eth34 #重命名 [root@localhost ~]# [root@localhost ~]# ifconfig -a ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.172.134 n