【Linux】网络管理

Posted by 西维蜀黍 on 2021-03-07, Last Modified on 2022-04-03

相关command

  • ifup
  • ifdown
  • netstat
  • route
  • ip

Interface Management

Restart network interface

Try

sudo service network-manager restart

Or on recent Ubuntu versions:

sudo systemctl restart systemd-networkd

instead.

View Interfaces

$ 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   2697347      0      0 0       2630262      0      0      0 BMRU
lo        16436 0      2840      0      0 0          2840      0      0      0 LRU
ppp0       1496 0    102800      0      0 0         63437      0      0      0 MOPRU
vmnet1     1500 0         0      0      0 0            49      0      0      0 BMRU
vmnet8     1500 0         0      0      0 0            49      0      0      0 BMRU
# View network settings of an Ethernet adapter:
$ ifconfig eth0
    
$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr b8:ac:6f:65:31:e5  
          inet addr:192.168.2.100  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::baac:6fff:fe65:31e5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2697529 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2630541 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2159382827 (2.0 GiB)  TX bytes:1389552776 (1.2 GiB)
          Interrupt:17 
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:16436  Metric:1
          RX packets:2849 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2849 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2778290 (2.6 MiB)  TX bytes:2778290 (2.6 MiB)
 
ppp0      Link encap:Point-to-Point Protocol  
          inet addr:10.1.3.105  P-t-P:10.0.31.18  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1496  Metric:1
          RX packets:102800 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63437 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:148532544 (141.6 MiB)  TX bytes:4425518 (4.2 MiB)
 
vmnet1    Link encap:Ethernet  HWaddr 00:50:56:c0:00:01  
          inet addr:192.168.47.1  Bcast:192.168.47.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:49 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
 
vmnet8    Link encap:Ethernet  HWaddr 00:50:56:c0:00:08  
          inet addr:172.16.232.1  Bcast:172.16.232.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:49 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)          
# List interfaces with link layer info:
$ ip link show
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
enp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
   link/ether 24:b6:fd:37:8b:29 brd ff:ff:ff:ff:ff:ff
wlp9s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
   link/ether c0:18:85:50:47:4f brd ff:ff:ff:ff:ff:ff

# List interfaces with detailed info:
$ ip address    

# To see statistics for all network interfaces    
$ ip -s link

Control Interfaces

# Make an interface up/down:
$ ip link set <interface> up/down

# Disable eth0 interface:
$ ifconfig eth0 down

# Enable eth0 interface:
$ ifconfig eth0 up

IP 设置

设置 Interface 为 DHCP

IPV4

via /etc/network/interfaces

$ sudo vim /etc/network/interfaces

Then add the following lines for IPv4 for eth1:

auto eth1
iface eth1 inet dhcp

For DHCPv6 (used for IPv6), append also the following iface eth1

iface eth1 inet6 dhcp

Bring the interface down.

$ ifdown eth1

Bring the interface back up.

$ ifup eth1

Verify your network settings have been applied.

$ ip a

Via /etc/config/network

$ vim /etc/config/network

config interface 'lan'
    option device 'br-lan'
    option proto 'dhcp'

IPV6

$ sudo vim /etc/network/interfaces

Add the following lines.

auto enp0s3
iface enp0s3 inet6 auto

Save your changes and exit the text editor.

Bring the interface down.

$ ifdown enp0s3

Bring the interface back up.

$ ifup enp0s3

Verify your network settings have been applied.

$ ip a

设置 Interface 为静态IP

via Commands

# Assign IP address to eth0 interface:
$ ifconfig eth0 192.168.1.10 netmask 255.255.255.0
$ ifconfig eth0 up/down

# Add/Delete an IP address to an interface:
$ ip addr add/del ip/mask dev <interface>

via Config Files

via /etc/network/interfaces

To set a Static IP address, you have to edit /etc/network/interfaces

 $ sudo vim /etc/network/interfaces

