Ubuntu 16.04 Server 设置静态IP

1、前言

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

2、环境

  • 系统:Ubuntu Server 16.04
  • 虚拟机:VM 15.X

3、解决方案

1. 查看IP信息

经过命令行查看当前IP信息。bash

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,则输入以下代码:oop

# 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的命令,可是我发现有些时候那些命令无法使用。
所以,最简单的方法就是直接重启系统。code

sudo reboot

4、测试是否OK

执行命令orm

ping [局域网IP] | [外网IP] | [具体域名]
  • 若是能访问局域网的其余IP,说明IP设置正常
  • 若是能访问外网IP,说明IP设置正常,不然就是DNS问题
  • 若是能访问具体外网域名,说明IP设置正常,不然就是DNS问题
相关文章
相关标签/搜索