Linux系统网络管理

一、网络参数配置

1、图形化配置

NetworkManager,Linux7系统中,一般建议停止该管理方式;Linux8以上操作系统统一将网络管理交给NetworkManager管理。

#开启
[root@localhost ~]# systemctl start NetworkManager
#关闭
[root@localhost ~]# systemctl stop NetworkManager
#关闭并开机不自启
[root@localhost ~]# systemctl disable --now NetworkManager
#开启并开机自启
[root@localhost ~]# systemctl enable --now NetworkManager

2、命令行配置

2.1、ifconfig命令

查看及配置网卡参数(临时配置),不能查看基于配置文件的多IP地址的网络参数,默认不能查看到未启用的网卡列表

root用户可以使用该命令更改网卡参数,普通用户只能用户查看

语法:

ifconfig [接口]
ifconfig 接口 [aftype] options | address ...

选项:

选项作用
-a查看所有网卡的配置信息,包括未启用网卡
up启用网卡,ifconfig ens33 up,不会重新读取配置文件
down停用网卡,ifconfig ens33 down

案例:

临时配置网卡参数

 [root@localhost ~]# ifconfig
 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:c5:1f:fe  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:b3:fd:17  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens33 192.168.115.136/24
[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.115.136  netmask 255.255.255.0  broadcast 192.168.115.255
        ether 00:0c:29:c5:1f:fe  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1218 (1.1 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 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:b3:fd:17  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


 

注意:临时配置,重启网络服务、网卡启停、主机重启都将失效

2.2、ifup和ifdown

ifup ens33 
#启用网卡,并读取配置文件
ifdown ens33
#停用网卡

子接口配置

  • 基于物理网卡的逻辑接口

  • 可以使系统基于一块网卡配置多个IP地址

ifconfig 网卡名:编号(上限999999999) IP地址/子网掩码的网络位长度 【临时配置】

生成子接口配置文件【永久配置】,不能使用DHCP获得地址

[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# ls -al
总用量 8
drwxr-xr-x. 2 root root   25 5月  21 13:37 .
drwxr-xr-x. 5 root root 4096 5月  21 13:37 ..
-rw-r--r--. 1 root root  270 5月  21 13:37 ifcfg-ens33
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# ls -al
总用量 12
drwxr-xr-x. 2 root root   46 5月  26 08:51 .
drwxr-xr-x. 5 root root 4096 5月  21 13:37 ..
-rw-r--r--. 1 root root  270 5月  21 13:37 ifcfg-ens33
-rw-r--r--. 1 root root  270 5月  26 08:51 ifcfg-ens33:0

vim ifcfg-ens37:0
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.115.214
PREFIX=24

                                                                                                        
                  
~                                                                                                        
-- 插入 --    
ifdown ens37:0 && ifup ens37:0

2.3、ip a[ddress]命令

查看网络参数,可以查看到未启用的网卡列表,能够查看基于配置文件实现的多IP地址配置后的网络参数

语法:

ip [ OPTIONS ] OBJECT { COMMAND | help }

选项:

选项作用
address查看设备IP地址

案例:

ip a

2.2、配置文件配置

  • /etc/sysconfig/network-scripts/ifcfg-网卡名

  • 永久配置,需要网卡重新读取配置文件

  • 网卡启停操作

#启动服务
systemctl  start  network
#停止服务
systemctl  stop  network
#重启服务
systemctl  restart  network
#使用命令行重新加载配置文件
ifup  网卡名
ifdown 网卡名
#使用命令行启停网卡,但是不加载配置文件
ifconfig  网卡名  up | down

2.2.1、单一IP地址配置

  • 自动获得,必须在当前网络中存在DHCP服务器。

TYPE=Ethernet
#网卡参数的类型,Ethernet:以太网
BOOTPROTO=dhcp
#获得IP地址的方式,dhcp|static|none
NAME=ens33
#网卡描述名称,可有可无
DEVICE=ens33
#网卡设备名称,必须与ifconfig看到的网卡名保持一致。不能书写不存在的网卡名
ONBOOT=yes
#是否启用配置文件,yes|no
  • 手动配置

TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.115.150
#IP地址
PREFIX=24
#子网掩码长度
#或者使用
NETMASK=255.255.255.0
GATEWAY=192.168.115.2
#默认网关,在同一系统下,不管几块网卡,建议配置一个默认网关。
DNS1=192.168.115.2
#域名解析服务器地址,不建议在多网卡的情况下书写DNS,因为后启动的网卡会覆盖原有的DNS服务器地址。

2.2.2、多IP地址配置

vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR1=192.168.115.150
PREFIX1=24
GATEWAY=192.168.115.2
DNS1=192.168.115.2
IPADDR2=192.168.115.151
PREFIX2=24
DNS2=192.168.115.3
DNS3=192.168.115.4root
[@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c5:1f:fe brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.116.150/24 brd 192.168.116.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.116.151/24 brd 192.168.116.255 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec5:1ffe/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:b3:fd:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c5:1f:08 brd ff:ff:ff:ff:ff:ff
    altname enp2s5
    inet6 fe80::20c:29ff:fec5:1f08/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]# cat ifcong-ens33
cat: ifcong-ens33: 没有那个文件或目录
[root@localhost network-scripts]# cat ifconfig-ens33
cat: ifconfig-ens33: 没有那个文件或目录
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR1=192.168.116.150
PREFIX1=24
GATEWAY=192.168.116.2
DNS1=192.168.116.2
IPADDR2=192.168.116.151
PREFIX2=24
DNS2=192.168.116.3
DNS3=192.168.116.4
[root@localhost network-scripts]# root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c5:1f:fe brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.116.150/24 brd 192.168.116.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.116.151/24 brd 192.168.116.255 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec5:1ffe/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:b3:fd:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c5:1f:08 brd ff:ff:ff:ff:ff:ff
    altname enp2s5
    inet6 fe80::20c:29ff:fec5:1f08/64 scope link 
       valid_lft forever preferred_lft foreverroot
[@localhost network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.116.2   0.0.0.0         UG    0      0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 ens37
192.168.116.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

  • 加载配置文件

systemctl restart network
#或者
ifdown  ens33 && ifup ens33
  • 验证参数

#IP地址
ifconfig | ip a
[root@localhost network-scripts]# root@localhost network-scripts]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00     inet 127.0.0.1/8 scope host lo        valid_lft forever preferred_lft forever     inet6 ::1/128 scope host         valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000     link/ether 00:0c:29:c5:1f:fe brd ff:ff:ff:ff:ff:ff     altname enp2s1     inet 192.168.116.150/24 brd 192.168.116.255 scope global ens33        valid_lft forever preferred_lft forever     inet 192.168.116.151/24 brd 192.168.116.255 scope global secondary ens33        valid_lft forever preferred_lft forever     inet6 fe80::20c:29ff:fec5:1ffe/64 scope link         valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000     link/ether 52:54:00:b3:fd:17 brd ff:ff:ff:ff:ff:ff     inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0        valid_lft forever preferred_lft forever 4: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000     link/ether 00:0c:29:c5:1f:08 brd ff:ff:ff:ff:ff:ff     altname enp2s5     inet6 fe80::20c:29ff:fec5:1f08/64 scope link         valid_lft forever preferred_lft foreverroot
#网关地址查看
route -n
[@localhost network-scripts]# route -n Kernel IP routing table Destination     Gateway         Genmask         Flags Metric Ref    Use Iface 0.0.0.0         192.168.116.2   0.0.0.0         UG    0      0        0 ens33 169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens33 169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 ens37 192.168.116.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33 192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
#DNS地址ne
cat /etc/resolv.conf
[root@localhost network-scripts]# cat ifcfg-ens33 TYPE=Ethernet BOOTPROTO=static NAME=ens33 DEVICE=ens33 ONBOOT=yes IPADDR1=192.168.116.150 PREFIX1=24 GATEWAY=192.168.116.2 DNS1=192.168.116.2 IPADDR2=192.168.116.151 PREFIX2=24 DNS2=192.168.116.3 DNS3=192.168.116.4

   

3、路由配置

route命令

route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由。

语法:

route [选项] [参数]

选项:

选项描述
-A设置地址类型;
-C打印将Linux核心的路由缓存;
-v详细信息模式;
-n直接显示数字形式的IP地址;
-enetstat格式显示路由表;
-net到一个网络的路由表;
-host到一个主机的路由表。

参数:

参数描述
add增加指定的路由记录;
del删除指定的路由记录;
target目的网络或目的主机;
gw设置默认网关;
mss设置TCP的最大区块长度(MSS),单位MB;
window指定通过路由表的TCP连接的TCP窗口大小;
dev路由记录所表示的网络接口。
route add -net 0/0 gw 192.168.100.254 dev ens34

实验:

1单一路由直连

#进入R1
[root@localhost ~]# systemctl disable --now NetworkManager
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.115.113
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.116.113
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens34 ; ifup ens34
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# vim /etc/sysctl.conf
插入:net.ipv4.ip_forward = 1
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
#将ens33设置为VMnet1(仅主机模式)、ens34设置为VMnet2(仅主机模式)
​
#进入虚拟机1
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.115.112
PREFIX=24
GATEWAY=192.168.115.113
DNS1=192.168.115.2
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
#将ens33设置为VMnet1(仅主机模式)
#进入虚拟机2
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.116.112
PREFIX=24
GATEWAY=192.168.116.113
DNS1=192.168.116.2
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
#将ens33设置为VMnet2(仅主机模式)

2两个路由连接

#进入R1
[root@localhost ~]# systemctl disable --now NetworkManager
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.115.114
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
[root@localhost network-scripts]# vim ifcfg-ens34
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.116.117
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens34 ; ifup ens34
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# vim /etc/sysctl.conf
插入:net.ipv4.ip_forward = 1
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
#将ens33设置为VMnet1(仅主机模式)、ens34设置为VMnet2(仅主机模式)
[root@localhost network-scripts]# route add -net 192.168.117.0/24 gw 192.168.116.114
​
#进入R2
[root@localhost ~]# systemctl disable --now NetworkManager
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.116.114
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
[root@localhost network-scripts]# vim ifcfg-ens34
TYPE=Ethernet
BOOTPROTO=static
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.117.114
PREFIX=24
​
[root@localhost network-scripts]# ifdown ens34 ; ifup ens34
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# vim /etc/sysctl.conf
插入:net.ipv4.ip_forward = 1
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
#将ens33设置为VMnet2(仅主机模式)、ens34设置为VMnet3(仅主机模式)
[root@localhost network-scripts]# route add -net 192.168.115.0/24 gw 192.168.116.117
​
#进入虚拟机1
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.115.112
PREFIX=24
GATEWAY=192.168.115.114
DNS1=192.168.115.2
​
#进入虚拟机2
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.116.113
PREFIX=24
GATEWAY=192.168.117.114
DNS1=192.168.117.2
​
[root@localhost network-scripts]# ifdown ens33 ; ifup ens33
#将ens33设置为VMnet3(仅主机模式)

二、网络连接查看

1、netstat

作用

  • 查看本地服务的网络监听状态

  • 查看客户端连接到本地服务的连接状态

语法

netstat  选项 (-anptu)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      797/rpcbind         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1481/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1224/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1220/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1498/master         
tcp        0      0 192.168.115.112:48884   202.202.1.140:80        ESTABLISHED 2462/python         
tcp        0      0 192.168.115.112:46794   210.28.130.3:80         TIME_WAIT   -                   
tcp        0      0 192.168.115.112:34178   210.44.151.62:80        TIME_WAIT   -                   
tcp        0      0 192.168.115.112:60438   124.70.125.153:80       TIME_WAIT   -                   
tcp        0      0 192.168.115.112:34186   210.44.151.62:80        TIME_WAIT   -                   
tcp        0      0 192.168.115.112:34688   59.72.66.10:80          TIME_WAIT   -                   
tcp        0      0 192.168.115.112:51620   39.155.141.16:80        TIME_WAIT   -                   
tcp        0      0 192.168.115.112:46796   210.28.130.3:80         TIME_WAIT   -                   
tcp        0      0 192.168.115.112:53846   44.238.81.22:443        ESTABLISHED 2092/geoclue        
tcp        1      0 192.168.115.112:49812   35.180.43.213:80        CLOSE_WAIT  2462/python         
tcp        0      0 192.168.115.112:46930   211.68.71.121:80        TIME_WAIT   -                   
tcp        0      0 192.168.115.112:34716   59.72.66.10:80          TIME_WAIT   -                   
tcp        0      0 192.168.115.112:49614   120.201.228.227:80      TIME_WAIT   -                   
tcp        0      0 192.168.115.112:56660   202.141.176.110:80      TIME_WAIT   -                   
tcp        0      0 192.168.115.112:49134   117.147.202.12:80       TIME_WAIT   -                   
tcp        0      0 192.168.115.112:56664   202.141.176.110:80      TIME_WAIT   -                   
tcp        0      0 192.168.115.112:51628   39.155.141.16:80        TIME_WAIT   -                   
tcp        0      0 192.168.115.112:53992   101.6.15.130:80         TIME_WAIT   -                   
tcp        0      0 192.168.115.112:54004   101.6.15.130:80         TIME_WAIT   -                   
tcp        0      0 192.168.115.112:49136   117.147.202.12:80       TIME_WAIT   -                   
tcp        0      0 192.168.115.112:48886   202.202.1.140:80        TIME_WAIT   -                   
tcp6       0      0 :::111                  :::*                    LISTEN      797/rpcbind         
tcp6       0      0 :::22                   :::*                    LISTEN      1224/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1220/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1498/master         
udp        0      0 0.0.0.0:966             0.0.0.0:*                           797/rpcbind         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1481/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1481/dnsmasq        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           797/rpcbind         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           795/avahi-daemon: r 
udp        0      0 0.0.0.0:55584           0.0.0.0:*                           795/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           852/chronyd         
udp6       0      0 :::966                  :::*                                797/rpcbind         
udp6       0      0 :::111                  :::*                                797/rpcbind         
udp6       0      0 ::1:323                 :::*                                852/chronyd   

选项

-n, --numeric 
​
显示数字形式地址而不是去解析主机、端口或用户名。
​
-a, --all
​
显示所有的监听或连接状态
​
-p, --program
​
显示连接所属进程的PID和名称。
​
-t,--tcp
​
显示TCP连接
​
-u,--udp
​
显示UDP连接
​
-l
​
查看监听

2、ss

作用

  • 查看本地服务的网络监听状态

  • 查看客户端连接到本地服务的连接状态

语法

ss  选项 (-anptuli)

选项

-n, --numeric 
​
显示数字形式地址而不是去解析主机、端口或用户名。
​
-a, --all
​
显示所有的监听或连接状态
​
-p, --program
​
显示连接所属进程的PID和名称。
​
-t,--tcp
​
显示TCP连接
​
-u,--udp
​
显示UDP连接
​
-i,--info
​
查看客户端连接到本地的状态
​
-l
​
查看监听

开启Linux服务的路由功能

vim /etc/sysctl.conf
​
内核参数修改配置文件
​
添加
​
net.ipv4.ip_forward = 1
​
sysctl -p
​
加载配置

3、bond绑定

将多个物理网卡进行排列组合,形成逻辑网卡,网卡的高可用

3.1、绑定模式

  • mode0(平衡负载模式):平时两块网卡均工作,且自动备援,但需要在与服务器本地 网卡相连的交换机设备上进行端口聚合来支持绑定技术。

  • mode1(自动备援模式):平时只有一块网卡工作,在它故障后自动替换为另外的网卡。

  • mode6(平衡负载模式):平时两块网卡均工作,且自动备援,无须交换机设备提供辅助支持。

3.2、绑定案例

服务器有两块网卡 ens33和ens36

ens34操作

TYPE=ethernet
​
BOOTPROTO=none
​
NAME=ens34
​
DEVICE=ens34
​
ONBOOT=yes
​
MASTER=bond0
​
SLAVE=yes
​

ens37操作

TYPE=ethernet
​
BOOTPROTO=none
​
NAME=ens37
​
DEVICE=ens37
​
ONBOOT=yes
​
MASTER=bond0
​
SLAVE=yes

bond0操作

TYPE=ethernet
​
BOOTPROTO=none
​
NAME=bond0
​
DEVICE=bond0
​
ONBOOT=yes
​
IPADDR=192.168.1.1
​
PREFIX=24
​
GATEWAY=192.168.1.254
​
DNS1=192.168.1.254

添加bond配置文件

vim /etc/modprobe.d/bond.conf
​
alias bond0 bonding
​
options bonding mode=6 miimon=200

关闭网络图形化服务

systemctl stop NetworkManager

重启网络服务

ifup ens34 && ifup ens37 && ifup bond0
​
systemctl restart network #重启会出现启动失败,但是bond绑定是OK的!!!
案例
[root@localhost network-scripts]# vim ifcfg-ens160
[root@localhost network-scripts]# cp ifcfg-ens160 ifcfg-bond0
[root@localhost network-scripts]# vim ifcfg-ens160
[root@localhost network-scripts]# cat ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=none
NAME=ens160
DEVICE=ens160
ONBOOT=yes
MASTER=bond0
SLAVE=yes
[root@localhost network-scripts]# cp ifcfg-ens160 ifcf
cp:是否覆盖'ifcfg-有线连接_1'? yes
[root@localhost network-scripts]# vim ifcfg-有线连接_1
[root@localhost network-scripts]# vim ifcfg-bond0
[root@localhost network-scripts]# cat ifcfg-bond0
TYPE=Ethernet
BOOTPROTO=static
NAME=bond0
DEVICE=bond0
ONBOOT=yes
IPADDR=192.168.74.131
PREFIX=24
GATEWAY=192.168.74.137
DNS1=192.168.74.7
[root@localhost network-scripts]# cat ifcfg-有线连接_1TYPE=Ethernet
BOOTPROTO=none
NAME=ens224
DEVICE=ens224
ONBOOT=yes
MASTER=bond0
SLAVE=yes
[root@localhost network-scripts]# systemctl stop NetworkManager
[root@localhost network-scripts]# ifdown ens160 && ifup ens160 ifdown ens224 && ifup ens224 && ifup bond0
WARN      : [ifdown] You are using 'ifdown' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifdown] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifdown] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
/etc/sysconfig/network-scripts/ifdown-eth:行129: /sys/class/net/bond0/bonding/slaves: 没有那个文件或目录
WARN      : [ifup] You are using 'ifup' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifup] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifup] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
WARN      : [ifup] You are using 'ifup' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifup] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifup] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
WARN      : [ifup] You are using 'ifup' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifup] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifup] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
WARN      : [ifup] You are using 'ifup' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifup] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifup] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
WARN      : [ifup] You are using 'ifup' script provided by 'network-scripts', which are now deprecated.
WARN      : [ifup] 'network-scripts' will be removed in one of the next major releases of RHEL.
WARN      : [ifup] It is advised to switch to 'NetworkManager' instead - it provides 'ifup/ifdown' scripts as well.
[root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:12:83:08 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
3: ens224: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:12:83:12 brd ff:ff:ff:ff:ff:ff
    altname enp19s0
    inet 192.168.74.133/24 brd 192.168.74.255 scope global dynamic noprefixroute ens224
       valid_lft 1604sec preferred_lft 1604sec
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:c5:18:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:12:83:08 brd ff:ff:ff:ff:ff:ff
    inet 192.168.74.131/24 brd 192.168.74.255 scope global bond0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe12:8308/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]# vim ifcfg-bond0
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:12:83:08 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
3: ens224: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:12:83:12 brd ff:ff:ff:ff:ff:ff
    altname enp19s0
    inet 192.168.74.133/24 brd 192.168.74.255 scope global dynamic noprefixroute ens224
       valid_lft 1519sec preferred_lft 1519sec
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:c5:18:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:12:83:08 brd ff:ff:ff:ff:ff:ff
    inet 192.168.74.131/24 brd 192.168.74.255 scope global bond0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe12:8308/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]# lsmod | grep bond
