lmgsanm

每天学习一点,每天进步一点点…… Tomorrow is another beatifull day

导航

使用virtualBox复制虚拟机自动分配IP地址解决方法

问题背景

使用ip a 命令查看主机IP信息如下

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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:25:3d:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.9/24 brd 192.168.1.255 scope global noprefixroute dynamic enp0s3
valid_lft 83720sec preferred_lft 83720sec
inet 192.168.1.213/24 brd 192.168.1.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 2408:8207:2624:5d30:a732:db9a:bdd0:3629/64 scope global noprefixroute dynamic
valid_lft 259047sec preferred_lft 172647sec
inet6 fe80::d9d5:8327:cec1:4898/64 scope link noprefixroute
valid_lft forever preferred_lft forever

 

 

 

使用ip r查看路由信息如下

ip r

default via 192.168.1.1 dev enp0s3
default via 192.168.1.1 dev enp0s3 proto dhcp metric 100
169.254.0.0/16 dev enp0s3 scope link metric 1002
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.9 metric 100

 

 

 

查看网卡文件配置

cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=statics
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=enp0s3
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.1.213
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

问题分析

从上面看,服务器自动分配了一个IP地址并绑定到ifcfg-enp0s3上,且设置的默认路由的下一跳为动态获取的IP地址

 

解决方案

在网卡配置文件中添加1行参数:NM_CONTROLLED=no后重启网卡

 即

cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=statics
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=enp0s3
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.1.213
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
NM_CONTROLLED=no

systemctl restart network.service

结果验证

查看网卡IP信息

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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:25:3d:21 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.213/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 2408:8207:2624:5d30:a00:27ff:fe25:3d21/64 scope global mngtmpaddr dynamic
       valid_lft 259164sec preferred_lft 172764sec
    inet6 fe80::a00:27ff:fe25:3d21/64 scope link
       valid_lft forever preferred_lft forever

 

查看路由信息

ip r

default via 192.168.1.1 dev enp0s3
169.254.0.0/16 dev enp0s3 scope link metric 1002
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.213

 

知识扩展

NM_CONTROLLED设置为no表示该接口将通过该配置文件进行设置,而不是通过网络管理器

NM_CONTROLLED 设置 yes 表示网卡允许用 NetworkManager 程序管理。它可以降低网络配置使用难度,便于管理无线网络、虚拟专用网等网络连接,适合普通台式机和笔记本电脑使用。

当 NM_CONTROLLED 设为 yes 并有安装运行 NetworkManager 服务。若编辑了网卡配置文件,需要先重启 NetworkManager 再重启 network 服务。

 

查看NetworkManager是否配置自启动:

systemctl list-unit-files | grep NetworkManager

NetworkManager-dispatcher.service             enabled
NetworkManager-wait-online.service            enabled
NetworkManager.service                        enabled

从上面结果可以看出来,NetworkManager服务默认配置了自启动,需进行关闭和禁用

systemctl stop NetworkManager.service

systemctl disable NetworkManager.service

Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.

systemctl list-unit-files | grep NetworkManager

NetworkManager-dispatcher.service             disabled
NetworkManager-wait-online.service            disabled
NetworkManager.service                        disabled

从上面结果来看,NetworkManager已被禁用开机自启动功能

systemctl restart network.service

posted on 2022-05-04 15:57  lmgsanm  阅读(329)  评论(0编辑  收藏  举报