1 啓動網絡管理服務和開機自啓動
在rhel7中網路管理相關命令nmcli
,nmtui
,nmtui-edit
,nm-connection-editor
linux
服務啓動 | 描述 |
---|---|
systemctl start NetworkManager | 啓動服務 |
systemctl restart NetworkManager | 重啓服務 |
systemctl stop NetworkManager | 中止服務 |
systemctl enable NetworkManager | 開機自啓動服務 |
systemctl disable NetworkManager | 取消開機自啓動 |
[root@qdlinux ~]# systemctl disable NetworkManager Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service. Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service. Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service. [root@qdlinux ~]# systemctl enable NetworkManager Created symlink from /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service to /usr/lib/systemd/system/NetworkManager.service. Created symlink from /etc/systemd/system/multi-user.target.wants/NetworkManager.service to /usr/lib/systemd/system/NetworkManager.service. Created symlink from /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service to /usr/lib/systemd/system/NetworkManager-dispatcher.service. [root@qdlinux ~]# systemctl is-enabled NetworkManager enabled [root@qdlinux ~]# systemctl is-active NetworkManager active
2 查看設備狀態命令nmcli device
和nmcli connection
ios
[root@qdlinux ~]# nmcli device DEVICE TYPE STATE CONNECTION ens32 ethernet connected ens32 virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged -- [root@qdlinux ~]# nmcli connection NAME UUID TYPE DEVICE ens32 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet ens32 virbr0 8e94e1f3-ca47-40b3-9003-15c741bcd332 bridge virbr0
3 查看ip地址vim
[root@qdlinux ~]# ifconfig ens32 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.5 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::65fe:5243:aab7:46ac prefixlen 64 scopeid 0x20<link> ether 00:0c:29:2e:6e:9f txqueuelen 1000 (Ethernet) RX packets 834 bytes 72426 (70.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 584 bytes 69140 (67.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@qdlinux ~]# ip address show ens32 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:2e:6e:9f brd ff:ff:ff:ff:ff:ff inet 192.168.56.5/24 brd 192.168.56.255 scope global noprefixroute ens32 valid_lft forever preferred_lft forever inet6 fe80::65fe:5243:aab7:46ac/64 scope link noprefixroute valid_lft forever preferred_lft forever
[root@qdlinux ~]# ip -s link show ens32 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:2e:6e:9f brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 85134 991 0 0 0 0 TX: bytes packets errors dropped carrier collsns 80226 678 0 0 0 0
生成新的配置文件centos
[root@qdlinux ~]# nmcli connection add con-name ens33-dhcp ifname ens32 autoconnect yes type ethernet ipv4.method auto Connection 'ens33-dhcp' (ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4) successfully added. [root@qdlinux ~]# nmcli connection NAME UUID TYPE DEVICE ens32 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet ens32 virbr0 8e94e1f3-ca47-40b3-9003-15c741bcd332 bridge virbr0 ens33-dhcp ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4 ethernet --
查看配置文件網絡
[root@qdlinux ~]# less /etc/sysconfig/network-scripts/ifcfg-ens33-dhcp TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp 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=ens33-dhcp UUID=ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4 DEVICE=ens32 ONBOOT=yes (END)
啓動新的配置文件,若是此時是用ssh鏈接上去的會掉線.less
[root@qdlinux ~]# nmcli connection up ens33-dhcp Socket error Event: 32 Error: 10053. Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(192.168.56.5) at 09:49:30.
在虛擬機中查看ip地址,發現ip地址已經自動獲取,而且已經改變.ssh
如今切換原來的配置文件,ssh再次鏈接又鏈接上去了.oop
[root@qdlinux ~]# nmcli connection up ens32 [root@qdlinux ~]# ifconfig ens32 ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.5 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::65fe:5243:aab7:46ac prefixlen 64 scopeid 0x20<link> ether 00:0c:29:2e:6e:9f txqueuelen 1000 (Ethernet) RX packets 2508 bytes 205179 (200.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1514 bytes 196730 (192.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
虛擬機中從新添加一塊網卡用於實驗,查看一下設備名稱和ip地址ui
[root@qdlinux ~]# nmcli device DEVICE TYPE STATE CONNECTION ens32 ethernet connected ens32 ens34 ethernet connected Wired connection 1 virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged -- [root@qdlinux ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.5 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::65fe:5243:aab7:46ac prefixlen 64 scopeid 0x20<link> ether 00:0c:29:2e:6e:9f txqueuelen 1000 (Ethernet) RX packets 3153 bytes 258362 (252.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1797 bytes 227166 (221.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.103 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::3432:a743:270f:eb3d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:2e:6e:a9 txqueuelen 1000 (Ethernet) RX packets 47 bytes 3569 (3.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 23 bytes 4673 (4.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
刪除該設備配置文件,從新配置一個新的配置文件.spa
[root@qdlinux ~]# nmcli connection delete ens32 help path virbr0 ens33-dhcp id uuid Wired\ connection\ 1 [root@qdlinux ~]# nmcli connection delete "Wired connection 1" Connection 'Wired connection 1' (e9c28a80-5519-359b-919f-4a0d572044e3) successfully deleted. [root@qdlinux ~]#
查看一下網絡設備狀態
[root@qdlinux ~]# nmcli connection NAME UUID TYPE DEVICE ens32 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet ens32 virbr0 8e94e1f3-ca47-40b3-9003-15c741bcd332 bridge virbr0 ens33-dhcp ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4 ethernet -- [root@qdlinux ~]# nmcli device DEVICE TYPE STATE CONNECTION ens32 ethernet connected ens32 virbr0 bridge connected virbr0 ens34 ethernet disconnected -- lo loopback unmanaged -- virbr0-nic tun unmanaged --
添加一個手動配置文件
[root@qdlinux ~]# nmcli connection add con-name ens34-static ifname ens34 type ethernet ipv4.method manual autoconnect yes ipv4.addresses 192.168.56.26/24 ipv4.gateway 192.168.56.2 ipv4.dns 202.102.134.68 +ipv4.dns 202.102.128.68 Connection 'ens34-static' (7050b2ec-2d8b-42a8-be2b-856d92dc66a4) successfully added.
再查看一個設備狀態
[root@qdlinux ~]# nmcli device DEVICE TYPE STATE CONNECTION ens32 ethernet connected ens32 ens34 ethernet connected ens34-static virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged -- [root@qdlinux ~]# nmcli connection NAME UUID TYPE DEVICE ens32 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet ens32 ens34-static 7050b2ec-2d8b-42a8-be2b-856d92dc66a4 ethernet ens34 virbr0 8e94e1f3-ca47-40b3-9003-15c741bcd332 bridge virbr0 ens33-dhcp ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4 ethernet --
查看一下該配置文件信息
[root@qdlinux ~]# less /etc/sysconfig/network-scripts/ifcfg-ens34-static TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=192.168.56.26 PREFIX=24 GATEWAY=192.168.56.2 DNS1=202.102.134.68 DNS2=202.102.128.68 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=ens34-static UUID=7050b2ec-2d8b-42a8-be2b-856d92dc66a4 DEVICE=ens34 ONBOOT=yes
修改開機自啓動
[root@qdlinux ~]# nmcli connection modify ens34-static autoconnect no [root@qdlinux ~]# tail /etc/sysconfig/network-scripts/ifcfg-ens34-static IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens34-static UUID=7050b2ec-2d8b-42a8-be2b-856d92dc66a4 DEVICE=ens34 ONBOOT=no
修改DNS經過+/-
添加刪除dns
[root@qdlinux ~]# nmcli connection modify ens34-static ipv4.dns 202.102.134.68 [root@qdlinux ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens34-static | grep DNS DNS1=202.102.134.68
經過手動方式修改配置文件,首先複製一份原始配置命名爲ifcfg-ens35-static
,修改配置名稱、協議、刪除UUID、IP地址
[root@qdlinux network-scripts]# vim ifcfg-ens35-static TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no TYPE=Ethernet PROXY_METHOD=none TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static //修改成static 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=ens35-static//修改成ens35-static DEVICE=ens35//修改成ens35 ONBOOT=yes IPADDR=192.168.56.26 PREFIX=24 GATEWAY=192.168.56.2 DNS1=202.102.134.68 DNS2=114.114.114.114
從新加載一下配置文件啓動配置
[root@qdlinux network-scripts]# nmcli connection reload [root@qdlinux network-scripts]# nmcli device DEVICE TYPE STATE CONNECTION ens32 ethernet connected ens32 ens34 ethernet connected ens34-static ens35 ethernet connected ens35-static virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged -- [root@qdlinux network-scripts]# nmcli connection NAME UUID TYPE DEVICE ens32 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet ens32 ens34-static 7050b2ec-2d8b-42a8-be2b-856d92dc66a4 ethernet ens34 ens35-static c3f27806-e8cf-8d29-2fba-c732def2dc76 ethernet ens35 virbr0 8e94e1f3-ca47-40b3-9003-15c741bcd332 bridge virbr0 ens33-dhcp ee3fc1d8-8d0e-4749-a624-cbdf6505bfe4 ethernet -- [root@qdlinux network-scripts]# nmcli connection down ens35-static ; nmcli connection up ens35-static
修改centos默認ens32爲eth0方法
[root@qdlinux network-scripts]# vim /etc/sysconfig/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="rhgb net.ifnames=0 biosdevname=0 quiet" GRUB_DISABLE_RECOVERY="true" //從新加載一下 [root@qdlinux network-scripts]# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-52d8eda1b73640adbcf5f8c63832171a Found initrd image: /boot/initramfs-0-rescue-52d8eda1b73640adbcf5f8c63832171a.img done [root@qdlinux network-scripts]#
從新啓動計算機,發現已經修改爲功
[root@qdlinux ~]# nmcli connection NAME UUID TYPE DEVICE eth0 b6ee9f55-35dd-41b0-865d-d83bc597877d ethernet eth0 eth1-static 7050b2ec-2d8b-42a8-be2b-856d92dc66a4 ethernet eth1 eth2-static 8f105ed6-1361-8e14-51fd-dedb8ef3510a ethernet eth2 virbr0 9a190443-83a0-4623-a6b4-a62fdda2d167 bridge virbr0 [root@qdlinux ~]# nmcli device DEVICE TYPE STATE CONNECTION eth0 ethernet connected eth0 eth1 ethernet connected eth1-static eth2 ethernet connected eth2-static virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged --