centos7下命令行网络管理nmcli

转载https://www.jianshu.com/p/5d5560e9e26a

激活连接

nmcli connection up connection-name## e.g. 激活eth1接口的static-eth1连接[root@localhost ~]# nmcli c up static-eth1Connection successfully activated (D-Bus activepath:/org/freedesktop/NetworkManager/ActiveConnection/2)

修改连接的IP地址

```

# 可修改的属性可通过以下命令查看nmcli c showstatic-eth1# 

修改命令nmcli connection modify [--temporary] [id | uuid | path] ([+|-]. )+## e.g. 修改连接static-eth1的ip地址[root@localhost ~]# ip addr | grep eth14: eth1: mtu1500qdisc pfifo_fast state UP qlen1000inet172.16.60.10/24brd172.16.60.255scopeglobaleth1[root@localhost ~]# nmcli c mod static-eth1 ipv4.addr 172.16.60.20/24[root@localhost ~]# nmcli c up static-eth1Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)[root@localhost ~]# ip a | grep eth14: eth1: mtu1500qdisc pfifo_fast state UP qlen1000inet172.16.60.20/24brd172.16.60.255scopeglobaleth1

```

配置连接的DNS

```

# 设定单个DNSnmcli connection modify static-eth1 ipv4.dns DNS1# 设定多个DNSnmcli connection modify static-eth1 ipv4.dns"DNS1 DNS2"# 以上命令会替换之前的DNS设置# 添加某个连接的DNS,需要使用前缀“+”nmcli connection modify static-eth1 +ipv4.dns DNS3## e.g. 配置static-eth1连接的DNS[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1IPV6_PEERDNS=yes[root@localhost ~]# nmcli c mod static-eth1 ipv4.dns "114.114.114.114 223.5.5.5"# 修改连接后,需要重新激活[root@localhost ~]# nmcli c up static-eth1Connection successfully activated (D-Bus activepath:/org/freedesktop/NetworkManager/ActiveConnection/4)[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1DNS1=114.114.114.114DNS2=223.5.5.5IPV6_PEERDNS=yes# 新增DNS[root@localhost ~]# nmcli c mod static-eth1 +ipv4.dns 223.5.5.6[root@localhost ~]# nmcli c up static-eth1Connection successfully activated (D-Bus activepath:/org/freedesktop/NetworkManager/ActiveConnection/5)[root@localhost ~]# grep DNS /etc/sysconfig/network-scripts/ifcfg-static-eth1DNS1=114.114.114.114DNS2=223.5.5.5DNS3=223.5.5.6IPV6_PEERDNS=yes

```

设置主机名

# 查询当前主机名nmcli general hostname# 更改主机名nmcli general hostname my-hostname# 重启hostnamed服务systemctl restart systemd-hostnamed

CentOS7下的主机名管理是基于系统服务systemd-hostnamed,服务自身提供了hostnamectl命令用于修改主机名,推荐这种方式进行修改

使用nmcli命令更改主机名时,systemd-hostnamed服务并不知晓 /etc/hostname 文件被修改,因此需要重启服务去读取配置;

你可能感兴趣的:(centos7下命令行网络管理nmcli)