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問題
相關文章
相關標籤/搜索