Add the below lines to /etc/network/interfaces.

auto eth1
  iface eth1 inet static
  address 192.168.72.8
  netmask 255.255.255.0
  gateway 192.168.72.1
  dns-nameservers 8.8.8.8 4.4.2.2
  
  # If you want to add an IPv6 address, too, append something like:
  iface eth0 inet6 static
        address 2001:db8::c0ca:1eaf/64
        gateway 2001:db8::1ead:ed:beef  

See man interfaces for more options.

Make sure to disable all DHCP services, e.g. dhcpcd.

Save the file and restart networking services using the below command.

$ sudo systemctl restart networking

via /etc/config/network

$ vim /etc/config/network

config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option ipaddr '192.168.1.1'
    option netmask '255.255.255.0'
    option ip6assign '60'

Starting and Stopping Interfaces On Boot-up

$ auto eth1

Setting up/Changing Hostname

The hostname of Ubuntu OS is configured in the file /etc/hostname.

To edit /etc/hostname, enter the below command:

$ sudo vim /etc/hostname

This file contains only the hostname of the file, change the name, and then save it.

This will bring up lo (loopback) and eth0 on running ifup -a (done at boot time).

From man interfaces:

Lines beginning with the word “auto” are used to identify the physical interfaces to be brought up when ifup is run with the -a option. (This option is used by the system boot scripts.) Physical interface names should follow the word “auto” on the same line. There can be multiple “auto” stanzas. ifup brings the named interfaces up in the order listed.

DNS Config

via /etc/resolv.conf

The configuration file resolv.conf at /etc/resolv.conf contains information that allows a computer connected to a network to resolve names into addresses. (Note: Do not confuse this configuration file with the program resolvconf, which unfortunately has a nearly identical name.)

The resolv.conf file typically contains the IP addresses of nameservers (DNS name resolvers) that will attempt to translate names into addresses for any node available on the network. There will be a line or lines that look like this:

nameserver 12.34.56.78
nameserver 12.34.56.79

via /etc/network/interfaces

You need to configure the /etc/network/interfaces file if you want to change your DNS server via the command line.

It should look something like this:

# The loopback network interface  
auto lo  
iface lo inet loopback  


# The primary network interface  
auto eth0 
iface eth0 inet static  
address 192.168.X.X
netmask 255.255.255.0
gateway 192.168.X.X
dns-nameservers X.X.X.X 

If you have more than one DNS server, just add a space between each:

dns-nameservers X.X.X.X Y.Y.Y.Y Z.Z.Z.Z

Just replace the Xs, Ys, and Zs with your own IPs of the DNS servers of choice, and when this is done, run this command to update the settings:

sudo ifdown eth0 && sudo ifup eth0

ARP

# 查看 ARP 表
$ arp -a
openwrt.lan (192.168.19.1) at 88:c3:dd:cc:bb:aa on en8 ifscope [ethernet]
weishi-mac.lan (192.168.19.173) at 0:e0:aa:bb:cc:dd on en8 ifscope permanent [ethernet]
nishiinhinoipad.lan (192.168.19.202) at 74:81:11:22:33:c on en8 ifscope [ethernet]
iphone.lan (192.168.19.206) at 94:c:aa:bb:cc:dd on en8 ifscope [ethernet]
? (192.168.19.255) at ff:ff:ff:ff:ff:ff on en8 ifscope [ethernet]
? (224.0.0.251) at 1:0:aa:0:0:fb on en8 ifscope permanent [ethernet]
? (239.255.255.250) at 1:0:bb:cc:dd:fa on en8 ifscope permanent [ethernet]

# 清空ARP表
$ sudo arp -ad

# Show neighbors (ARP table):
$ ip neighbour

路由表(Route Tables)

Operation

Operation via route Command

View

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         dsldevice.lan   0.0.0.0         UG    100    0        0 enp0s4
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.18.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s4

As you can see the default gateway is 192.168.0.1, it is the router, the first hop through which the traffic passes before going to the next hop or final node.

