CentOS 7 以上默认使用 nmcli 配置IP地址等选项。首先修改主机名。本文设置为 node03。
[root@localhost ~]# nmcli general hostname node03
[root@localhost ~]# nmcli general hostname
node03
[root@localhost ~]# systemctl restart systemd-hostnamed.service
[root@localhost ~]# nmcli general hostname
node03
[root@localhost ~]#
[root@localhost ~]# hostnamectl set-hostname node3
[root@localhost ~]#
[root@localhost ~]# nmcli general hostname
node3
[root@localhost ~]#
分别使用命令 nmcli 和 hostnamectl 。
设置IP地址、DNS服务器和默认网关信息。
#检查网卡连接信息
[root@localhost ~]# nmcli networking connectivity check
full
#查看网卡 ens33 信息
[root@localhost ~]# nmcli device show ens33
GENERAL.DEVICE: ens33
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:16:0A:FC
GENERAL.MTU: 1500
GENERAL.STATE: 100(已连接)
GENERAL.CONNECTION: ens33
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER: 开
IP4.ADDRESS[1]: 192.168.116.132/24
IP4.GATEWAY: 192.168.116.2
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 192.168.116.2, mt = 100
IP4.ROUTE[2]: dst = 192.168.116.0/24, nh = 0.0.0.0, mt = 100
IP4.DNS[1]: 192.168.116.2
IP4.DOMAIN[1]: localdomain
IP6.ADDRESS[1]: fe80::e957:4fec:700b:434c/64
IP6.ADDRESS[2]: fe80::1b52:c838:3bfa:c862/64
IP6.ADDRESS[3]: fe80::88ef:2271:537d:fb4d/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
#设置 IP 地址、默认网关和 DNS
[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.116.132/24 +ipv4.dns 114.114.114.114 +ipv4.gateway 192.168.116.2
[root@localhost ~]# ip address show
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:16:0a:fc brd ff:ff:ff:ff:ff:ff
inet 192.168.116.132/24 brd 192.168.116.255 scope global dynamic noprefixroute ens33
valid_lft 970sec preferred_lft 970sec
inet6 fe80::88ef:2271:537d:fb4d/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::1b52:c838:3bfa:c862/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::e957:4fec:700b:434c/64 scope link noprefixroute
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:d9:4c:35 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: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:d9:4c:35 brd ff:ff:ff:ff:ff:ff
#加载配置信息
[root@localhost ~]# nmcli connection up ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ~]# ip address show
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:16:0a:fc brd ff:ff:ff:ff:ff:ff
inet 192.168.116.132/24 brd 192.168.116.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::88ef:2271:537d:fb4d/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::1b52:c838:3bfa:c862/64 scope link dadfailed tentative noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::e957:4fec:700b:434c/64 scope link noprefixroute
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:d9:4c:35 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: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:d9:4c:35 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]#
重新加载网络配置。
[root@localhost ~]# nmcli connection reload
[root@localhost ~]#
来源:oschina
链接:https://my.oschina.net/u/1011130/blog/4952963