在各大Linux的最新发行版中(Centos 8版本以上),废弃了network服务以及/etc/sysconfig/network-scripts进行管理地址的方式。
在Centos8版本及以上发行版中,可以使用以下两种方式设置IP地址。
IP地址修改
修改配置文件
网卡配置文件位于以下路径:
vi /etc/NetworkManager/system-connections/ens160.nmconnection
配置方式如下:
[connection]
id=ens160
uuid=17951fcc-b2e6-3fad-bd25-ab226763637f
type=ethernet
autoconnect-priority=-999
interface-name=ens160
timestamp=1677439993
[ethernet]
[ipv4]
method=manual #manual为手工 auto为自动获取 disabled为禁用
address1=192.168.3.41/24,192.168.3.1
dns=114.114.114.114;8.8.8.8
[ipv6]
addr-gen-mode=eui64
method=disabled
[proxy]
使地址生效
nmcli c reload
# 重启生效
nmcli con down ens160 && nmcli con up ens160
使用命令设置静态地址
# 设置网卡自启动
nmcli con mod ens160 connection.autoconnect yes
# 设置IP地址
nmcli con mod ens160 ipv4.address "192.168.3.41/24"
# 设置网关地址
nmcli con mod ens160 ipv4.gateway "192.168.3.1"
# 设置dns
nmcli con mod ens160 ipv4.dns 114.114.114.114
# 添加dns
nmcli con mod ens160 +ipv4.dns 8.8.8.8
# 修改IP地址是静态(manual)还是dhcp动态(auto) disabled为禁用
nmcli con mod ens160 ipv4.method manual
# 重启生效
nmcli con down ens160 && nmcli con up ens160
更改网卡名称
在使用Linux的过程中会遇到修改网卡名称的情况,方式如下 :
# 将网卡名称为dhcp的网卡,修改名称为ens160
nmcli con modify dhcp.id ens160
# 重新载入网卡,使网卡生效
nmcli c reload
# 重启
nmcli con down ens160 && nmcli con up ens160
查看网卡名称
[root@k8s-master ~]# nmcli con show
NAME UUID TYPE DEVICE
ens160 e375725e-fdbe-48b1-9c2c-64358d92c16f ethernet ens160
查看网卡信息
[root@k8s-master ~]# nmcli con show ens160
connection.id: ens160
connection.uuid: e375725e-fdbe-48b1-9c2c-64358d92c16f
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: ens160
connection.autoconnect: 是
connection.autoconnect-priority: 0
connection.autoconnect-retries: -1 (default)
connection.multi-connect: 0(default)
connection.auth-retries: -1
connection.timestamp: 1677419863
connection.read-only: 否
connection.permissions: --
connection.zone: --
connection.master: --
connection.slave-type: --
connection.autoconnect-slaves: -1(default)
connection.secondaries: --
connection.gateway-ping-timeout: 0
connection.metered: 未知
connection.lldp: default
connection.mdns: -1(default)
connection.llmnr: -1(default)
connection.dns-over-tls: -1(default)
connection.mptcp-flags: 0x0(default)
connection.wait-device-timeout: -1
connection.wait-activation-delay: -1
802-3-ethernet.port: --
802-3-ethernet.speed: 0
802-3-ethernet.duplex: --
802-3-ethernet.auto-negotiate: 否
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: 自动
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,8.8.8.8
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.3.41/24
ipv4.gateway: 192.168.3.1
ipv4.routes: --
ipv4.route-metric: -1
ipv4.route-table: 0 (unspec)
ipv4.routing-rules: --
ipv4.ignore-auto-routes: 否
ipv4.ignore-auto-dns: 否
ipv4.dhcp-client-id: --
ipv4.dhcp-iaid: --
ipv4.dhcp-timeout: 0 (default)
ipv4.dhcp-send-hostname: 是
ipv4.dhcp-hostname: --
ipv4.dhcp-fqdn: --
ipv4.dhcp-hostname-flags: 0x0(none)
ipv4.never-default: 否
ipv4.may-fail: 是
ipv4.required-timeout: -1 (default)
ipv4.dad-timeout: -1 (default)
ipv4.dhcp-vendor-class-identifier: --
ipv4.link-local: 0(default)
ipv4.dhcp-reject-servers: --
ipv6.method: disabled
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: 否
ipv6.ignore-auto-dns: 否
ipv6.never-default: 否
ipv6.may-fail: 是
ipv6.required-timeout: -1 (default)
ipv6.ip6-privacy: -1(unknown)
ipv6.addr-gen-mode: default
ipv6.ra-timeout: 0 (default)
ipv6.mtu: 自动
ipv6.dhcp-duid: --
ipv6.dhcp-iaid: --
ipv6.dhcp-timeout: 0 (default)
ipv6.dhcp-send-hostname: 是
ipv6.dhcp-hostname: --
ipv6.dhcp-hostname-flags: 0x0(none)
ipv6.token: --
proxy.method: none
proxy.browser-only: 否
proxy.pac-url: --
proxy.pac-script: --
GENERAL.NAME: ens160
GENERAL.UUID: e375725e-fdbe-48b1-9c2c-64358d92c16f
GENERAL.DEVICES: ens160
GENERAL.IP-IFACE: ens160
GENERAL.STATE: 已激活
GENERAL.DEFAULT: 是
GENERAL.DEFAULT6: 是
GENERAL.SPEC-OBJECT: --
GENERAL.VPN: 否
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/4
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/3
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
IP4.ADDRESS[1]: 192.168.3.41/24
IP4.GATEWAY: 192.168.3.1
IP4.ROUTE[1]: dst = 192.168.3.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.3.1, mt = 100
IP4.DNS[1]: 114.114.114.114
IP4.DNS[2]: 8.8.8.8
IP6.ADDRESS[1]: 2408:8215:4b3e:3f80:e020:dcc1:32b5:5b3/64
IP6.ADDRESS[2]: fe80::63d2:6af6:8bb2:cf94/64
IP6.GATEWAY: fe80::2ea0:42ff:fe20:e61f
IP6.ROUTE[1]: dst = 2408:8215:4b3e:3f80:2ca0:4220:e61f:8/128, nh = ::, mt = 100
IP6.ROUTE[2]: dst = 2408:8215:4b3e:3f80::/64, nh = ::, mt = 100
IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 1024
IP6.ROUTE[4]: dst = ::/0, nh = fe80::2ea0:42ff:fe20:e61f, mt = 100
IP6.DNS[1]: fe80::2ea0:42ff:fe20:e61f
DHCP6.OPTION[1]: dhcp6_name_servers = fe80::2ea0:42ff:fe20:e61f
DHCP6.OPTION[2]: ip6_address = 2408:8215:4b3e:3f80:2ca0:4220:e61f:8
评论区