Linux_网络基础管理
阅读原文时间:2023年07月09日阅读:4

一、网卡的命名

1、传统网卡命名

  • eth0、eth1、eth2、eth3………
  • wlan0、wlan1、waln2、wlan3………

2、RHEL7命名机制

  • systemd对网络设备的命名方式:

    • 如果firmwareBIOS为主板上集成的设备提供的索引信息可用,且可预测, 则根据此索引进行命名,例如:eno1
    • 如果firmwareBIOSPCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如:ens1
    • 如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如:enp2s0
    • 如果用户显式启动,也可根据MAC地址进行命名,例如:enx2387a1dc56
    • 上述均不可用时,则使用传统命名机制
  • 上述命名机制中,有的需要biosdevname程序的参与,所以必须安装biosdevname程序且启用它。

3、网卡接口名称组成

  • 基于固件、 设备结构、设备类型三种方式:

    • 由两个字母开头标示固件

      • 以太网网卡以 en开头
      • 无线网卡以wl开头
    • 设备结构

      • o:主板上集成的设备的设备索引号
      • s:扩展槽的索引号
      • x:基于MAC地址的命名
      • ps:基于物理位置拓扑的命名。如:enp2s1,表示PCI总线上第2个总线的第1个插槽的设备索引号

4、重设传统网卡命名

  • 修改网卡配置文件

//修改网卡配置文件
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens160
[root@localhost network-scripts]# mv ifcfg-ens160 ifcfg-eth0
[root@localhost network-scripts]# ls
ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0 //把ens160换成eth0
UUID=cf57c3f1-7eaf-489c-86d4-2ebbc39f6105
DEVICE=eth0 //把ens160换成eth0
ONBOOT=yes

  • 编辑/etc/default/grub配置文件,在以GRUB_CMDLINE_LINUX开头的行内rhgb的前面加上net.ifnames=0 biosdevname=0

[root@localhost ~]# vim /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true  

  • grub2生成配置文件

    //为grub2生成配置文件
    [root@localhost ~]# grub2-mkconfig -o /etc/grub2.cfg (-o:outpu更新配置文件更新到/etc/grub2.cfg)
    Generating grub configuration file …
    done
    [root@localhost ~]# reboot
    //生成配置文件后需要重启  

  • 查看网卡名称

//之前的网卡名称
1: lo: 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: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global dynamic noprefixroute ens160
valid_lft 1750sec preferred_lft 1750sec
inet6 fe80::dc83:4685:5028:2c09/64 scope link noprefixroute
valid_lft forever preferred_lft forever

//重设网卡名之后
[root@localhost ~]# ip a
1: lo: 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: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever

二、网络管理常用的命令

1、ifconfig命令(用于查看当前处于活跃状态的网络接口)

  • ifconfig —— 命令