By applying the flag -n (numerical) you can replace the destination domain name for IP addresses:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.18.1    0.0.0.0         UG    100    0        0 enp0s4
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.18.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s4
192.168.18.1    0.0.0.0         255.255.255.255 UH    100    0        0 enp0s4

各列字段说明:

含义
Destination 目标网络段(如192.168.1.0 就表示 192.168.1.0)或目标主机。Destination 为 default(0.0.0.0)时,表示这个是默认网关,所有未在路由表中声明的流量都会发到这个网关(这里是 192.168.19.1)
Gateway 网关地址,0.0.0.0 (或 *)表示当前记录对应的 Destination 跟本机在同一个网段,通信时不需要经过网关
Genmask Destination 字段的网络掩码,当Destination 是主机时,Genmask为 255.255.255.255,是默认路由时(即 Destination 为 default 时)会设置为 0.0.0.0
Flags 标记,含义参考表格后面的解释
Metric 路由距离,到达指定网络所需的中转数,是大型局域网和广域网设置所必需的 (不在Linux内核中使用。)
Ref 路由项引用次数 (不在Linux内核中使用,恒为0)
Use 该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。
Iface 网络接口名字,例如 eth0

​ Flags 含义:

  • U :表示该路由是活跃的,因此可以被使用
  • H :表示目标是一个主机(而不是一个网络段)
    • 如果没有设置H标志,说明 Destination 是一个网络段(主机号部分为0)
    • 当为某个目的IP地址搜索路由表时,主机地址项必须与目的地址完全匹配,而网络地址项只需要匹配目的地址的网络号和子网号就可以了
  • G :表示需要经过网关
    • 如果没有设置该标志,说明目的地是直接相连的。标志G是非常重要的,因为它区分了间接路由和直接路由(对于直接路由来说是不设置标志G的)
  • R :恢复动态路由产生的表项
  • D :表示该路由是由重定向报文创建的。
  • M:表示该路由已被重定向报文修改。
  • ! :表示拒绝路由

If you want to display the local table, which contains the destination addresses assigned to the local host you can run:

$ ip route show table local

For IPv4

$ ip -4 r
default via 192.168.18.1 dev enp0s4 proto dhcp src 192.168.18.110 metric 100
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.18.0/24 dev enp0s4 proto kernel scope link src 192.168.18.110
192.168.18.1 dev enp0s4 proto dhcp scope link src 192.168.18.110 metric 100

For IPv6 the syntax is the same:

$ ip -6 r
::1 dev lo proto kernel metric 256 pref medium
2406:3003:206b:2a4e::/64 dev enp0s4 proto ra metric 100 expires 172783sec pref medium
2406:3003:206b:2a4e::/64 via fe80::1 dev enp0s4 proto ra metric 1024 expires 172783sec pref high
fe80::/64 dev enp0s4 proto kernel metric 256 pref medium
fe80::/64 dev docker0 proto kernel metric 256 linkdown pref medium

Add

Add One Entity

# add
$ route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

# Add a default route
$ ip route add default via ip dev <interface>

# restart the service
$ sudo service networking restart

adds the normal loopback entry, using netmask 255.0.0.0 and associated with the “lo” device (assuming this device was previously set up correctly with ifconfig(8)).

$ route add -net 127.0.0.0 netmask 255.0.0.0 dev lo

adds a route to the local network 192.56.76.x via “eth0”. The word “dev” can be omitted here.

$ route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0

In order to add a new route, we will use the command ip route add by providing related information. We will add a new route to the 172.16.0.0/16 network.

$ sudo ip route add 172.16.0.0/24 via 192.168.122.1 dev ens3
  • ip route add is our command to add a new route.
  • 172.16.0.0/24 is a target network that is the destination range.
  • via 192.168.122.1 specifies the next hoop which should be directly connected to our system network.
  • dev ens3 specifies our interface which is the same network with 192.168.122.1

