前記:
Ubuntu 從 17.10 開始,已放棄在 /etc/network/interfaces ⾥固定IP的配置,⽽是改成 netplan ⽅式, 配置⽂件是:/etc/netplan/01-netcfg.yaml
實驗目標:
把eth0網卡IP地址192.168.1.100改爲192.168.1.101
配置文件:
/etc/netplan/*.yaml
*注意:要嚴格遵循yaml格式的上下級縮進關係 否則配置可能會無法生效
實驗步驟:
1、確認要修改的網卡號
以下以我的服務器eth0網卡爲例
user@user:~$ ifconfig
我的服務器配置如下:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe19:e292 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:19:e2:92 txqueuelen 1000 (Ethernet)
RX packets 1617 bytes 119610 (119.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7097 bytes 565714 (565.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 7505 bytes 575143 (575.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7505 bytes 575143 (575.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2、設置靜態IP
ubuntu1804版本的靜態網卡的ip配置文件爲/etc/netplan/
user@user:~$ sudo vim /etc/netplan/01-netcfg.yaml
把eth0網卡IP地址192.168.1.100改爲192.168.1.101
ip前綴24位
子網掩碼:255.255.255.0
網關位:192.168.1.2
DNS:114.114.114.114
~#sudo vim /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses: [192.168.1.101/24]
gateway4: 192.168.1.2
nameservers:
addresses: [114.114.114.114]
# sudo netplan apply //應用新配置
# vim ~/.vimrc #取消vim ⾃動縮進功能
set paste
3、應用新的配置
user@user:~$ sudo netplan apply //應用當前修改配置
使用ifconfig檢查新地址
user@user:~$ ifconfig
新的ip地址
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe19:e292 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:19:e2:92 txqueuelen 1000 (Ethernet)
RX packets 141 bytes 11412 (11.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1555 bytes 120688 (120.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 2249 bytes 169961 (169.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2249 bytes 169961 (169.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
user@user:~$
4、測試網絡聯通
user@user:~$ ping 192.168.1.101