[root@localhost ~]# ifconfig
eth0: flags=4163 mtu 1500
inet 192.168.174.128 netmask 255.255.255.0 broadcast 192.168.174.255
inet6 fe80::6cda:49cd:383e:cfa3 prefixlen 64 scopeid 0x20
ether 00:0c:29💿6a:1b txqueuelen 1000 (Ethernet)
RX packets 95 bytes 9605 (9.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 93 bytes 10685 (10.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 12 bytes 1020 (1020.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 1020 (1020.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

  • 仅查看eth0网卡的状态信息

[root@localhost ~]# ifconfig eth0
eth0: flags=4163 mtu 1500
inet 192.168.174.128 netmask 255.255.255.0 broadcast 192.168.174.255
inet6 fe80::6cda:49cd:383e:cfa3 prefixlen 64 scopeid 0x20
ether 00:0c:29💿6a:1b txqueuelen 1000 (Ethernet)
RX packets 111 bytes 10847 (10.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 108 bytes 12827 (12.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

  • 查看所以网卡状态信息(包括禁用和启用的)

[root@localhost ~]# ifconfig -a
eth0: flags=4163 mtu 1500
inet 192.168.174.128 netmask 255.255.255.0 broadcast 192.168.174.255
inet6 fe80::6cda:49cd:383e:cfa3 prefixlen 64 scopeid 0x20
ether 00:0c:29💿6a:1b txqueuelen 1000 (Ethernet)
RX packets 134 bytes 12673 (12.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 124 bytes 14771 (14.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 12 bytes 1020 (1020.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 1020 (1020.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

  • 网卡配置信息含义

UP: 网卡处于活动状态
BROADCAST: 支持广播
RUNNING: 网线已接入
MULTICAST: 支持组播
MTU: 最大传输单元(字节) ,即此接口一次所能传输的最大封包

inet: 显示IPv4地址行
inet6: 显示IPv6地址行
link/enther: 指设备硬件(MAC )地址
txqueuelen: 传输缓存区长度大小
RX packets: 接收的数据包
TX packets: 发送的数据包
errors: 总的收包的错误数量
dropped: 由于各种原因,导致拷贝在内存过程中被丢弃
collisions: 网络信号冲突情况,值不为0则可能存在网络故障

2、ip —— 命令

  • 语法:

ip [ option ] object { command | help }
object :
link:网络接口
addr:协议地址
route:路由

  • 查看网络接口所有地址

[root@localhost ~]# ip link show
1: lo: 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
2: eth0: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff

  • 显示报文统计信息

[root@localhost ~]# ip -s link show
1: lo: 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
RX: bytes packets errors dropped overrun mcast
1020 12 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1020 12 0 0 0 0
2: eth0: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
21460 245 0 0 0 0
TX: bytes packets errors dropped carrier collsns
21963 202 0 0 0 0

  • 启用或禁用网络接口

语法:ip link set devname { up | down}

[root@localhost ~]# ip link set lo down
[root@localhost ~]# ip link set lo up   

  • 添加ip地址

语法:ip addr add ip/prefix dev devname

[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.174.150/24 dev eth0
[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.174.150/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever 

  • 删除ip地址

语法:ip addr del ip/prefix dev devname

[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.174.150/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr del 192.168.174.150/24 dev eth0
[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever  

  • 查看网络接口地址

语法:ip addr show devname

[root@localhost ~]# ip addr show
[root@localhost ~]# ip addr show eth0
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29💿6a:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.174.128/24 brd 192.168.174.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6cda:49cd:383e:cfa3/64 scope link noprefixroute
valid_lft forever preferred_lft forever

3、route —— 命令

Linux主机之间是使用IP进行通信,假设A主机和B主机同在一个网段内且网卡都处于激活状态,则A具备和B直接通信的能力,但如果A主机和B主机处于两个不同的网段,则A必须通过路由器才能和B通信,路由器属于IT设备的基础设施,每个网段 都应该至少有一个网关

  • 查看当前路由表(有的默认安装了route命令;最小化安装的没有route命令,需要手动安装net-tools安装包)

[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

  • 以数字方式显示各主机或端口等相关信息

[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

  • 增加路由

语法:route add [ net | host ] ip/prefix gw dev devname (net:主机路由;host:主机路由)

[root@localhost ~]# route add -net 192.168.1.0/24 gw 192.168.174.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 100 0 0 eth0
192.168.1.0 192.168.174.2 255.255.255.0 UG 0 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
[root@localhost ~]# route add -net 0.0.0.0/0 gw 192.168.174.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.174.2 0.0.0.0 UG 100 0 0 eth0 (这条路由条目是原本就有的)
192.168.1.0 192.168.174.2 255.255.255.0 UG 0 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 

  • 增加主机路由

[root@localhost ~]# route add -host 192.168.174.150 gw 192.168.174.2
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.174.150 192.168.174.2 255.255.255.255 UGH 0 0 0 eth0

[root@localhost ~]# route del -host 192.168.174.150
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

三、网路相关配置文件

1、网络配置文件

网络配置文件:/etc/sysconfig/network
NETWORKING= [ yes | no ]:设置整个系统是否启用网络功能,若设为no,则无论网卡如何设置都不难使用网络功能
HOSTNAME:设置主机名

2、网卡接口配置文件

网卡接口的配置文件路径:/etc/sysconfig/network-scripts/ifcfg-devname

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
UUID=cf57c3f1-7eaf-489c-86d4-2ebbc39f6105
DEVICE=eth0
ONBOOT=yes

3、网卡配置说明

BOOTPROTO=none //引导协议,可选值有{static /none / dhcp /bootp}
NM_ CONTROLLED //NM是NetworkManager的简写,可选值有{yes/no}
TYPE //接口类型;常见的接口类型有: Etherdet, Bridge
UUID //设备的惟一标识
HWADDR //硬件地址,要与硬件中的地址保持一-致,可省
IPADDR=172.16.12.130 //固定IP地址
PREFIX=24 //子网掩码
NETMASK=255.255.255.0 //子网掩码
GATEWAY=172.16.12.2 //默认网关
DNS1=172.16.12.2 //第一个DNS服务器指向
DNS2 //第二个DNS服务器指向
DNS3 //第三个DNS服务器指向
DEVICE=eth1 //关联的设备名称,要与文件名的后半部INTERFACE_ NAME"保持一致
NAME="eth1" //连接名称
ONB00T=yes //在系统引导时是否自动激活此网络接口,可选值有{ yes / no }
DEFROUTE=yes //将接口设定为默认路由{ yes |no }
USERCTL={yes |no} //是否允许普通用户控制此接口的启用与禁用
PEERDNS={yes |no} //是否在BOOTPROTO为dhcp时接受由dhcp服务器指定的DNS地址 

4、路由配置文件

  • 配置文件路径

/etc/sysconfig/network-scripts/route-eth0(route-网卡设备名称)

  • 添加路由方式一:(方式一是最常用的、最简单明了、个人推荐的使用的方法;其中via指的是下一跳)

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim route-eth0
192.168.11.0/24 via 192.168.174.2
192.168.12.0/24 via 192.168.174.2
192.168.13.0/24 via 192.168.174.2
[root@localhost network-scripts]# ifdown eth0;ifup eth0
成功停用连接 "eth0"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/19)

连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/21)
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.11.0 192.168.174.2 255.255.255.0 UG 100 0 0 eth0
192.168.12.0 192.168.174.2 255.255.255.0 UG 100 0 0 eth0
192.168.13.0 192.168.174.2 255.255.255.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

  • 添加路由方式二:(方式二相对方式一比较麻烦,个人不推荐使用方式二)

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim route-eth0
ADDRESS0=192.168.1.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.174.2

ADDRESS1=192.168.2.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.174.2
[root@localhost network-scripts]# ifdown eth0;ifup eth0
成功停用连接 "eth0"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/17)

连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/19)
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 192.168.174.2 255.255.255.0 UG 100 0 0 eth0
192.168.2.0 192.168.174.2 255.255.255.0 UG 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0  

  • 添加到主机的路由:(添加到主机指的是只能与该主机通信)

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim route-eth0
192.168.1.1/32 via 192.168.174.2
192.168.2.2/32 via 192.168.174.2
192.168.3.3/32 via 192.168.174.2
[root@localhost network-scripts]# ifdown eth0;ifup eth0
成功停用连接 "eth0"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/21)

连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/23)
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.1 192.168.174.2 255.255.255.255 UGH 100 0 0 eth0
192.168.2.2 192.168.174.2 255.255.255.255 UGH 100 0 0 eth0
192.168.3.3 192.168.174.2 255.255.255.255 UGH 100 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

5、DNS配置文件

  • DNS配置文件路径

/etc/resolv.conf

  • 查看配置文件

[root@localhost ~]# vim /etc/resolv.conf

Generated by NetworkManager

nameserver 8.8.8.8

//查看网卡的dns
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
UUID=cf57c3f1-7eaf-489c-86d4-2ebbc39f6105
DEVICE=eth0
ONBOOT=yes
DNS1=8.8.8.8
IPADDR=192.168.174.128
PREFIX=24
//只需更改网卡里面的的DNS,不需要更改/etc/resolv.conf里面的内容  

四、NetworkManager管理网络

1、NetworkManager基本概念

1️⃣:RHEL/CentOS7系统默认使用NetworkManager来提供网络服务,这是一种动态管理网络配置的守护进程,能够让网络设备保持连接状态。

2️⃣:NetworkManager提供的命令行和图形配置工具对网络进行设定,设定保存的配置文件。

3️⃣:在/etc/sysconf ig/network-scripts目录下,工具有nmcli, nmtui, nm- connect ion-editor

4️⃣:device物理设备,例如ens33 , enp2s0, virbr0, team0

5️⃣:connection连接设置,具体网络配置方案:

  • 不同的网络连接配置可以应用到相同的物理设备,但物理设备同一时间只能应用其中某个网络连接

  • 针对物理网络接口,设定不同的网络连接,在不同的使用环境中激活相应的网络连接,就可以实现网络配置信息的自动切换了

2、查看NetworkManager状态:

[root@localhost ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-08-18 05:32:03 CST; 38min ago
Docs: man:NetworkManager(8)
Main PID: 5188 (NetworkManager)
Tasks: 3 (limit: 12356)
Memory: 4.4M
CGroup: /system.slice/NetworkManager.service
└─5188 /usr/sbin/NetworkManager --no-daemon

8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3406] device (eth0): Activation: starting connection 'eth0' (cf57c3f1-7eaf-489c-86d4-2ebbc39f6105)
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3422] device (eth0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3425] manager: NetworkManager state is now CONNECTING
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3427] device (eth0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3433] device (eth0): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3456] device (eth0): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3593] device (eth0): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3596] device (eth0): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3599] manager: NetworkManager state is now CONNECTED_LOCAL
8月 18 05:58:40 localhost.localdomain NetworkManager[5188]: [1597701520.3669] device (eth0): Activation: successful, device activated.  

3、使用nmcli命令查看设备以及连接状态

  • 查看设备状态

[root@localhost ~]# nmcli device
DEVICE TYPE STATE CONNECTION
eth0 ethernet 已连接 eth0
lo loopback 未托管 --   

  • 查看所有网卡设备信息

[root@localhost ~]# nmcli device show
GENERAL.DEVICE: eth0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:CD:6A:1B
GENERAL.MTU: 1500
GENERAL.STATE: 100(已连接)
GENERAL.CONNECTION: eth0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/25
WIRED-PROPERTIES.CARRIER: 开
IP4.ADDRESS[1]: 192.168.174.128/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 192.168.174.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]: dst = 192.168.1.1/32, nh = 192.168.174.2, mt = 100
IP4.ROUTE[3]: dst = 192.168.2.2/32, nh = 192.168.174.2, mt = 100
IP4.ROUTE[4]: dst = 192.168.3.3/32, nh = 192.168.174.2, mt = 100
IP4.DNS[1]: 8.8.8.8
IP6.ADDRESS[1]: fe80::6cda:49cd:383e:cfa3/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255

GENERAL.DEVICE: lo
GENERAL.TYPE: loopback
GENERAL.HWADDR: 00:00:00:00:00:00
GENERAL.MTU: 65536
GENERAL.STATE: 10(未托管)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
IP4.ADDRESS[1]: 127.0.0.1/8
IP4.GATEWAY: --
IP6.ADDRESS[1]: ::1/128
IP6.GATEWAY: --

  • 查看指定设备信息

[root@localhost ~]# nmcli device show eth0
GENERAL.DEVICE: eth0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:CD:6A:1B
GENERAL.MTU: 1500
GENERAL.STATE: 100(已连接)
GENERAL.CONNECTION: eth0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/25
WIRED-PROPERTIES.CARRIER: 开
IP4.ADDRESS[1]: 192.168.174.128/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 192.168.174.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]: dst = 192.168.1.1/32, nh = 192.168.174.2, mt = 100
IP4.ROUTE[3]: dst = 192.168.2.2/32, nh = 192.168.174.2, mt = 100
IP4.ROUTE[4]: dst = 192.168.3.3/32, nh = 192.168.174.2, mt = 100
IP4.DNS[1]: 8.8.8.8
IP6.ADDRESS[1]: fe80::6cda:49cd:383e:cfa3/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255  

  • 查看网卡连接状态

[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
eth0 cf57c3f1-7eaf-489c-86d4-2ebbc39f6105 ethernet eth0
有线连接 1 a026ad4d-b586-32c4-908a-0501f9f2b522 ethernet --

  • 查看指定网卡连接信息

[root@localhost ~]# nmcli connection show eth0
connection.id: eth0
connection.uuid: cf57c3f1-7eaf-489c-86d4-2ebbc39f6105
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: eth0
connection.autoconnect: 是
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0(default)
connection.auth-retries: -1
connection.timestamp: 1597702924
connection.read-only: 否
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1(default)
………..  

五、网络管理常用命令

1、ping —— 命令

  • 目的:测试另一台主机是否可达,如果ping不到某台主机,就说明对方主机以及出现了问题,但不排除由于防火墙的原因、ICMP包被丢弃等原因造成ping不通的情况。
  • 常用命令:

ping命令常用选项:
-c :指定ping包的次数
-i :指定ping包发送间隔
-w :如果pingh'm没有回应。则在指定超时后退出

  • 实例:

    //指定ping包次数
    [root@localhost ~]# ping -c 3 www.baidu.com
    PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=25.10 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=25.4 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=23.3 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 5ms
rtt min/avg/max/mdev = 23.331/24.910/25.977/1.154 ms

//指定ping包的时间间隔
[root@localhost ~]# ping -i 2 -c 3 www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=23.8 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=24.8 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=21.9 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 10ms
rtt min/avg/max/mdev = 21.906/23.477/24.759/1.182 ms

2、host命令和nslookup命令

  • 作用:hostnslookup命令都是用于查询DNS记录的
  • 实例:

[root@localhost ~]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.38
www.a.shifen.com has address 14.215.177.39

[root@localhost ~]# nslookup www.baidu.com
Server: 192.168.174.2
Address: 192.168.174.2#53

Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 14.215.177.39
Name: www.a.shifen.com
Address: 14.215.177.38

3、netstat命令(查看网络状态;最小化安装默认是没有装netstat命令,需要手动安装net-tools安装包)

  • 查看路由表

[root@localhost ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default _gateway 0.0.0.0 UG 0 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

  • 以数字方式显示路由表

[root@localhost ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.174.2 0.0.0.0 UG 0 0 0 eth0
192.168.174.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

  • 查看所以状态连接

[root@localhost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost.localdoma:ssh 192.168.174.1:9531 ESTABLISHED
tcp 0 0 localhost.localdoma:ssh 192.168.174.1:12204 ESTABLISHED
tcp 0 208 localhost.localdoma:ssh 192.168.174.1:4293 ESTABLISHED
tcp6 0 0 [::]:ssh [::]:* LISTEN
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (servers and established)
……….

  • 显示监听指定的套接字的进程号及进程名

[root@localhost ~]# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 localhost.localdoma:ssh 192.168.174.1:9531 ESTABLISHED 1220/sshd: root [pr
tcp 0 0 localhost.localdoma:ssh 192.168.174.1:12204 ESTABLISHED 1412/sshd: root [pr
tcp 0 36 localhost.localdoma:ssh 192.168.174.1:4293 ESTABLISHED 5011/sshd: root [pr
Active UNIX domain sockets (w/o servers)
…………

  • 常用选项:-antlp

[root@localhost ~]# netstat -antlp
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:22 0.0.0.0:* LISTEN 1010/sshd
tcp 0 0 192.168.174.128:22 192.168.174.1:9531 ESTABLISHED 1220/sshd: root [pr
tcp 0 0 192.168.174.128:22 192.168.174.1:12204 ESTABLISHED 1412/sshd: root [pr
tcp 0 36 192.168.174.128:22 192.168.174.1:4293 ESTABLISHED 5011/sshd: root [pr
tcp6 0 0 :::22 :::* LISTEN 1010/sshd

4、ss命令(网络状态查看工具,与netstat命令差不多)

  • 语法:ss [ option ] [ FILTER ]
  • 常用的选项:

options:
-t: tcp协议相关-u: udp协议相关-W:裸套接字相关-x: unix套接字相关-l: listen状态的连接-a:所有
-n:数字格式
-p:相关的程序及pid-e:扩展的信息
-m:内存用量
-0:显示计时器信息  

  • 常见的FILTER

FILTERs:
FILTER := [ state TCP-STATE] [ EXPRESSION ]
如: ss -antlp state ESTABLISHED

  • 实例:

[root@localhost ~]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1010,fd=4))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1010,fd=6))

[root@localhost ~]# ss -antlp state ESTABLISHED
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 192.168.174.128:22 192.168.174.1:9531 users:(("sshd",pid=1233,fd=5),("sshd",pid=1220,fd=5))
0 0 192.168.174.128:22 192.168.174.1:12204 users:(("sshd",pid=1414,fd=5),("sshd",pid=1412,fd=5))
0 36 192.168.174.128:22 192.168.174.1:4293 users:(("sshd",pid=5013,fd=5),("sshd",pid=5011,fd=5))

  • 常见的state:

常见的state:
tcp finite state machine: 有限状态机
LISTENING:监听
ESTABLISHED:已建立的连接
EXPRESSION:
dport =
sport =

示例: '( dport = :ssh or sport = :ssh)',此处的ssh也即服务名可以使用其对应的端口号代替

  • 实例:

[root@localhost ~]# ss -antlp state ESTABLISHED '( dport = :ssh or sport = :ssh )'
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 192.168.174.128:22 192.168.174.1:9531 users:(("sshd",pid=1233,fd=5),("sshd",pid=1220,fd=5))
0 0 192.168.174.128:22 192.168.174.1:12204 users:(("sshd",pid=1414,fd=5),("sshd",pid=1412,fd=5))
0 36 192.168.174.128:22 192.168.174.1:4293 users:(("sshd",pid=5013,fd=5),("sshd",pid=5011,fd=5))

  • 常见端口:

http 80/tcp
httpd 443/tcp
ssh 22/tcp
ftp 20,21/tcp
mysql 3306/tc[
rsync 873/rsync
redis 6379/tcp

Windows远程:3389/tcp
dhcp 67/tcp
dns 53/tcp
MSSQL 1433/tcp
tomcat 8080
postfix 25