linux-ubuntu配置静态IP,适用于14.04,16.04和17.10

linux-ubuntu配置静态IP,适用于14.04,16.04和17.10

Ubuntu 的网络配置文件主要有以下几种:
IP地址配置文件、主机名称配置文件、DNS配置文件。

对于14.04和16.04版本

1.配置IP地址

/etc/network/interfaces

1.1. 以DHCP方式配置网卡

sudo vim /etc/network/interfaces
--------------------------------------------
auto eth0
iface eth0 inet dhcp
--------------------------------------------
重启网卡
sudo /etc/init.d/networking     restart

也可以直接输入下面的命令来获取地址
sudo dhclient eth0

1.2. 为网卡配置静态IP地址

sudo vim /etc/network/interfaces
--------------------------------------------
auto eth0
iface eth0 inet static
address 192.168.1.11
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 202.106.0.20
dns-nameservers 8.8.8.8
--------------------------------------------
重启网卡
sudo /etc/init.d/networking restart

1.3. 设定第二个IP地址(虚拟IP地址)

sudo vim /etc/network/interfaces
--------------------------------------------
auto eth0:1
iface eth0:1 inet static
address 192.168.1.60
netmask 255.255.255.0
gateway x.x.x.x
network x.x.x.x
broadcast x.x.x.x
--------------------------------------------
重启网卡:
sudo /etc/init.d/networking restart

2.修改主机名

查看当前主机名:
sudo /bin/hostname

2.1.设置当前主机名称

sudo /bin/hostname myhost

2.2.永久修改主机名

sudo vim /etc/hostname

3.修改DNS配置

3.1.修改/etc/hosts文件使用本机的静态查询

sudo vim /etc/hosts

3.2.配置DNS服务器来进行查询

sudo vim /etc/resolv.conf
-------------------------------
nameserver 192.168.1.2
nameserver 202.106.0.20
nameserver 8.8.8.8
--------------------------------
重启网卡
sudo /etc/init.d/networking restart

对于Ubuntu 17.10

1.配置静态IP

1.1.可以使用ip add查看网卡名称

ip add

1.2.编辑网卡配置文件

对于17.10,网卡的配置文件已经改变

sudo vim /etc/netplan/01-netcfg.yaml 
-----------------------------------------------------
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no	#dhcp4关闭
      dhcp6: no	#dhcp6关闭
      addresses: [192.168.1.65/24]
      gateway4: 192.168.1.1
      nameservers:
              addresses: [202.106.0.20,4.4.4.4]
-----------------------------------------------------
通过改配置文件,可以设置IP,网关和DNS地址

1.3.执行以下命令让配置生效

netplan apply

2修改网卡名称

新装的Ubuntu17.10默认网卡名字一般是enns33,可以修改成通用的eth0,

posted @ 2023-05-17 17:42  天生帅才  阅读(272)  评论(0编辑  收藏  举报
// 百度统计