标签:linux命令ifconfig、route、netstat基本用法
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
linux命令ifconfig、route、netstat基本用法
标签:linux命令ifconfig、route、netstat基本用法
原文地址:http://7038006.blog.51cto.com/7028006/1832186