ubuntu16.04下配置静态ip

ubuntu下配置静态ip

1.先获取ip基本信息

 ifconfig
enp3s0    Link encap:以太网  硬件地址 2c:4d:54:65:de:6e  
          inet 地址:192.168.199.118  广播:192.168.199.255  掩码:255.255.255.0
          inet6 地址: fe80::127f:9874:8077:cfbc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  跃点数:1
          接收数据包:850211 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:562755 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:1124337691 (1.1 GB)  发送字节:80760594 (80.7 MB)

lo        Link encap:本地环回  
          inet 地址:127.0.0.1  掩码:255.0.0.0
          inet6 地址: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  跃点数:1
          接收数据包:10036 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:10036 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:46777037 (46.7 MB)  发送字节:46777037 (46.7 MB)

2.获取路由基本信息netstat -rn

jiqing@Ubuntu:/usr/local/nginx/conf/vhost$ netstat -rn
内核 IP 路由表
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.199.1   0.0.0.0         UG        0 0          0 enp3s0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 enp3s0
192.168.199.0   0.0.0.0         255.255.255.0   U         0 0          0 enp3s0

3.修改配置文件

 sudo vim /etc/network/interfaces
# set static ip
auto enp3s0
iface enp3s0 inet static
address 192.168.199.126
gateway 192.168.199.1
netmask 255.255.255.0
broadcast 192.168.199.255

4.修改dns,最好把路由也加入

sudo vim /etc/resolvconf/resolv.conf.d/base
nameserver 192.168.199.1
nameserver 114.114.114.114
nameserver 8.8.8.8

5.重启网卡

sudo service networking restart

如果还不生效,就重启电脑。