配置静态地址的方法
图形界面配置
略(主要是需要截图好几张,但是本身操作又简单就不写了)
修改配置文件
配置文件路径在/etc/netplan/xxx.yaml(这里每个人的xxx可能不一样)
默认如果开启了dhcp自动获取地址的话应该是这样
root@Ubuntu:~# cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: version: 2 ethernets: ens3: dhcp4: true match: macaddress: fa:16:3e:20:57:4e mtu: 1500 set-name: ens3
如果需要修改为静态地址可以先找出当前IP然后配置为一样的
root@Ubuntu:~# ip a |grep "global.*ens3" #这里的ens3是对应的网卡名 inet 10.10.21.170/24 brd 10.10.21.255 scope global dynamic ens3
接下来就是将配置文件的IP修改为10.10.21.170/24
root@Ubuntu:~# cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: version: 2 ethernets: ens3: dhcp4: false #这里也可以写no addresses: [10.10.21.170/24] optional: true gateway4: 10.10.21.254 nameservers: addresses: [223.5.5.5] match: macaddress: fa:16:3e:20:57:4e mtu: 1500 set-name: ens3 root@Ubuntu:~# netplan apply
NetworkManager修改
如果没有命令的话需要先安装NetworkManager
root@Ubuntu:~# apt-get install network-manager
接下来启动NetworkManager管理网卡
修改NewworkManager配置文件
root@Ubuntu:~# cat /etc/NetworkManager/NetworkManager.conf [main] plugins=ifupdown,keyfile [ifupdown] managed=true #这里默认是false,需要修改为true [device] wifi.scan-rand-mac-address=no
修改/etc/netplan/50-cloud-init.yaml
root@Ubuntu:~# cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: version: 2 renderer: NetworkManager #必须修改否则NetworkManager无法管理
修改完成之后重启服务
root@Ubuntu:~# systemctl restart network-manager root@Ubuntu:~# nmcli device DEVICE TYPE STATE CONNECTION ens13 ethernet connected ens3.1 ens3 ethernet connected ens3 如果上面没有改的话,这里的state会是unmanaged
nmtui修改
这种也是类图形界面
root@Ubuntu:~# nmtui
然后按提示一步步修改即可,修改完了记得activate激活配置
nmcli
命令行修改
root@Ubuntu:~# nmcli connection modify ens3.1 ipv4.addresses 10.10.30.28/24 ipv4.gateway 10.10.30.254 ipv4.method manual ipv4.dns 114.114.114.114 autoconnect yes root@Ubuntu:~# nmcli connection up ens3.1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
nmcli修改网卡名字
很多时候用来NetworkManager会生成一些奇奇怪怪的网卡名字,比如"Wired connection 1",看起来就难受,用起来有时候也不方便
root@Ubunt:~# nmcli connection modify "Wired connection 1" connection.id ens33