一、ifcongfig
Centos6之前最常用的配置網絡命令就是ifconfig,使用ifconfig命令時最好切換到root用戶的身份
1、直接使用ifconfig可以查看當前配置的網絡設備的信息
例如
[root@localhost ~]# ifconfig eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.9.9 netmask 255.255.0.0 broadcast 172.16.255.255 inet6 fe80::20c:29ff:fe83:15cb prefixlen 64 scopeid 0x20<link> ether 00:0c:29:83:15:cb txqueuelen 1000 (Ethernet) RX packets 120796 bytes 20258545 (19.3 MiB) RX errors 0 dropped 1418 overruns 0 frame 0 TX packets 28466 bytes 47363344 (45.1 MiB) 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 0 (Local Loopback) RX packets 20 bytes 1780 (1.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 20 bytes 1780 (1.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
這里可以看到我目前有一個網絡設備eno16777728和一個網絡回環接口lo,常見的網絡接口有以下幾個
eth0: ethernet的簡寫,一般用于以太網接口。在centos7上是eno16777728
wifi0:wifi是無線局域網,因此wifi0一般指無線網絡接口。
ath0: Atheros的簡寫,一般指Atheros芯片所包含的無線網絡接口。
2、用ifconfig啟動或關閉指定網卡
ifconfig up 啟動指定的網絡設備
ifconfig down 關閉指定的網絡設備
網卡名稱一定要寫在down或up之前
[root@localhost ~]# ifconfig eno16777728 down [root@localhost ~]# ifconfig eno16777728 up
3、用ifconfig配置網絡地址和網關
比如這里我配置eth0網卡的ip地址為:172.16.7.254子網掩碼為255.255.0.0
注意用ifconfig改網絡配置會立即生效
[root@localhost ~]# ifconfig eth0 172.16.7.254 netmask 255.255.0.0 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:4B:1F inet addr:172.16.7.254 Bcast:172.16.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe5e:4b1f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1372221 errors:2 dropped:0 overruns:0 frame:0 TX packets:3465 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:119202924 (113.6 MiB) TX bytes:334330 (326.4 KiB) Interrupt:19 Base address:0x2000
還可以給一個網卡配上多個地址,例如
[root@localhost ~]# ifconfig eth0:0 172.16.7.225 netmask 255.255.0.0 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:4B:1F inet addr:172.16.7.254 Bcast:172.16.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe5e:4b1f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1375940 errors:2 dropped:0 overruns:0 frame:0 TX packets:3579 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:119494875 (113.9 MiB) TX bytes:348430 (340.2 KiB) Interrupt:19 Base address:0x2000 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:5E:4B:1F inet addr:172.16.7.225 Bcast:172.16.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000
如果還想增加更多則可以eth0:1 eth0:2依次繼續增加
其他的一些參數:
-arp 打開或關閉指定接口上使用的ARP協議。前面加上一個負號用于關閉該選項。
-allmuti 關閉或啟動指定接口的無區別模式。前面加上一個負號用于關閉該選項。
-promisc 關閉或啟動指定網絡設備的promiscuous模式。前面加上一個負號用于關閉該選項。
add<地址> 設置網絡設備IPv6的IP地址。
del<地址> 刪除網絡設備IPv6的IP地址。
media<網絡媒介類型> 設置網絡設備的媒介類型。
mem_start<內存地址> 設置網絡設備在主內存所占用的起始地址。
metric<數目> 指定在計算數據包的轉送次數時,所要加上的數目。
mtu<字節> 設置網絡設備的MTU。
netmask<子網掩碼> 設置網絡設備的子網掩碼。
tunnel<地址> 建立IPv4與IPv6之間的隧道通信地址。
-broadcast<地址> 將要送往指定地址的數據包當成廣播數據包來處理。
-pointopoint<地址> 與指定地址的網絡設備建立直接連線,此模式具有保密功能。
二、route
route命令用于管理本機路由表,路由表就是指,本機訪問指定的主機或網絡需要通過的網絡接口,簡單點理解,你的網關就是通往所有其他網絡所必須要經過的網絡接口。
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
Destination代表目標網絡
Gateway代表網關地址,*代表目標是本機網絡不需要網關
Genmask代表你要訪問的網絡的子網掩碼
Flags為路由標志,標記當前網絡節點的狀態。
Flags標志說明:
U Up表示此路由當前為啟動狀態
H Host,表示此網關為一主機
G Gateway,表示此網關為一路由器
R Reinstate Route,使用動態路由重新初始化的路由
D Dynamically,此路由是動態性地寫入
M Modified,此路由是由路由守護程序或導向器動態修改
! 表示此路由當前為關閉狀態
Metric代表路由距離,到達指定網絡所需的中轉數
Ref代表路由項引用次數
Use代表此路由項被路由軟件查找的次數
Iface 該路由表項對應的輸出接口
route常用命令:
add : 添加一條路由規則
del : 刪除一條路由規則
-net : 目的地址是一個網絡
-host : 目的地址是一個主機
target : 目的網絡或主機
netmask : 目的地址的網絡掩碼
gw : 路由數據包通過的網關
dev : 為路由指定的網絡接口
1、添加一個主機路由到eth0
[root@localhost ~]# route add -host 172.16.1.2 dev eth0 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
2、刪除路由
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0 [root@localhost ~]# route del -net 192.168.122.0 netmask 255.255.255.0 virbr0 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
3、添加默認網關
[root@localhost ~]# route add default gw 172.16.0.1 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0 default 172.16.0.1 0.0.0.0 UG 0 0 0 eth0
4、刪除默認網關
[root@localhost ~]# route del default eth0 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
5、添加網絡到路由
[root@localhost ~]# route add -net 172.16.9.9 netmask 255.255.255.255 gw 172.16.0.1 eth0 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.9.9 172.16.0.1 255.255.255.255 UGH 0 0 0 eth0 172.16.1.2 * 255.255.255.255 UH 0 0 0 eth0 172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
三、DNS服務器指定
編輯/etc/resolv.conf
這里我配置三個DNS為例
[root@localhost ~]# vim /etc/resolv.conf #打開后的內容 # Generated by NetworkManager # No nameservers found; try putting DNS servers into your # ifcfg files in /etc/sysconfig/network-scripts like so: # # DNS1=xxx.xxx.xxx.xxx # DNS2=xxx.xxx.xxx.xxx # DOMAIN=lab.foo.com bar.foo.com #下面是三個DNS DNS1=172.16.0.1 DNS2=8.8.8.8 DNS3=114.114.114.114
四、ip
ip命令的功能和ifconfig類似,但是功能更加強大,所以centos7之后的系統有得包組是不帶ifconfig而只帶ip命令了
1、查看當前配置的所有網絡設備的信息
[root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:83:15:cb brd ff:ff:ff:ff:ff:ff inet 172.16.9.9/16 brd 172.16.255.255 scope global eno16777728 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe83:15cb/64 scope link valid_lft forever preferred_lft forever
后面接上指定的網卡也可以查看指定網卡的信息,也可使用ip address show
2、添加刪除地址
#添加 [root@localhost ~]# ip addr add 172.16.9.10 dev eno16777728:1 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:83:15:cb brd ff:ff:ff:ff:ff:ff inet 172.16.9.9/16 brd 172.16.255.255 scope global eno16777728 valid_lft forever preferred_lft forever inet 172.16.9.10/32 scope global eno16777728 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe83:15cb/64 scope link valid_lft forever preferred_lft forever #刪除 [root@localhost ~]# ip addr del 172.16.9.10 dev eno16777728:1 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:83:15:cb brd ff:ff:ff:ff:ff:ff inet 172.16.9.9/16 brd 172.16.255.255 scope global eno16777728 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe83:15cb/64 scope link valid_lft forever preferred_lft forever
3、查看現有的路由表
[root@localhost ~]# ip route show default via 172.16.0.1 dev eno16777728 proto static metric 100 172.16.0.0/16 dev eno16777728 proto kernel scope link src 172.16.9.9 metric 100
4、添加刪除路由和網關
#添加路由 [root@localhost ~]# ip route add 172.16.200.51 via 172.16.0.1 dev eno16777728 src 172.16.9.9 [root@localhost ~]# ip route show default via 172.16.0.1 dev eno16777728 proto static metric 100 172.16.0.0/16 dev eno16777728 proto kernel scope link src 172.16.9.9 metric 100 172.16.200.51 via 172.16.0.1 dev eno16777728 src 172.16.9.9 #172.16.200.51為目標ip #172.16.0.1為路由 #172.16.9.9為源ip #刪除路由 [root@localhost ~]# ip route del 172.16.200.51 [root@localhost ~]# ip route default via 172.16.0.1 dev eno16777728 proto static metric 100 172.16.0.0/16 dev eno16777728 proto kernel scope link src 172.16.9.9 metric 100 #添加默認網關 [root@localhost ~]# ip route add default via 172.16.0.1 dev eno16777728 [root@localhost ~]# ip route show default via 172.16.0.1 dev eno16777728 default via 172.16.0.1 dev eno16777728 proto static metric 100 172.16.0.0/16 dev eno16777728 proto kernel scope link src 172.16.9.9 metric 100 #刪除剛才添加的網關 [root@localhost ~]# ip route del default via 172.16.0.1 dev eno16777728 [root@localhost ~]# ip route default via 172.16.0.1 dev eno16777728 proto static metric 100 172.16.0.0/16 dev eno16777728 proto kernel scope link src 172.16.9.9 metric 100
4、關閉網卡修改網卡名稱
[root@localhost ~]# ip link set eno16777728 down [root@localhost ~]# ip link set eno16777728 name eth0 [root@localhost ~]# ip link set show [root@localhost ~]# ip link set eth0 up #這樣就把eno16777728改為了eth0,但是因為/etc/sysconfig/network-scripts/ifcfg-eno16777728還是原來的 #名字所以,配置不會跟著到eth0上,所以要完整的改名需要改變此配置文件中NAME=eth0,并將此文件名改為 #ifcfg-eth0
五、netstat
netstat命令主要用來查看整個系統的網絡情況
1、選項:
-a或–all:顯示所有連線中的端口;
-A<網絡類型>或–<網絡類型>:列出該網絡類型連線中的相關地址;
-c或–continuous:持續列出網絡狀態;
-C或–cache:顯示路由器配置的快取信息;
-e或–extend:顯示網絡其他相關信息;
-F或–fib:顯示FIB;
-g或–groups:顯示多重廣播功能群組組員名單;
-h或–help:在線幫助;
-i或–interfaces:顯示網絡界面信息表單;
-l或–listening:顯示監控中的服務器的Socket;
-M或–masquerade:顯示偽裝的網絡連線;
-n或–numeric:直接使用ip地址,而不通過域名服務器;
-N或–netlink或–symbolic:顯示網絡硬件外圍設備的符號連接名稱;
-o或–timers:顯示計時器;
-p或–programs:顯示正在使用Socket的程序識別碼和程序名稱;
-r或–route:顯示Routing Table;
-s或–statistice:顯示網絡工作信息統計表;
-t或–tcp:顯示TCP傳輸協議的連線狀況;
-u或–udp:顯示UDP傳輸協議的連線狀況;
-v或–verbose:顯示指令執行過程;
-V或–version:顯示版本信息;
-w或–raw:顯示RAW傳輸協議的連線狀況;
-x或–unix:此參數的效果和指定"-A unix"參數相同;
–ip或–inet:此參數的效果和指定"-A inet"參數相同。
2、常用的組合:
-tan, -uan, -tnl, -unl
#顯示連接的tcp端口 [root@localhost network-scripts]# 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 52 172.16.9.9:22 172.16.7.211:54886 ESTABLISHED tcp6 0 0 :::80 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN #顯示連接中的udp端口 [root@localhost network-scripts]# netstat -uan #顯示所有處于監聽(LISTEN)狀態的tcp端口 [root@localhost network-scripts]# 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 tcp6 0 0 :::80 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN #顯示所有處于監聽狀態的udp端口 [root@localhost network-scripts]# netstat -unl
六、ss
ss和netstat類似,但是顯示的信息更詳細,速度更快,在連接數上萬的情況下用ss比netstat更快更高效,所以推薦使用ss
1、選項:
-t: tcp協議相關
-u: udp協議相關
-w: 裸套接字相關
-x:unix sock相關
-l: listen狀態的連接
-a: 所有
-n: 數字格式
-p: 相關的程序及PID
-e: 擴展的信息
-m:內存用量
-o:計時器信息
2、常用組合
-tan:顯示所有連接的tcp端口
-tanl:顯示處于監聽狀態的tcp端口
-tanlp:顯示監聽狀態的tcp端口并顯示對應程序的PID
-uan:顯示所有連接的udp端口
-uanl:顯示處于監聽狀態的udp端口
-uanlp:顯示監聽狀態的udp端口并顯示對應程序的PID
[root@localhost network-scripts]# ss -tan State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* ESTAB 0 52 172.16.9.9:22 172.16.7.211:54886 LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@localhost network-scripts]# ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@localhost network-scripts]# ss -tanlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1250,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=2365,fd=13)) LISTEN 0 128 :::80 :::* users:(("httpd",pid=2824,fd=4),("httpd",pid=2823,fd=4),("httpd",pid=2822,fd=4),("httpd",pid=2821,fd=4)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=1250,fd=4)) LISTEN 0 100 ::1:25
七、網絡配置文件
1、前面所有的修改網絡參數的命令在重啟系統之后就會失效,只有更改網絡配置文件/etc/sysconfig/network-scripts/ifcfg-eno16777728【注意這是Centos7的,Centos6為ifcfg-eth0】在重啟之后才會有效
[root@localhost network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777728 TYPE="Ethernet"#接口類型 BOOTPROTO="static"#激活此設備時使用的地址配置協議,常用的dhcp, static, none, bootp DEFROUTE="yes" PEERDNS="yes"#如果BOOTPROTO的值為“dhcp”,是否允許dhcp server分配的dns服務器指向信息直接覆蓋至/etc/resolv.conf文件中 PEERROUTES="yes" IPV4_FAILURE_FATAL="no" NAME="eno16777728"#設備名稱 UUID="758d5aa7-239e-4488-94b3-6c3f33688dd5"#設備的唯一標識 DEVICE="eno16777728"#此配置文件應用到的設備 ONBOOT="yes"#在系統引導時是否激活此設備 IPADDR="172.16.9.9"#IP地址 NETMASK="255.255.0.0"#子網掩碼 GATEWAY="172.16.0.1"#默認網關 DNS1="61.128.128.68"#DNS服務器1 DNS2="61.128.192.68"#DNS服務器2
注意:記得重啟網絡服務方能生效;
重啟網絡服務:service network restart
/etc/rc.d/init.d/network restart
2、路由表的配置文件/etc/sysconfig/network-scripts/route-eno16777728【注意這是Centos7的,Centos6為ifcfg-eth0】
路由表有兩種風格,
#第一種 #目標網絡 via 路由 172.16.55.34 via 172.16.0.1 #第二種 #每三行定義一條路由 #目標地址 #子網掩碼 #路由 ADDRESS0=172.16.55.34 NETMASK0=255.255.0.0 GATEWAY0=172.16.0.1
注意:記得重啟網絡服務方能生效;
重啟網絡服務:service network restart
/etc/rc.d/init.d/network restart
原創文章,作者:N17_信風,如若轉載,請注明出處:http://www.www58058.com/13524
這篇文章的置頂,我只花了一眼的時間,因為我知道用心在寫標簽的同學都是用心在做到極致