網路設定之nmcli,配置nmcli
使用nmcli命令配置網路
NetworkManager是管理和監控網路設定的守護進程,裝置既就是網路介面,串連是對網路介面的配置,一個網路介面可以有多個串連配置,但同時只有一個串連配置生效。
1 配置主機名稱
CentOS6 之前主機設定檔:/etc/sysconfig/network
CentOS7主機名稱設定檔:/etc/hostname,預設沒有這個檔案,通過DNS反向解析擷取主機名稱,主機名稱預設為:localhost.localdomain
顯示主機名稱資訊
hostname
hostnamectl status
建立並修改檔案並生效
hostnamectl set-hostname deskX.example.com
[root@centos73 ~]# hostnamectl set-hostname centos7.3.com[root@centos73 ~]# hostnamecentos7.3.com[root@centos73 ~]# cat /etc/hostnamecentos7.3.com
可以看到已經修改成功。
刪除檔案。回複預設主控件名localhost.localdomain
2 常用命令
nmcli connection show 顯示所有串連
nmcli connection show --active 顯示所有活動的串連狀態
nmcli connection show "ens33" 顯示網路連接配置
nmcli device status 顯示裝置狀態
nmcli device show ens33 顯示網路介面屬性
nmcli connection add help 查看協助
nmcli connection reload 重新載入配置
nmcli connection down test2 禁用test2的配置,注意一個網卡可以有多個配置。
nmcli connection up test2 啟用test2的配置
nmcli device disconnect ens33 禁用ens33網卡,物理網卡
nmcli device disconnect ens33 啟用ens33網卡
圖形工具
nm-connection-editor
字元工具
nmtui
nmtui-connect
nmtui-edit
nmtui-hostname
3 建立新串連配置
(1)建立新串連配置default,IP通過DHCP自動擷取
[root@centos73 ~]# nmcli connection add con-name default type Ethernet ifname eth0Connection 'default' (d62f98af-86ed-4735-80aa-2d80351765b2) successfully added.
刪除串連
[root@centos73 ~]# nmcli connection delete defaultConnection 'default' (d62f98af-86ed-4735-80aa-2d80351765b2) successfully deleted.
(2)建立新的串連配置test2,指定靜態IP,不自動連接
[root@centos73 ~]# nmcli connection add con-name test2 ipv4.method manual ifname ens33 autoconnect no type Ethernet ipv4.addresses 172.25.100.100/24 gw4 172.25.100.1Connection 'test2' (98385bf5-8cc3-47e3-a5f2-51e2d736be6d) successfully added.
參數說明:
con-name 指定串連名字,沒有特殊要求,
ipv4.methmod 指定擷取IP地址的方式
ifname 指定網卡設被名,既就是次配置所生效的網卡
autoconnect 指定是否自動啟動
ipv4.addresses 指定IPv4地址
gw4指定網關
4查看/etc/sysconfig/network-scripts/目錄
會看到多出一個檔案ifcfg-test2。說明添加確實生效了。
/etc/sysconfig/network-scripts/ifcfg-test2
[root@centos73 ~]# ls /etc/sysconfig/network-scripts/ifcfg-*/etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-lo/etc/sysconfig/network-scripts/ifcfg-ens34 /etc/sysconfig/network-scripts/ifcfg-test2
5 啟用test2串連配置
[root@centos73 ~]# nmcli connection up test2Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)[root@centos73 ~]# systemctl restart network
看下是否生效
[root@centos73 ~]# nmcli device show ens33GENERAL.DEVICE: ens33GENERAL.TYPE: ethernetGENERAL.HWADDR: 00:0C:29:3A:00:82GENERAL.MTU: 1500GENERAL.STATE: 100 (connected)GENERAL.CONNECTION: test2GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/7WIRED-PROPERTIES.CARRIER: onIP4.ADDRESS[1]: 172.25.100.100/24IP4.GATEWAY: 172.25.100.1IP6.ADDRESS[1]: fe80::5a5b:2cc3:a448:e2e1/64IP6.GATEWAY:
好了,基本的IP地址配置成功
6 修改串連設定
(1)修改test2為自動啟動
[root@centos73 ~]# nmcli connection modify test2 connection.autoconnect yes
(2)修改DNS為172.25.1.1
[root@centos73 ~]# nmcli connection modify test2 ipv4.dns 172.25.1.1
(3)添加DNS 114.114.114.114
[root@centos73 ~]# nmcli connection modify test2 +ipv4.dns 114.114.114.114
看下是否成功
[root@centos73 ~]# cat /etc/sysconfig/network-scripts/ifcfg-test2TYPE=Ethernet[……]NAME=test2UUID=98385bf5-8cc3-47e3-a5f2-51e2d736be6dDEVICE=ens33ONBOOT=yesDNS1=172.25.1.1IPADDR=172.25.100.100PREFIX=24GATEWAY=172.25.100.1DNS2=114.114.114.114
可以看到均已生效
(4)刪除DNS
[root@centos73 ~]# nmcli connection modify test2 -ipv4.dns 114.114.114.114
(5)修改IP地址
[root@centos73 ~]# nmcli connection modify test2 ipv4.addresses 200.100.100.100/24 gw4 200.100.100.1
(6)還可以添加多個IP
[root@centos73 ~]# nmcli connection modify test2 +ipv4.addresses 10.10.10.10/8
7 nmcli 命令和/etc/sysconfig/network-scripts/ifcfg-*檔案的對應關係
ipv4.method manual BOOTPROTO=none
ipv4.method auto BOOTPROTO=dhcp
ipv4.addresses "192.0.2.1/24 IPADDR=192.0.2.1
PREFIX=24
gw4 192.0.2.254" GATEWAY=192.0.2.254
ipv4.dns 8.8.8.8 DNS0=8.8.8.8
ipv4.dns-search example.com DOMAIN=example.com
ipv4.ignore-auto-dns true PEERDNS=no
connection.autoconnect yes ONBOOT=yes
connection.id eth0 NAME=eth0
connection.interface-name eth0 DEVICE=eth0
802-3-ethernet.mac-address . . . HWADDR= . . .