linux网络配置杂记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux网络配置杂记相关的知识,希望对你有一定的参考价值。

11.2

11.2.1 ifcfg

查看linux路由条目

[[email protected] ~]# 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

查看系统活动状态网路接口信息

[[email protected] ~]# 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

显示所有活动和非活动接口信息

[[email protected] ~]# 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)

显示指定接口信息

[[email protected] ~]# 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地址和掩码并启动

[[email protected] ~]# ifconfig eth0 10.201.106.129/24 up
[[email protected] ~]# 

[[email protected] ~]# ifconfig eth0 10.201.106.129 netmask 255.255.255.0 up #第二种方法

[[email protected] ~]# 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 

不反解显示路由表信息

[[email protected] ~]# 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发送

[[email protected] ~]# route add -host 192.168.1.3 gw 10.201.106.2 dev eth0
[[email protected] ~]# 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

第二种方法
[[email protected] ~]# route add default ge 10.201.106.2

添加网段路由

[[email protected] ~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 10.201.106.2 dev eth0
[[email protected] ~]# 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

添加默认路由

[[email protected] ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.201.106.2 dev eth0
SIOCADDRT: 文件已存在
[[email protected] ~]# 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

删除主机路由

[[email protected] ~]# route del -host 192.168.1.3/32  gw 10.201.106.2
[[email protected] ~]# 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

删除网段路由

[[email protected] ~]# route del -net 192.168.0.0 netmask 255.255.255.0
[[email protected] ~]# 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地址

[[email protected] ~]# vim /etc/resolv.conf 

; generated by /sbin/dhclient-script
search localdomain
nameserver 10.201.106.2

域名解析

[[email protected] ~]# 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

反向解析

[[email protected] ~]# dig -x 120.198.201.156

host命令正向和反向解析

[[email protected] ~]# host -t A www.qq.com
www.qq.com has address 120.198.201.156
[[email protected] ~]# 
[[email protected] ~]# 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

[[email protected] ~]# 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 
[[email protected] ~]# 
[[email protected] ~]# netstat -u
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     

不做端口反解

[[email protected]host ~]# 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 

显示所有连接,包括监听的

[[email protected] ~]# 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  

只显示监听的连接

[[email protected] ~]# 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                       :::*     

显示扩展属性

[[email protected] ~]# 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

[[email protected] ~]# 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  

查看路由表

[[email protected] ~]# 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

显示接口统计信息

[[email protected] ~]# 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
[[email protected] ~]# 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网络配置杂记的主要内容,如果未能解决你的问题,请参考以下文章

嵌入式Linux开发环境中双网卡使用过程杂记

nmcli和nmtui命令杂记

rhcsa学习杂记—centos7命令行配置网络连接以及修改主机名

linux杂记

Linux杂记

KVM虚拟机&openVSwitch杂记