linux命令ifconfig、route、netstat基本用法 Linux網絡屬性管理
局域網:以太網,令牌環網
Ethernet: CSMA/CD
沖突域
廣播域
MAC:Media Access Control
48bits:
24bits:
24bits:
IP: Internet Protocol
Routing protocol
Routed protocol
OSI, TCP/IP
tcp/ip分層:
application layer
transport layer
internet layer
datalink layer
pysical layer
傳輸層協議:
tcp, udp, sctp
網絡層協議:
ip
ip協議:
IPv4 地址分類:
點分十進制:0-255
0000 0000 - 1111 1111
0.0.0.0-255.255.255.255
A類:
0 000 0000 - 0 111 1111: 1-127
網絡數:126, 127
每個網絡中的主機數:2^24-2
默認子網掩碼:255.0.0.0
私網地址:10.0.0.0/8
B類:
10 00 0000 - 10 11 1111:128-191
網絡數:2^14
每個網絡中的主機數:2^16-2
默認子網掩碼:255.255.0.0
私網地址:172.16.0.0/16-172.31.0.0/16
C類:
110 0 0000 - 110 1 1111: 192-223
網絡數:2^21
每個網絡中的主機數:2^8-2
默認子網掩碼:255.255.255.0
私網地址:192.168.0.0/24-192.168.255.0/24
D類:組播
1110 0000 - 1110 1111: 224-239
E類:
240-255
子網掩碼:
172.16.100.100/255.255.0.0, 172.17.1.1
跨網絡通信:路由
主機路由
網絡路由
默認路由
將Linux主機接入到網絡中:
IP/mask
路由:默認網關
DNS服務器
主DNS服務器
次DNS服務器
第三DNS服務器
配置方式:
靜態指定:
ifcfg: ifconfig, route, netstat
ip: object {link, addr, route}, ss, tc
配置文件
system-config-network-tui (setup)
CentOS 7:
nmcli, nmtui
動態分配:
DHCP: Dynamic Host Configuration Protocol
[root@slave ~]# ifconfig
eth3 Link encap:Ethernet HWaddr 00:0C:29:31:3F:E3
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe31:3fe3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2530 errors:0 dropped:0 overruns:0 frame:0
TX packets:1051 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:377511 (368.6 KiB) TX bytes:210192 (205.2 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:65536 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:960 (960.0 b) TX bytes:960 (960.0 b)
[root@slave ~]# ifconfig -a
eth3 Link encap:Ethernet HWaddr 00:0C:29:31:3F:E3
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe31:3fe3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2548 errors:0 dropped:0 overruns:0 frame:0
TX packets:1063 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:379051 (370.1 KiB) TX bytes:212392 (207.4 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:65536 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:960 (960.0 b) TX bytes:960 (960.0 b)
[root@slave ~]# ifconfig eth3
eth3 Link encap:Ethernet HWaddr 00:0C:29:31:3F:E3
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe31:3fe3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2565 errors:0 dropped:0 overruns:0 frame:0
TX packets:1075 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:380439 (371.5 KiB) TX bytes:214764 (209.7 KiB)
配置網絡接口:
接口命名方式:
CentOS 6:
以太網:eth[0,1,2,...]
ppp:ppp[0,1,2,...]
ifconfig命令
ifconfig [interface]
# ifconfig -a
# ifconfig IFACE [up|down]
ifconfig interface [aftype] options | address ...
# ifconfig IFACE IP/mask [up]
# ifconfig IFACE IP netmask MASK
注意:立即生效;
啟用混雜模式:[-]promisc
route命令
路由管理命令
查看:route -n
添加:route add
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
目標:192.168.1.3 網關:172.16.0.1
# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0
目標:192.168.0.0 網關:172.16.0.1
# route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
# route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
默認路由,網關:172.16.0.1
# route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
]# route add default gw 172.16.0.1
刪除:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
目標:192.168.1.3 網關:172.16.0.1
# route del -host 192.168.1.3
目標:192.168.0.0 網關:172.16.0.1
# route del -net 192.168.0.0 netmask 255.255.255.0
[root@slave ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth3
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth3
DNS服務器指定
/etc/resolv.conf
nameserver DNS_SERVER_IP1
nameserver DNS_SERVER_IP2
nameserver DNS_SERVER_IP3
正解:FQDN-->IP
# dig -t A FQDN
# host -t A FQDN
反解:IP-->FQDN
# dig -x IP
# host -t PTR IP
FQDN: www.magedu.com.
[root@slave ~]# dig -x 192.168.0.108
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -x 192.168.0.108
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 7000
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;108.0.168.192.in-addr.arpa. IN PTR
;; AUTHORITY SECTION:
168.192.in-addr.arpa. 10800 IN SOA localhost. nobody.invalid. 1 3600 1200 604800 10800
;; Query time: 12 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Sat Jul 30 06:35:18 2016
;; MSG SIZE rcvd: 103
[root@slave ~]# dig -t A slave
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t A slave
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40867
;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available
;; QUESTION SECTION:
;slave. IN A
;; ANSWER SECTION:
slave. 128 IN A 180.168.41.175
;; Query time: 51 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Sat Jul 30 06:36:37 2016
;; MSG SIZE rcvd: 39
netstat命令:
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
顯示網絡連接:
netstat [--tcp|-t] [--udp|-u] [--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
[root@slave ~]# netstat -tnp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 52 192.168.0.108:22 192.168.0.107:49783 ESTABLISHED 3463/sshd
tcp 1 0 192.168.0.108:51708 23.62.109.73:80 CLOSE_WAIT 2417/clock-applet
[root@slave ~]# 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:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
[root@slave ~]# 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:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 ::1:6010 :::* LISTEN
[root@slave ~]# 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:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
顯示路由表:
netstat {--route|-r} [--numeric|-n]
-r: 顯示內核路由表
-n: 數字格式
顯示接口統計數據:
netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [--numeric|-n]
# netstat -i
# netstat -I IFACE
總結:ifcfg家庭命令配置
ifconfig/route/netstat
ifup/ifdown
本文出自 “梁小明的博客” 博客,請務必保留此出處http://7038006.blog.51cto.com/7028006/1832186