Ubuntu系统中存在两种不同的网络管理工具分别是:systemd-networkd和NetworkManager,它们用于不同的环境和需求其主要区别如下。
1. systemd-networkd
systemd-networkd是systemd 组件的一部分,一个轻量级、高性能的网络管理器。它使用原生Linux网络接口,不依赖第三方软件包,因此占用更少的资源和内存。
systemd-networkd通过systemd网络守护进程启动和管理,可以实现自动启动、监控和重启,提高了系统的可靠性和稳定性。
特点:
- 轻量级:设计上比较简洁,适合资源有限的环境;
- 稳定性:由于其与 systemd 紧密集成,通常具有较高的稳定性;
- 配置方式:使用简单的配置文件(如 /etc/netplan/*.yaml)进行网络配置,通常不提供图形化界面;
- 服务启动:默认随系统启动并管理网络接口;
- 支持场景:适合需要稳定性和简单配置的服务器环境和嵌入式系统。
配置文件
vim /etc/netplan/01-network-manager-all.yaml
network:version: 2renderer: networkdethernets:ens1f0:dhcp4: nodhcp6: noaddresses:- 192.168.20.200/24routes:- to: defaultvia: 192.168.20.254nameservers:addresses:- 114.114.114.144- 221.228.255.1
备注:首先确定systemd-networkd已启动,其次需要注意配置文件内容缩进是否正确否则会报错。
sudo systemctl start systemd-networkd.service
sudo systemctl status systemd-networkd.service
sudo systemctl enable systemd-networkd.service
sudo netplan apply
2. NetworkManager
NetworkManager是一个功能强大的网络管理工具,更加如全面和复杂,设计目标是提供更多的功能和管理选项,它使用DBus作为通信机制,可以集成各种网络类型和设备,包括以太网、无线网络、蓝牙、移动宽带等。
NetworkManager支持广泛的网络协议和语言环境,并提供图形界面和命令行界面等多种管理方式。因此更适合桌面环境和对网络性能与安全性能要求更高的场景,优势在于功能全面、易于管理和配置。
特点:
- 全面性:提供图形化界面、命令行工具 (nmcli) 和 API,支持复杂的网络配置。
- 动态管理:支持 Wi-Fi、VPN、移动宽带等多种网络类型的动态配置和管理。
- 易用性:图形用户界面(如 GNOME 网络管理器)使得网络配置和管理更加直观。
- 服务启动:默认在图形化桌面环境中启动,并管理所有网络连接。
- 支持场景:适合需要频繁更改网络配置或使用图形界面的桌面系统。
NetworkManager服务启动与关闭
sudo systemctl start NetworkManager
sudo systemctl status NetworkManager
sudo systemctl enable NetworkManager
配置实例命令:nmtui与nmcli
nmcli general status / nmcli networking connectivity # 查看网络状态,状态:none、portal、limited、full、unknown
none : the host is not connected to any network.
portal : the host is behind a captive portal and cannot reach the full Internet.
limited : the host is connected to a network, but it has no access to the Internet.
full : the host is connected to a network and has full access to the Internet.
unknown : the connectivity status cannot be found out.nmcli networking on # 启用网络
nmcli networking off # 禁用网络
网络连接管理命令
nmcli connection {show | up | down | modify | add | edit | clone | delete | monitor | reload | load | import | export} [ARGUMENTS...]nmcli connection show # 查看网络连接信息
NAME UUID TYPE DEVICE
netplan-ens1f0 2dda75dd-8719-3e4f-8cd8-1f5e1f688daf ethernet ens1f0
Wired connection 1 5856440f-7eda-3a69-bb65-2f3d55bcae9b ethernet --
Wired connection 2 b49a0878-017c-35ec-924d-b48aea4b63ef ethernet --NAME: 连接名称,虚拟名称,无线连接一般为连接的ssid名称
UUID: 唯一标识
TYPE: 连接类型,ethernet代表网线连接(也称以太连接),wireless代表无线(WiFi)
DEVICE: 设备名称nmcli connection up [connection name] # 开启网卡,例如:nmcli connection up netplan-ens1f0
nmcli connection down [connection name] # 关闭网卡,例如:nmcli connection down netplan-ens1f0
网络修改管理命令
nmcli connection modify 设备名 [+ | -]选项 选项值 # 可简写为:nmcli c m 设备名 [+ | -]选项 选项值
nmcli connection modify netplan-ens1f0 ipv4.address 192.168.20.200/24 # 修改 IP 地址和子网掩码
nmcli connection modify netplan-ens1f0 +ipv4.addresses 192.168.20.11/24
nmcli connection modifynetplan-ens1f0 ipv4.method manual # 修改为静态配置,默认是 auto
nmcli connection modify netplan-ens1f0 ipv4.gateway 192.168.20.254 # 修改默认网关
nmcli connection modify netplan-ens1f0 ipv4.dns 221.228.255.1 # 修改 DNS
nmcli connection modify netplan-ens1f0 +ipv4.dns 114.114.114.114 # 添加一个 DNS
nmcli connection modify netplan-ens1f0 ipv6.method ignored # 将 IPv6 禁用
nmcli connection modify netplan-ens1f0 connection.autoconnect yes # 开机启动
同时支持修改配置文件:vim /etc/netplan/01-network-manager-all.yaml
network:version: 2renderer: NetworkManager # 选择以NetworkManager方式管理网络ethernets:ens1f0:dhcp4: nodhcp6: noaddresses:- 192.168.20.200/24gateway4: 192.168.20.254 # 这个参数在高版本已经被弃用会报warningroutes:- to: defaultvia: 192.168.20.254nameservers:addresses:- 114.114.114.144- 221.228.255.1
配置gateway4后报错如下:
** (process:7286): WARNING **: 14:19:06.345: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.
小结:
两种网络管理工具并不互斥,可根据需求选择使用。