Alternatively, we can add a new route by using the command route. We will use the option add of the route command where also provide other parameters like destination network, gateway, and interface name. In the following example, we will add a route to the network 10.0.0.0 by using the -net option by setting 192.168.1.1 as gateway for the interface eth0.

$ sudo route add -net 10.0.0.0/8 gw 192.168.1.1 eth0

Add Default Route

# route command to set a default router to 192.168.1.254
$ route add default gw 192.168.1.254

# or 
$ ip route add default via 192.168.0.1

IPv6

To add an IPv6 gateway run:

# ip -6 route add default via <IP-Address>

Save routing information to a configuration file /etc/network/interfaces

You can put static routes in /etc/network/interfaces:

$ vi /etc/network/interfaces

# OR
$ sudo vi /etc/network/interfaces
auto eth0
iface eth0 inet static
      address 192.168.1.2
      netmask 255.255.255.0
      up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
      up route add -net 172.16.0.0 netmask 255.240.0.0 gw 192.168.1.1

Save and close the file. Restart networking:

$ /etc/init.d/networking restart
# OR
$ sudo /etc/init.d/networking restart

Delete

We can delete an existing route by using the command route. It is very similar to the adding route where we will replace the add options with the del option.

$ sudo route del -net 10.0.0.0/8 gw 192.168.1.1 eth0

deletes the current default route, which is labeled “default” or 0.0.0.0 in the destination field of the current routing table.

$  route del default

Operation via ip route Command

View

Use the ip route command without options to display the IP routing table. For example:

# Display the routing table:
$ ip route
default via 192.168.122.1 dev eth0  proto static  metric 1024 
192.168.122.0/24 dev ens9  proto kernel  scope link  src 192.168.122.107 
192.168.122.0/24 dev eth0  proto kernel  scope link  src 192.168.122.126

Add

To add a static route to a host address, in other words to a single IP address, issue a command as root:

~]# ip route add 192.0.2.1 via 10.0.0.1 [dev ifname]

Where 192.0.2.1 is the IP address of the host in dotted decimal notation, 10.0.0.1 is the next hop address and ifname is the exit interface leading to the next hop.

To add a static route to a network, in other words to an IP address representing a range of IP addresses, issue the following command as root:

~]# ip route add 192.0.2.0/24 via 10.0.0.1 [dev ifname]

where 192.0.2.0 is the IP address of the destination network in dotted decimal notation and /24 is the network prefix. The network prefix is the number of enabled bits in the subnet mask. This format of network address slash network prefix length is sometimes referred to as classless inter-domain routing (CIDR) notation.

unicast

A unicast route is the most common route in routing tables. This is a typical route to a destination network address, which describes the path to the destination. Even complex routes, such as nexthop routes are considered unicast routes. If no route type is specified on the command line, the route is assumed to be a unicast route.

ip route add unicast 192.168.0.0/24 via 192.168.100.5
ip route add default via 193.7.255.1
ip route add unicast default via 206.59.29.193
ip route add 10.40.0.0/16 via 10.72.75.254

broadcast

This route type is used for link layer devices (such as Ethernet cards) which support the notion of a broadcast address. This route type is used only in the local routing table [26] and is typically handled by the kernel.

ip route add table local broadcast 10.10.20.255 dev eth0 proto kernel scope link src 10.10.20.67
ip route add table local broadcast 192.168.43.31 dev eth4 proto kernel scope link src 192.168.43.14

local

The kernel will add entries into the local routing table when IP addresses are added to an interface. This means that the IPs are locally hosted IPs [27].

ip route add table local local 10.10.20.64 dev eth0 proto kernel scope host src 10.10.20.67
ip route add table local local 192.168.43.12 dev eth4 proto kernel scope host src 192.168.43.14

nat

This route entry is added by the kernel in the local routing table, when the user attempts to configure stateless NAT. See Section 5.3, “Stateless NAT with iproute2 for a fuller discussion of network address translation in general. [28].

