首先要注意的是,不同版本的ubuntu系统设置静态ip的方式是不同的。
这当然是由于随着系统版本的变化,对于网络的设置管理组件的变更。
因此如下设置是与系统版本相关。
一、查看系统版本
lsb_release -a
输出
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
Release: 18.04
Codename: bionic
二、不同版本Ubuntu系统静态IP设置
Ubuntu 18.04.6 LTS
1 查看当前网卡
ifconfig
输出
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:07:f5:31:16 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.5.181 netmask 255.255.255.0 broadcast 172.16.5.255
inet6 fe80::69ab:4fa0:1090:7637 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b8:ce:c3 txqueuelen 1000 (Ethernet)
RX packets 72299 bytes 89104212 (89.1 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 27269 bytes 3351560 (3.3 MB)
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 44 bytes 3692 (3.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 44 bytes 3692 (3.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2 设置固定IP
这里修改ens33对外的网络为固定IP
查看配置文件
cd /etc/network
cat interfaces
输出
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
备份网卡文件
cp interfaces interfaces.bak
增加固定IP配置参数
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
## 增加的部分
auto ens33
iface ens33 inet static
address 172.16.5.156 ## 设置为的固定IP值
netmask 255.255.255.0 ## 参考ifconfig输出信息
gateway 172.16.5.2 ## gateway参考后面的查询命令
注意,其中的
查看gateway
route -n
输出
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.5.2 0.0.0.0 UG 20100 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 ens33
172.16.5.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
本机操作中,其中的第二列,第一行即为本机的有效gateway,用户可以参考观察自己本机的操作。
2 重启系统,观察是否生效
ifconfig
观察配置之后的IP是否有效,是否可以正常访问网络。
三、注意:
同一台虚拟机或者统一网络,不同机器必须采用不同的IP地址,否则会发生IP冲突,导致整个网络故障。