Linux基础学习-网络管理
程序员文章站
2022-04-04 16:48:26
Linux系统网络管理NetworkManager 1 启动网络管理服务和开机自启动 在rhel7中网路管理相关命令 ,`nmtui nmtui edit nm connection editor` 服务启动 | 描述 | systemctl start NetworkManager | 启动服务 ......
Linux系统网络管理NetworkManager
1 启动网络管理服务和开机自启动
在rhel7中网路管理相关命令nmcli
,nmtui
,nmtui-edit
,nm-connection-editor
服务启动 | 描述 |
---|---|
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
[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地址
[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
生成新的配置文件
[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连接上去的会掉线.
[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再次连接又连接上去了.
[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地址
[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
删除该设备配置文件,重新配置一个新的配置文件.
[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 --