方法1:Netplan实用程序 版本
配置前请自行备份配置文件
第一步,配置文件
root@lenovo-07:/etc/netplan# ls
01-network-manager-all.yaml
root@lenovo-07:/etc/netplan# cat 01-network-manager-all.yaml
network:
ethernets:
eno1: #网卡名字
dhcp4: no #是否自动配置ip
dhcp6: no #是否自动配置ipv6
addresses: [*.*.*.*/23] #IP地址用,隔开 / 后面的为子网
gateway4: *.*.*.1 #网关
nameservers:
addresses: [114.114.114.114,8.8.8.8] #设置DNS服务器,能上大部分网站,且有概率提升下载速度
# DHCP
eno2:
dhcp4: true
addresses: []
# Static
eno3:
addresses: [192.168.225.50/24]
dhcp4: no
dhcp6: no
gateway4: 192.168.225.1
nameservers:
addresses: [114.114.114.114,8.8.8.8]
version: 2
第二步 激活配置
root@lenovo-07:/etc/netplan# sudo netplan apply
或
root@lenovo-07:/etc/netplan# sudo netplan --debug apply # 此命令可查看配置生效情况,可根据提示进行修改调整
方法2:配置文件版本 (/etc/network/interfaces)
第一步 修改配置文件
root@lenovo-07:/etc/network# cat interfaces
# DHCP
auto eno1 # 网卡名称
iface eno1 inet dhcp # 设置DHCP
# Static
auto eno2
iface eno2 inet static # 静态ip,不采用DHCP动态分配
address 192.168.90.142 # 配置ip
netmask 255.255.255.0 # 子网掩码
gateway 192.168.90.2 # 网关
dns-nameservers 10.0.208.1 #设置dns服务器地址
第二步 激活配置
root@lenovo-07:/etc/netplan# sudo /etc/init.d/networking restart
或者
#刷新网络接口
root@lenovo-07:/etc/netplan# sudo ip addr flush eno1
#重启网络服务
root@lenovo-07:/etc/netplan# sudo systemctl restart networking.service
查看当前激活的DNS
root@lenovo-07:/etc# cat resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 211.137.191.26
nameserver 223.5.5.5