1.概述

CentOS 8 中,使用 systemctl restart network 命令重启网络服务可能会报错,因为CentOS 8 已经不再使用 network 服务管理网络。相反,它使用 NetworkManager 服务管理网络。

[root@centos8 ~]#vi /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=ed245c0e-01ab-4d8e-871f-cf52ac79347f
DEVICE=ens160
ONBOOT=yes
IPADDR=10.0.0.10
GATEWAY=10.0.0.2
NETMASK=255.255.255.0
DNS1=114.114.114.114

关于网卡的配置文件,各个参数的含义如下:
TYPE:网卡类型。
PROXY_METHOD:代理方法。
BROWSER_ONLY:是否仅用于浏览器。
BOOTPROTO:网卡的 IP 地址获取方式。static 表示手动配置,而 dhcp 表示使用 DHCP 协议自动获取。
DEFROUTE:是否将此网卡设为默认路由。
IPV4_FAILURE_FATAL:IPv4 连接失败是否视为致命错误。
IPV6INIT:是否初始化 IPv6。
IPV6_AUTOCONF:是否使用自动配置获取 IPv6 地址。
IPV6_DEFROUTE:是否将此网卡设为默认 IPv6 路由。
IPV6_FAILURE_FATAL:IPv6 连接失败是否视为致命错误。
NAME:网卡名称。
UUID:网卡的唯一标识符。
DEVICE:网卡设备名称。
ONBOOT:是否在启动时启用此网卡。
IPADDR:静态 IP 地址。
GATEWAY:默认网关。
NETMASK:子网掩码。
DNS1:首选 DNS 服务器。

2.方式一:使用 NetworkManager 服务

在上面修改完网卡的配置文件以后,使用 :wq 进行保存退出,然后使用如下的命令来重启网卡

要重启 NetworkManager 服务,您可以使用以下命令:
[root@centos8 ~]#systemctl restart NetworkManager

如果想检查 NetworkManager 服务的状态,可以使用以下命令:
[root@centos8 ~]#systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2023-01-08 15:45:06 CST; 57s ago
     Docs: man:NetworkManager(8)
 Main PID: 1824 (NetworkManager)
    Tasks: 3 (limit: 12253)
   Memory: 3.0M
   CGroup: /system.slice/NetworkManager.service
           └─1824 /usr/sbin/NetworkManager --no-daemon

Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0678] device (ens160): state change: config -> ip-config (reason 'none', sys-iface-state: 'assume')
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0681] device (ens160): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'assume>
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0696] device (ens160): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'assu>
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0698] device (ens160): state change: secondaries -> activated (reason 'none', sys-iface-state: 'ass>
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0700] manager: NetworkManager state is now CONNECTED_LOCAL
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0702] manager: NetworkManager state is now CONNECTED_SITE
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0703] policy: set 'ens160' (ens160) as default for IPv4 routing and DNS
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0726] device (ens160): Activation: successful, device activated.
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0747] manager: NetworkManager state is now CONNECTED_GLOBAL
Jan 08 15:45:07 centos8.xingyuyu.com NetworkManager[1824]: <info>  [1673163907.0749] manager: startup complete
[root@centos8 ~]#

如果执行完上述命令以后,使用 ifconfig 命令查看IP地址仍然是修改之前的地址,但是使用 nmcli 命令查看网卡的 IP 地址显示正确,则可能是因为系统缓存了旧的地址。

