ifconfig命令被用於配置和顯示Linux內核中網絡接口的網絡參數。用ifconfig命令配置的網卡信息,在網卡重啓後機器重啓後,配置就不存在。要想將上述的配置信息永遠的存的電腦裏,那就要修改網卡的配置文件了。
若系統默認沒有ifconfig命令,則使用下面命令進行安裝。linux
yum install net-tools
up
:啓動指定的網絡設備;
down
:關閉指定的網絡設備;
mtu <字節>
:設置網絡設備的最大傳輸單元;
netmask <子網掩碼>
:設置網絡設備的子網掩碼;
broadcast <廣播地址>
:設置網絡設備的廣播地址;
[vagrant@10 ~]$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet) RX packets 952 bytes 85854 (83.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 621 bytes 73814 (72.0 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<host> loop txqueuelen 0 (Local Loopback) 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
eth0 表示第一塊網卡。shell
lo是表示主機的迴環地址,這個通常是用來測試一個網絡程序,但又不想讓局域網或外網的用戶可以查看,只能在此臺主機上運行和查看所用的網絡接口。好比把 httpd服務器的指定到迴環地址,在瀏覽器輸入127.0.0.1就能看到你所架WEB網站了。但只是您能看獲得,局域網的其它主機或用戶無從知道。瀏覽器
\ | 解釋 |
---|---|
UP | 網卡處在開啓狀態 |
RUNNING | 網卡的網線被接上 |
MULTICAST | 支持組播 |
mtu 1500 | 最大傳輸單元:1500字節 |
inet | 網卡的IP地址 |
netmask | 掩碼地址 |
broadcast | 廣播地址 |
RX packets [xx] bytes [xx] | 接收數據包數量、字節數 |
TX packets [xx] bytes [xx] | 發送數據包數量、字節數 |
ifconfig eth0 up # 啓動網卡eth0 ifconfig eth0 down # 關閉網卡eth0
ifconfig eth0 arp #開啓網卡eth0 的arp協議 ifconfig eth0 -arp #關閉網卡eth0 的arp協議
# 若是不加任何其餘參數,則系統會依照該 IP 所在的 class 範圍,自動的計算出 netmask 以及 network, broadcast 等 IP 參數; [root@localhost ~]# ifconfig eth0 192.168.2.10 [root@localhost ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0 [root@localhost ~]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0 broadcast 192.168.2.255
ifconfig eth0 mtu 1500 #設置能經過的最大數據包大小爲 1500 bytes
ifconfig
的所有修改,以 ifcfg-eth*
的配置文件重置網絡設置/etc/init.d/network restart
ifconfig 全部配置修改功能都只是臨時修改,重啓網絡服務就會失效。
根據 /etc/sysconfig/network-scripts/ifcfg-eth* 配置文件啓動和關閉網卡
ifup [interface]服務器
ifdown [interface]網絡
ifup 與 ifdown 其實都是 shell 腳本,他會直接到 /etc/sysconfig/network-scripts
目錄下查找對應的配置文件,例如 ifup eth0
會讀取 ifcfg-eth0
這個文件的內容,而後加以設置。
不過,因爲這兩個腳本主要是經過讀取配置文件 (ifcfg-eth*) 來啓動與關閉網絡接口,因此在使用前請肯定 ifcfg-eth*
是否真的存在於正確的目錄內,不然會啓動失敗。另外,若是以 ifconfig eth0 ...
的方式 設定或修改了網路接口後,就沒法再以 ifdown eth0
的方式來關閉了! 由於 ifdown
會分析比對目前的網路參數與 ifcfg-eth0
是否相符,不符的話,就會放棄本次動做。所以,使用 ifconfig
修改完畢後,應該要用 ifconfig eth0 down
纔可以關閉該接口。dom
[root@10 vagrant]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet) RX packets 1870 bytes 173264 (169.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1110 bytes 143493 (140.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 08:00:27:db:78:8f txqueuelen 1000 (Ethernet) RX packets 107 bytes 12570 (12.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 17 bytes 1326 (1.2 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<host> loop txqueuelen 0 (Local Loopback) 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
ifconfig eth1 down
關閉 eth1
網卡[root@10 vagrant]# ifconfig eth1 down [root@10 vagrant]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet) RX packets 1938 bytes 178334 (174.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1145 bytes 146715 (143.2 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<host> loop txqueuelen 0 (Local Loopback) 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
ifdown lo
關閉 lo
網卡[root@10 vagrant]# ifdown lo [root@10 vagrant]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet) RX packets 2018 bytes 184304 (179.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1186 bytes 150461 (146.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifup
開啓 lo
和 eth1
網卡[root@10 vagrant]# ifup lo [root@10 vagrant]# ifup eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) [root@10 vagrant]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::e096:3a76:6df1:bd6d prefixlen 64 scopeid 0x20<link> ether 08:00:27:6b:57:88 txqueuelen 1000 (Ethernet) RX packets 2083 bytes 189104 (184.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1221 bytes 153755 (150.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.88 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::a00:27ff:fedb:788f prefixlen 64 scopeid 0x20<link> ether 08:00:27:db:78:8f txqueuelen 1000 (Ethernet) RX packets 107 bytes 12570 (12.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 24 bytes 1884 (1.8 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<host> loop txqueuelen 0 (Local Loopback) 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
顯示並設置Linux內核中的網絡路由表,route命令設置的路由主要是靜態路由。ssh
要實現兩個不一樣的子網之間的通訊,須要一臺鏈接兩個網絡的路由器,或者同時位於兩個網絡的網關來實現。socket
在Linux系統中設置路由一般是爲了解決如下問題:該Linux系統在一個局域網中,局域網中有一個網關,可以讓機器訪問Internet,那麼就須要將這臺機器的ip地址設置爲Linux機器的默認路由。要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啓或者機器重啓以後,該路由就失效了;能夠在/etc/rc.local中添加route命令來保證該路由設置永久有效。tcp
在一臺服務器裏,鏈接內網的網卡是不能進行設置。oop
route [選項] [參數]
-n
:不使用通訊協議或主機名,直接顯示數字形式的IP地址和端口號;
-net
:到一個網絡的路由表;
-host
:到一個主機的路由表。
add
:增長指定的路由記錄;
del
:刪除指定的路由記錄;
gw
:設置默認網關;
route -n
功能與
netstat -rn
命令一致
[root@10 vagrant]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.2.2 0.0.0.0 UG 102 0 0 eth0 10.0.2.0 0.0.0.0 255.255.255.0 U 102 0 0 eth0 192.168.33.0 0.0.0.0 255.255.255.0 U 103 0 0 eth1
Flags | 說明 |
---|---|
U | Up表示此路由當前爲啓動狀態。 |
H | Host,表示此網關爲一主機。 |
G | Gateway,表示此網關爲一路由器。 |
R | Reinstate Route,使用動態路由從新初始化的路由。 |
D | Dynamically,此路由是動態性地寫入。 |
M | Modified,此路由是由路由守護程序或導向器動態修改。 |
! 表示此路由當前爲關閉狀態。
route del default gw 192.168..1 route add default gw 192.168.0.2
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 #增長一條到達244.0.0.0的路由。
route add -net 224.0.0.0 netmask 240.0.0.0 reject #增長一條屏蔽的路由,目的地址爲224.x.x.x將被拒絕。
route del -net 224.0.0.0 netmask 240.0.0.0 route del -net 224.0.0.0 netmask 240.0.0.0 reject
查詢系統的狀態信息。
netstat [選項]
-t
:列出TCP協議的端口
-u
:列出UDP協議的端口
-n
:不使用域名與服務名,而使用IP地址和端口號
-l
:僅列出在監聽狀態的網絡服務
-a
:列出全部的網絡鏈接
-p
:顯示正在使用Socket的程序識別碼和程序名稱
-r
:顯示路由表
netstat -a #列出全部端口 netstat -at #列出全部tcp端口 netstat -au #列出全部udp端口
[root@10 vagrant]# netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:ssh 10.0.2.2:surveyinst ESTABLISHED tcp6 0 0 [::]:ssh [::]:* LISTEN udp 0 0 0.0.0.0:bootpc 0.0.0.0:* raw6 0 0 [::]:ipv6-icmp [::]:* 7 Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 3 [ ] DGRAM 6409 /run/systemd/notify ... 省略n行 ... unix 3 [ ] STREAM CONNECTED 12780 [root@10 vagrant]# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:ssh 10.0.2.2:surveyinst ESTABLISHED tcp6 0 0 [::]:ssh [::]:* LISTEN [root@10 vagrant]# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
netstat -l #只顯示監聽端口 netstat -lt #只列出全部監聽 tcp 端口 netstat -lu #只列出全部監聽 udp 端口
[root@10 vagrant]# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN tcp6 0 0 [::]:ssh [::]:* LISTEN udp 0 0 0.0.0.0:bootpc 0.0.0.0:* raw6 0 0 [::]:ipv6-icmp [::]:* 7 Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6422 /run/systemd/journal/stdout unix 2 [ ACC ] STREAM LISTENING 22047 /var/run/NetworkManager/private-dhcp unix 2 [ ACC ] STREAM LISTENING 10811 /run/lvm/lvmetad.socket unix 2 [ ACC ] STREAM LISTENING 10583 /run/lvm/lvmpolld.socket unix 2 [ ACC ] STREAM LISTENING 13211 /var/lib/gssproxy/default.sock unix 2 [ ACC ] STREAM LISTENING 10345 /run/systemd/private unix 2 [ ACC ] STREAM LISTENING 12662 /run/dbus/system_bus_socket unix 2 [ ACC ] STREAM LISTENING 12665 /var/run/rpcbind.sock unix 2 [ ACC ] STREAM LISTENING 13212 /run/gssproxy.sock unix 2 [ ACC ] SEQPACKET LISTENING 10439 /run/udev/control [root@10 vagrant]# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN tcp6 0 0 [::]:ssh [::]:* LISTEN [root@10 vagrant]# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
netstat -tulnp
[root@10 vagrant]# netstat -tulnp 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 1053/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 4406/sendmail: acce tcp6 0 0 :::22 :::* LISTEN 1053/sshd udp 0 0 0.0.0.0:68 0.0.0.0:* 3824/dhclient
netstat -anp | grep ssh
netstat -anp | grep ':22'
[root@10 vagrant]# netstat -anp | grep ssh tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1053/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:3212 ESTABLISHED 3888/sshd: vagrant tcp6 0 0 :::22 :::* LISTEN 1053/sshd unix 3 [ ] STREAM CONNECTED 29697 3890/sshd: vagrant@ unix 3 [ ] STREAM CONNECTED 16196 1053/sshd unix 3 [ ] STREAM CONNECTED 29698 3888/sshd: vagrant unix 2 [ ] DGRAM 29694 3888/sshd: vagrant [root@10 vagrant]# netstat -anp | grep ':22' tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1053/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:3212 ESTABLISHED 3888/sshd: vagrant tcp6 0 0 :::22 :::* LISTEN 1053/sshd
netstat -an | grep "ESTABLISHED" | wc -l
[root@10 vagrant]# netstat -an | grep "ESTABLISHED" tcp 0 0 10.0.2.15:22 10.0.2.2:3212 ESTABLISHED [root@10 vagrant]# netstat -an | grep "ESTABLISHED" | wc -l 1
netstat -anop | grep "ssh" | wc -l
[root@10 vagrant]# netstat -anop | grep "ssh" tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1053/sshd off (0.00/0/0) tcp 0 0 10.0.2.15:22 10.0.2.2:3212 ESTABLISHED 3888/sshd: vagrant keepalive (3494.79/0/0) tcp6 0 0 :::22 :::* LISTEN 1053/sshd off (0.00/0/0) unix 3 [ ] STREAM CONNECTED 29697 3890/sshd: vagrant@ unix 3 [ ] STREAM CONNECTED 16196 1053/sshd unix 3 [ ] STREAM CONNECTED 29698 3888/sshd: vagrant unix 2 [ ] DGRAM 29694 3888/sshd: vagrant [root@10 vagrant]# netstat -anop | grep "ssh" | wc -l 7
netstat -rn
功能與
route -n
命令一致
[root@10 vagrant]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.33.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 [root@10 vagrant]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.2.2 0.0.0.0 UG 102 0 0 eth0 10.0.2.0 0.0.0.0 255.255.255.0 U 102 0 0 eth0 192.168.33.0 0.0.0.0 255.255.255.0 U 103 0 0 eth1
顯示處於活動狀態的Socket信息。ss命令能夠用來獲取socket統計信息,它能夠顯示和netstat相似的內容。但ss的優點在於它可以顯示更多更詳細的有關TCP和鏈接狀態的信息,並且比netstat更快速更高效。
ss [選項]
-t
:列出TCP協議的Socket
-u
:列出UDP協議的Socket
-n
:不使用域名與服務名,而使用IP地址和端口號
-l
:僅列出在監聽狀態的Socket
-a
:列出全部的Socket
-p
:顯示正在使用Socket的進程信息
ss -at #列出tcp鏈接 ss -au #列出udp鏈接
[root@10 vagrant]# ss -at State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* LISTEN 0 10 127.0.0.1:smtp *:* ESTAB 0 0 10.0.2.15:ssh 10.0.2.2:surveyinst LISTEN 0 128 :::ssh :::* [root@10 vagrant]# ss -au State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:bootpc *:*
ss -lt #列出監聽 tcp 端口 ss -lu #列出監聽 udp 端口
[root@10 vagrant]# ss -lt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* LISTEN 0 10 127.0.0.1:smtp *:* LISTEN 0 128 :::ssh :::* [root@10 vagrant]# ss -lu State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:bootpc *:*
ss -tulnp
[root@10 vagrant]# ss -tulnp Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 *:68 *:* users:(("dhclient",pid=3824,fd=6)) tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1053,fd=3)) tcp LISTEN 0 10 127.0.0.1:25 *:* users:(("sendmail",pid=4406,fd=4)) tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=1053,fd=4))
ss -anp | grep ssh
ss -anp | grep ':22'
[root@10 vagrant]# ss -anp | grep ssh u_str ESTAB 0 0 * 29697 * 29698 users:(("sshd",pid=3890,fd=5)) u_str ESTAB 0 0 * 16196 * 16249 users:(("sshd",pid=1053,fd=2),("sshd",pid=1053,fd=1)) u_str ESTAB 0 0 * 29698 * 29697 users:(("sshd",pid=3888,fd=7)) u_dgr UNCONN 0 0 * 29694 * 6427 users:(("sshd",pid=3890,fd=4),("sshd",pid=3888,fd=4)) tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1053,fd=3)) tcp ESTAB 0 0 10.0.2.15:22 10.0.2.2:3212 users:(("sshd",pid=3890,fd=3),("sshd",pid=3888,fd=3)) tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=1053,fd=4)) [root@10 vagrant]# ss -anp | grep ':22' tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1053,fd=3)) tcp ESTAB 0 0 10.0.2.15:22 10.0.2.2:3212 users:(("sshd",pid=3890,fd=3),("sshd",pid=3888,fd=3)) tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=1053,fd=4))