bonding               204800  0
tls                   110592  1 bonding
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.74.137  0.0.0.0         UG    0      0        0 bond0
169.254.0.0     0.0.0.0         255.255.0.0     U     1005   0        0 bond0
192.168.74.0    0.0.0.0         255.255.255.0   U     0      0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

4、Linux下的抓包工具

4.1tcpdump

语法

tcpdump  -i  网卡名

选项

-c  
​
当 收到多少个报文后退出
​
-i  
​
监听  interface.  如果 不指定 接口, tcpdump 在 系统 的 接口 清单 中,寻找 号码最小, 已经 配置好的 接口 (loopback 除外). 选中的时候会中断连接.
​
-n 
​
不要把地址转换成 名字 (指的是 主机地址, 端口号等)
​
-t 
​
禁止 显示 时戳标志.
​
-v  
​
(稍微多一点)  繁琐的输出.  例如,  显示  IP  数据报  中的  生存周期 和服务类型.
​
-e  
​
显示 链路层报头

额外参数

dst port portNumber
​
抓取目标端口为portNumber的报文
​
port portNumber
​
抓取端口为portNumber的报文
​
src port portNumber
​
抓取源端口为portNumber的报文

案例

抓取客户端访问本机80号端口的1000个报文

tcpdump -tn dst port 80 -c 1000 -i bond0 
[root@localhost network-scripts]# tcpdump -tn dst port 80 -c 1000 -i bond0 
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on bond0, link-type EN10MB (Ethernet), capture size 262144 bytes
4.2Wireshark

5、web压力测试工具

ab -c 100 -n 2000 http://192.168.1.254/

6、路由追踪命令

traceroute  目标地址或域名

优化网络配置,提升用户体验。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值