Ubuntu配置静态IP地址
程序员文章站
2024-02-12 11:01:58
...
经常需要把局域网内部服务器设置静态IP地址,接下来讲Ubuntu的命令行模式下完成这件事。
- 获取当前主机的IP和子网掩码
$ ifconfig
ens33 Link encap:Ethernet HWaddr 00:0c:29:f8:fc:d1
inet addr:192.168.79.128 Bcast:192.168.79.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef8:fcd1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:741 errors:0 dropped:0 overruns:0 frame:0
TX packets:495 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:64463 (64.4 KB) TX bytes:83013 (83.0 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:992 errors:0 dropped:0 overruns:0 frame:0
TX packets:992 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:74912 (74.9 KB) TX bytes:74912 (74.9 KB)
我们使用的网卡名叫 ens33
,IP地址是 192.168.79.128
,子网掩码是 255.255.255.0
- 获取网关
$ ip route
default via 192.168.79.2 dev ens33 onlink
192.168.79.0/24 dev ens33 proto kernel scope link src 192.168.79.128
网关地址是 192.168.79.2
,我们可以把DNS也指定为网关地址
- 修改配置文件
sudo vi /etc/network/interfaces
修改配置文件内容如下,请根据自己的实际情况填写
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.79.128 # IP地址
netmask 255.255.255.0 # 子网掩码
gateway 192.168.79.2 # 网关
dns-nameserver 192.168.79.2 # DNS服务器
- 重启网络
sudo systemctl restart networking