参考 https://www.cnblogs.com/yaohong/p/11593989.html
1. 系统版本
首先,先查看当前系统版本,16和18修改方法不一样的
$ cat /etc/issue
Ubuntu 16.04.4 LTS
2. 基本信息查找
2.1 网卡信息
当前地址查看,主要是找到当前机器地址的IP的网卡,如下即是eno3
$ ifconfig -a
2.2 网关信息
另外,查看网关地址,其中以0.0.0.0
开头的则是网关
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.3.1 0.0.0.0 UG 100 0 0 eno3
2.3 DNS信息
在局域网内一台可以使用的电脑上使用命令查找DNS信息
yangliu@ylmac ~ % nslookup baidu.com
Server: 192.168.3.1
Address: 192.168.3.1#53
Non-authoritative answer:
Name: baidu.com
Address: 220.181.38.148
Name: baidu.com
Address: 39.156.69.79
其中Server: 192.168.3.1
即为DNS信息
3. 修改网关配置、DNS配置并生效
之后修改网关配置文件,找到文件sudo vim /etc/network/interfaces
- 修改为静态IP配置相关,以下为示例
# 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
# auto enp0s31f6
# iface enp0s31f6 inet dhcp
# 动态IP代码注释,以下为设置静态IP
auto enp0s31f6
iface enp0s31f6 inet static
address 192.168.3.119
netmask 255.255.255.0
gateway 192.168.3.1
然后修改DNS配置信息
$ sudo vim /etc/resolvconf/resolv.conf.d/head
最终DNS配置文件如下
➜ ~ cat /etc/resolvconf/resolv.conf.d/head
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.3.1
然后两条执行命令,使配置生效
sudo ip addr flush enp7s0
sudo systemctl restart networking.service
然后通过ifconfig
就会发现IP已经更改,注意不要和局域网的其他IP冲突