ip route add nat 193.7.255.184 via 172.16.82.184
ip route add nat 10.40.0.0/16 via 172.40.0.0

unreachable

When a request for a routing decision returns a destination with an unreachable route type, an ICMP unreachable is generated and returned to the source address.

ip route add unreachable 172.16.82.184
ip route add unreachable 192.168.14.0/26
ip route add unreachable 209.10.26.51

prohibit

When a request for a routing decision returns a destination with a prohibit route type, the kernel generates an ICMP prohibited to return to the source address.

ip route add prohibit 10.21.82.157
ip route add prohibit 172.28.113.0/28
ip route add prohibit 209.10.26.51

blackhole

A packet matching a route with the route type blackhole is discarded. No ICMP is sent and no packet is forwarded.

ip route add blackhole default
ip route add blackhole 202.143.170.0/24
ip route add blackhole 64.65.64.0/18

throw

The throw route type is a convenient route type which causes a route lookup in a routing table to fail, returning the routing selection process to the RPDB. This is useful when there are additional routing tables. Note that there is an implicit throw if no default route exists in a routing table, so the route created by the first command in the example is superfluous, although legal.

ip route add throw default
ip route add throw 10.79.0.0/16
ip route add throw 172.16.0.0/12

Linux 内核的路由种类

主机路由

路由表中指向单个 IP 地址或主机名的路由记录,其 Flags 字段为 H。下面示例中,对于目的地为 10.0.0.10 这个主机的流量,路由到网关 10.139.128.1 :

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.10       10.139.128.1    255.255.255.255 UGH   0      0        0 eth0
...

网络路由

主机可以到达的网络。下面示例中,对于目的地为 10.0.0.0/24 这个网络的流量,路由到网关 10.139.128.1 :

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        10.139.128.1    255.255.255.0   UG    0      0        0 eth0

默认路由

当目标主机的 IP 地址或网络不在路由表中时,数据包就被路由到默认路由(默认网关)。默认路由的 Destination 是 default 或 0.0.0.0。

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0

Example

一个路由器开启NAT后:

  • eth4 网口连接到上游路由器
  • NAT的网段为 192.168.55/24
$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.18.1    0.0.0.0         UG        0 0          0 eth4
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
192.168.18.0    0.0.0.0         255.255.255.0   U         0 0          0 eth4
192.168.18.1    0.0.0.0         255.255.255.255 UH        0 0          0 eth4
192.168.55.0    0.0.0.0         255.255.255.0   U         0 0          0 br0
239.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 br0

一个WIFI路由器开启 bridge模式:

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.18.1    0.0.0.0         UG        0 0          0 br0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
192.168.18.0    0.0.0.0         255.255.255.0   U         0 0          0 br0

$ ifconfig
archer    Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1754667 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1775025 errors:0 dropped:8 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:456004197 (434.8 MiB)  TX bytes:457135713 (435.9 MiB)

br0       Link encap:Ethernet  HWaddr ...
          inet addr:192.168.18.96  Bcast:192.168.18.255  Mask:255.255.255.0
          UP BROADCAST RUNNING ALLMULTI MULTICAST  MTU:1500  Metric:1
          RX packets:532824 errors:0 dropped:141606 overruns:0 frame:0
          TX packets:245298 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:75437079 (71.9 MiB)  TX bytes:169992108 (162.1 MiB)

eth0      Link encap:Ethernet  HWaddr ...
          UP BROADCAST ALLMULTI MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr ...
          UP BROADCAST ALLMULTI MULTICAST  MTU:1500  Metric:1
          RX packets:25209 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26707 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3328434 (3.1 MiB)  TX bytes:20415621 (19.4 MiB)

即该WIFI “路由器” 作为一个AP(严格来说,并不是一个路由器,因为没有路由功能)。这时候,连接该 AP 的所有设备都会由该 AP 的上一级路由设备分配IP。

Reference