linux網絡配置雜記
阿新 • • 發佈:2018-03-26
linux 路由 配置 雜記 基礎11.2
11.2.1 ifcfg
查看linux路由條目
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
查看系統活動狀態網路接口信息
[root@CentOS7 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.201.106.130 netmask 255.255.255.0 broadcast 10.201.106.255 inet6 fe80::20c:29ff:fea7:7aec prefixlen 64 scopeid 0x20<link> ether 00:0c:29:a7:7a:ec txqueuelen 1000 (Ethernet) RX packets 15514 bytes 17988974 (17.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5902 bytes 1004507 (980.9 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 4 bytes 340 (340.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 340 (340.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
顯示所有活動和非活動接口信息
[root@localhost ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:0C:29:DA:5A:22 inet addr:10.201.106.129 Bcast:10.201.106.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feda:5a22/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14676 errors:0 dropped:0 overruns:0 frame:0 TX packets:10293 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:9260953 (8.8 MiB) TX bytes:1450866 (1.3 MiB) Interrupt:19 Base address:0x2000 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:65536 Metric:1 RX packets:10 errors:0 dropped:0 overruns:0 frame:0 TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:568 (568.0 b) TX bytes:568 (568.0 b) pan0 Link encap:Ethernet HWaddr 52:2C:94:69:6E:69 inet6 addr: fe80::502c:94ff:fe69:6e69/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:592 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:200880 (196.1 KiB)
顯示指定接口信息
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:DA:5A:22
inet addr:10.201.106.129 Bcast:10.201.106.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feda:5a22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14707 errors:0 dropped:0 overruns:0 frame:0
TX packets:10322 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9263637 (8.8 MiB) TX bytes:1454632 (1.3 MiB)
Interrupt:19 Base address:0x2000
配置eth0地址和掩碼並啟動
[root@localhost ~]# ifconfig eth0 10.201.106.129/24 up
[root@localhost ~]#
[root@localhost ~]# ifconfig eth0 10.201.106.129 netmask 255.255.255.0 up #第二種方法
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:DA:5A:22
inet addr:10.201.106.129 Bcast:10.201.106.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feda:5a22/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15000 errors:0 dropped:0 overruns:0 frame:0
TX packets:10544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9290103 (8.8 MiB) TX bytes:1479528 (1.4 MiB)
Interrupt:19 Base address:0x2000
不反解顯示路由表信息
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
添加一條主機路由,並指定從eth0發送
[root@localhost ~]# route add -host 192.168.1.3 gw 10.201.106.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table # Flags:條目標誌位 U:啟用狀態,G:表示網關 H:主機路由
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.3 10.201.106.2 255.255.255.255 UGH 0 0 0 eth0
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
第二種方法
[root@localhost ~]# route add default ge 10.201.106.2
添加網段路由
[root@localhost ~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 10.201.106.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.3 10.201.106.2 255.255.255.255 UGH 0 0 0 eth0
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 10.201.106.2 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
添加默認路由
[root@localhost ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.201.106.2 dev eth0
SIOCADDRT: 文件已存在
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.3 10.201.106.2 255.255.255.255 UGH 0 0 0 eth0
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 10.201.106.2 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
刪除主機路由
[root@localhost ~]# route del -host 192.168.1.3/32 gw 10.201.106.2
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 10.201.106.2 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
刪除網段路由
[root@localhost ~]# route del -net 192.168.0.0 netmask 255.255.255.0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
11.2.2 DNS
修改系統DNS地址
[root@localhost ~]# vim /etc/resolv.conf
; generated by /sbin/dhclient-script
search localdomain
nameserver 10.201.106.2
域名解析
[root@localhost ~]# dig -t A www.qq.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t A www.qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12877
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.qq.com. IN A
;; ANSWER SECTION:
www.qq.com. 5 IN A 120.198.201.156
;; Query time: 25 msec
;; SERVER: 10.201.106.2#53(10.201.106.2)
;; WHEN: Wed Jul 20 18:19:45 2016
;; MSG SIZE rcvd: 44
反向解析
[root@localhost ~]# dig -x 120.198.201.156
host命令正向和反向解析
[root@localhost ~]# host -t A www.qq.com
www.qq.com has address 120.198.201.156
[root@localhost ~]#
[root@localhost ~]# host -t PTR 120.192.201.156
Host 156.201.192.120.in-addr.arpa. not found: 3(NXDOMAIN)
11.2.3 netstat
只顯示TCP或UDP
[root@localhost ~]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 10.201.106.129:ssh 10.201.106.1:55907 ESTABLISHED
tcp 0 52 10.201.106.129:ssh 10.201.106.1:56655 ESTABLISHED
[root@localhost ~]#
[root@localhost ~]# netstat -u
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
不做端口反解
[root@localhost ~]# netstat -tn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 10.201.106.129:22 10.201.106.1:55907 ESTABLISHED
tcp 0 52 10.201.106.129:22 10.201.106.1:56655 ESTABLISHED
顯示所有連接,包括監聽的
[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:21 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 10.201.106.129:22 10.201.106.1:55907 ESTABLISHED
tcp 0 52 10.201.106.129:22 10.201.106.1:56655 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
只顯示監聽的連接
[root@localhost ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address
tcp 0 0 0.0.0.0:21 0.0.0.0:*
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp 0 0 127.0.0.1:631 0.0.0.0:*
tcp 0 0 127.0.0.1:25 0.0.0.0:*
tcp 0 0 :::22 :::*
tcp 0 0 ::1:631 :::*
tcp 0 0 ::1:25 :::*
tcp 0 0 :::80 :::*
顯示擴展屬性
[root@localhost ~]# netstat -tnle
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address
tcp 0 0 0.0.0.0:21 0.0.0.0:*
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp 0 0 127.0.0.1:631 0.0.0.0:*
tcp 0 0 127.0.0.1:25 0.0.0.0:*
tcp 0 0 :::22 :::*
tcp 0 0 ::1:631 :::*
tcp 0 0 ::1:25 :::*
tcp 0 0 :::80
通過-p參數,顯示連接所屬的進程和PID
[root@localhost ~]# netstat -tnlp
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:21 0.0.0.0:* LISTEN 1438/vsftpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1425/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1253/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1550/master
tcp 0 0 :::22 :::* LISTEN 1425/sshd
tcp 0 0 ::1:631 :::* LISTEN 1253/cupsd
tcp 0 0 ::1:25 :::* LISTEN 1550/master
tcp 0 0 :::80 :::* LISTEN 20471/httpd
查看路由表
[root@localhost ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.201.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.201.106.2 0.0.0.0 UG 0 0 0 eth0
顯示接口統計信息
[root@localhost ~]# netstat -Ieth0 #通過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 18050 0 0 0 13229 0 0 0 BMRU
[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 18064 0 0 0 13239 0 0 0 BMRU
lo 65536 0 10 0 0 0 10 0 0 0 LRU
pan0 1500 0 0 0 0 0 871 0 0 0 BMRU
linux網絡配置雜記