[3] 操作系统 · 2021年4月8日

Ubuntu 18.04 服务器配置固定IP

1. 确认你要修改的网卡号

先确认你要修改的网卡号,假设你的服务器有多张网卡:

(base) liupei@I620-G20:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens2f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether e8:61:1f:1f:68:1c brd ff:ff:ff:ff:ff:ff
    inet 218.196.248.142/27 brd 218.196.248.159 scope global ens2f0
       valid_lft forever preferred_lft forever
3: ens2f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether e8:61:1f:1f:68:1d brd ff:ff:ff:ff:ff:ff
    inet 218.196.248.142/27 brd 218.196.248.159 scope global ens2f1
       valid_lft forever preferred_lft forever

2. 修改配置文件/etc/network/interfaces:

liupei@I620-G20:~$ sudo vim /etc/network/interfaces
 1 # interfaces(5) file used by ifup(8) and ifdown(8)
 2 auto ens2f0
 3 iface ens2f0 inet static
 4 address 218.196.248.142
 5 netmask 255.255.255.224
 6 gateway 218.196.248.129
 7 dns-servername 218.196.240.8 218.196.240.18

3. 配置DNS

 sudo vim /etc/systemd/resolved.conf
[Resolve]
DNS=[218.196.240.8,218.196.240.18]
sudo service systemd-resolved restart

sudo vim /etc/resolv.conf
17 nameserver 218.196.240.8 218.196.240.18
18 options edns0

4. 重启网络服务使配置生效

sudo services network restrart
sudo ifdown ens2f0
sudo ifup ens2f0

5. 测试网络是否连通

 2004  ping baidu.com
 2005  ping hpu.edu.cn
 2006  ping liupei.ink