当前位置: 移动技术网 > 科技>操作系统>Linux > Ubuntu 16.04 Server 设置静态IP

Ubuntu 16.04 Server 设置静态IP

2018年12月02日  | 移动技术网科技  | 我要评论

一、前言

最近需要在虚拟机当中装个ubuntu server 16.04的系统,但是在虚拟机安装的时候,并不像ubuntu server 18.04那样能一步步的进行配置,因此导致装好后的虚拟机是动态ip地址。而该虚拟机要作为测试服务器来使用,所以要将ip地址设置为静态ip。

二、环境

  • 系统:ubuntu server 16.04
  • 虚拟机:vm 15.x

三、解决方案

1. 查看ip信息

通过命令行查看当前ip信息。

ifconfig
# 输出结果如下:
ens33     link encap:ethernet  hwaddr 00:0c:29:98:5f:81  
          inet addr:192.168.4.246  bcast:192.168.4.255  mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe98:5f81/64 scope:link
          up broadcast running multicast  mtu:1500  metric:1
          rx packets:28536 errors:0 dropped:0 overruns:0 frame:0
          tx packets:17938 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          rx bytes:3741540 (3.7 mb)  tx bytes:2286437 (2.2 mb)

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:193 errors:0 dropped:0 overruns:0 frame:0
          tx packets:193 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          rx bytes:16356 (16.3 kb)  tx bytes:16356 (16.3 kb)

2. 安装vim

在新安装的系统当中,默认安装vi编辑器,但是本人觉得这个编辑器的操作没有vim熟悉,因此要安装vim编辑器。

# 先更新apt-get源
sudo apt-get update
# 安装vim
sudo apt-get install vim

3. 修改配置

打开修改文件

修改/etc/network/interfaces文件。注意:是interfaces,有s

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

# the primary network interface
auto ens33
iface ens33 inet dhcp

如果要修改为静态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

# the primary network interface
auto ens33
iface ens33 inet static
address 192.168.4.246
netmask 255.255.255.0
gateway 192.168.4.1
dns-nameservers 8.8.8.8

注意:如果已经有网卡ens33,则按对应名称编写即可。
配置说明:

  • auto ens33:使用的网络接口
  • iface ens33 inet static:ens33这个接口,使用静态ip设置
  • address 192.168.4.256:设置ip地址
  • netmask 255.255.255.0:设置子网掩码
  • gateway 192.168.4.1:设置网关
  • dns-nameservers 8.8.8.8:设置dns服务器地址

修改完之后,按esc键,然后输入:qw即可保存并关闭文件。

4. 重启系统

在网上找到的一些教程当中,要使用刷新ip的命令,但是我发现有些时候那些命令没法使用。
因此,最简单的方法就是直接重启系统。

sudo reboot

四、测试是否ok

执行命令

ping [局域网ip] | [外网ip] | [具体域名]
  • 如果能访问局域网的其他ip,说明ip设置正常
  • 如果能访问外网ip,说明ip设置正常,否则就是dns问题
  • 如果能访问具体外网域名,说明ip设置正常,否则就是dns问题

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网