[root@centos8 ~]#ifconfig 
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fecd:edb8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:cd:ed:b8  txqueuelen 1000  (Ethernet)
        RX packets 3347  bytes 276213 (269.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2027  bytes 390939 (381.7 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

[root@centos8 ~]#nmcli connection show ens160 
connection.id:                          ens160
connection.uuid:                        ed245c0e-01ab-4d8e-871f-cf52ac79347f
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              ens160
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1673164388
connection.read-only:                   no
connection.permissions:                 --
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 --
connection.gateway-ping-timeout:        0
connection.metered:                     unknown
connection.lldp:                        default
connection.mdns:                        -1 (default)
connection.llmnr:                       -1 (default)
connection.wait-device-timeout:         -1
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          no
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist:   --
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        --
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            --
802-3-ethernet.wake-on-lan:             default
802-3-ethernet.wake-on-lan-password:    --
802-3-ethernet.accept-all-mac-addresses:-1 (default)
ipv4.method:                            manual
ipv4.dns:                               114.114.114.114
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.addresses:                         10.0.0.10/24
ipv4.gateway:                           10.0.0.2
ipv4.routes:                            --
ipv4.route-metric:                      -1
ipv4.route-table:                       0 (unspec)
ipv4.routing-rules:                     --
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-iaid:                         --
ipv4.dhcp-timeout:                      0 (default)
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.dhcp-fqdn:                         --
ipv4.dhcp-hostname-flags:               0x0 (none)
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv4.required-timeout:                  -1 (default)
ipv4.dad-timeout:                       -1 (default)
ipv4.dhcp-vendor-class-identifier:      --
ipv4.dhcp-reject-servers:               --
ipv6.method:                            auto
ipv6.dns:                               --
ipv6.dns-search:                        --
ipv6.dns-options:                       --
ipv6.dns-priority:                      0
ipv6.addresses:                         --
ipv6.gateway:                           --
ipv6.routes:                            --
ipv6.route-metric:                      -1
ipv6.route-table:                       0 (unspec)
ipv6.routing-rules:                     --
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.required-timeout:                  -1 (default)
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.addr-gen-mode:                     eui64
ipv6.ra-timeout:                        0 (default)
ipv6.dhcp-duid:                         --
ipv6.dhcp-iaid:                         --
ipv6.dhcp-timeout:                      0 (default)
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
ipv6.dhcp-hostname-flags:               0x0 (none)
ipv6.token:                             --
proxy.method:                           none
proxy.browser-only:                     no
proxy.pac-url:                          --
proxy.pac-script:                       --
GENERAL.NAME:                           ens160
GENERAL.UUID:                           ed245c0e-01ab-4d8e-871f-cf52ac79347f
GENERAL.DEVICES:                        ens160
GENERAL.IP-IFACE:                       ens160
GENERAL.STATE:                          activated
GENERAL.DEFAULT:                        yes
GENERAL.DEFAULT6:                       no
GENERAL.SPEC-OBJECT:                    --
GENERAL.VPN:                            no
GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/1
GENERAL.ZONE:                           --
GENERAL.MASTER-PATH:                    --
IP4.ADDRESS[1]:                         10.0.0.100/24
IP4.GATEWAY:                            10.0.0.2
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 10.0.0.2, mt = 100
IP4.ROUTE[2]:                           dst = 10.0.0.0/24, nh = 0.0.0.0, mt = 100
IP4.DNS[1]:                             114.114.114.114
IP6.ADDRESS[1]:                         fe80::20c:29ff:fecd:edb8/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
[root@centos8 ~]#

为什么会会有这种情况呢?这是因为系统会缓存 IP 地址是为了加快网络连接的速度。当我们在修改网卡的 IP 地址后,系统会将新的地址保存在配置文件中,但是会将旧的地址缓存在内存中,以便在下次连接时快速访问。

遇到这种情况怎么解决呢?在某些情况下,当我们修改了网卡的 IP 地址后,系统可能会继续使用缓存的地址,而不是新的地址。这时,您可以使用 ifdown 和 ifup 命令来更新缓存的地址。

[root@centos8 ~]#ifdown ens160 
[root@centos8 ~]#ifup ens160 
[root@centos8 ~]#ifup ens160 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

使用ifconfig 查看ip地址已经生效了

3.方式二:直接使用ifdown和ifup命令

使用 方式一 可能会有问题,我们可以直接使用ifdownifup 命令来重启网卡,使得网卡直接生效

[root@centos8 ~]#ifdown ens160 
[root@centos8 ~]#ifup ens160 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

4.方式三:使用nmcli命令

这种方式不用修改配置文件,直接使用命令来进行配置IP地址,并且配置文件会自动修改ip地址

用法如下:
[root@centos8 ~]#nmcli connection down ens160
[root@centos8 ~]#nmcli connection modify ens160 ipv4.addresses [IPADDR]/[PREFIX]
[root@centos8 ~]#nmcli connection up ens160

[root@centos8 ~]#nmcli connection down ens160
[root@centos8 ~]#nmcli connection modify ens160 ipv4.addresses 10.0.0.120/24
[root@centos8 ~]#nmcli connection up ens160

这时,我们去看一下配置文件是否有变化

[root@centos8 ~]#vi /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=ed245c0e-01ab-4d8e-871f-cf52ac79347f
DEVICE=ens160
ONBOOT=yes
IPADDR=10.0.0.120
GATEWAY=10.0.0.2                                                
NETMASK=255.255.255.0
DNS1=114.114.114.114
PREFIX=24
BOOTPROTO=none

我们可以看到IPADDR修改了,并且多了两列

PREFIX=24
BOOTPROTO=none

PREFIX 指的是子网掩码的前缀长度,用于表示网络中可用的 IP 地址数量。
BOOTPROTO 指定网卡的 IP 地址获取方式。如果将 BOOTPROTO 设置为 "none",则表示手动配置网卡的 IP 地址。这意味着需要在配置文件中手动指定 IP 地址(即 IPADDR 参数)和子网掩码(即 NETMASK 参数)。
如果将 BOOTPROTO 设置为 "dhcp",则表示使用 DHCP 协议自动获取 IP 地址。这意味着无需在配置文件中手动指定 IP 地址和子网掩码,系统会自动使用 DHCP 服务器分配地址。

使用ifconfig命令查看发现ip地址已经修改成当前的地址。

5.使用nmcli命令重新加载配置文件

CentOS8中,当我们修改网卡配置文件(/etc/sysconfig/network-scripts/ifcfg-eth1)以后,使用传统的命令(service network restart或者systemctl restartnetwork)已经无效了。

#使用nmcli查看网络连接,这个命令会显示系统中所有的网络连接,包括活动的和非活动的连接
[root@centos8 network-scripts]#nmcli connection show
NAME                UUID                                  TYPE      DEVICE
eth0                5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0
Wired connection 1  bd3d361f-bd4d-3778-ad0b-2a5a957bfcc4  ethernet  eth1
[root@centos8 network-scripts]

#重新加载网卡配置文件
[root@centos8 network-scripts]#nmcli connection reload
[root@centos8 network-scripts]#nmcli connection
NAME                UUID                                  TYPE      DEVICE
eth0                5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0
Wired connection 1  bd3d361f-bd4d-3778-ad0b-2a5a957bfcc4  ethernet  eth1
eth1                9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04  ethernet  --
[root@centos8 network-scripts]#

#激活连接
[root@centos8 network-scripts]#nmcli connection up eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@centos8 network-scripts]#

#查看ip
[root@centos8 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:cd:ed:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:cd:ed:c2 brd ff:ff:ff:ff:ff:ff
    inet 6.6.6.6/24 brd 6.6.6.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:edc2/64 scope link
       valid_lft forever preferred_lft forever
[root@centos8 network-scripts]

#查看路由 route -n /ip route
[root@centos8 network-scripts]#ip route
default via 10.0.0.2 dev eth0 proto static metric 100
6.6.6.0/24 dev eth1 proto kernel scope link src 6.6.6.6 metric 101
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.100 metric 100
[root@centos8 network-scripts]#

#查看DNS
[root@centos8 network-scripts]#cat /etc/resolv.conf
# Generated by NetworkManager
search xingyuyu.com
nameserver 10.0.0.2
[root@centos8 network-scripts]#

邢宇宇

我是一名热情的云原生架构师和Java开发者,专注于构建可扩展的大数据解决方案。我的职业旅程始于对代码的热爱和创新技术的追求。我拥有在云计算和大数据领域深厚的技术背景,这使我能够设计和实现高效、稳健的系统。 我擅长使用Java来开发高性能的应用程序,同时也深入研究了如何利用云服务来优化数据处理过程。我热衷于探索新技术,如容器化、微服务架构和自动化,这些都是当今构建和部署现代软件解决方案的关键元素。 在我的博客中,我分享我的专业知识和行业见解,探讨最佳实践,以及如何克服在云原生生态系统中遇到的挑战。我相信通过共享和协作,我们可以推动技术的发展,创造出更好的解决方案来解决复杂问题。欢迎加入我的技术之旅,与我一起深入云原生的世界!

0 Comments

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注