分享交流
合作共赢!

Ubuntu各版本配置静态IP地址方法总结

一、Ubuntu Desktop14.04

Ubuntu14.04

cat  /etc/network/interfaces

默认已经有lo: 系统给定的网络回环接口

eth0的配置(Ubuntu Desktop14.04)

1.  修改/etc/network/interfaces文件,添加配置信息

cd  /etc/network
vim  interfaces

键入如下命令:

auto  eth0
iface  eth0  inet  static

address  192.168.1.1
netmask  255.255.255.0
network  192.168.1.0
gateway  192.168.1.254
broadcast  192.168.1.255
dns-nameservers 8.8.8.8 114.114.114.114
配置说明:
auto lo
iface lo inet loopback

auto ens33(ens33为以太网卡,根据实际名称填写)
iface ens33 inet static
address 192.168.1.8(IP地址,要和物理机在同一网段,且不要和局域网内其他设备IP冲突,查看方法见下)
gateway 192.168.1.1(默认网关,和物理机一样)
netmask 255.255.255.0(子网掩码,和物理机一样)
dns-nameservers 8.8.8.8 114.114.114.114

2.  也可添加另一块网卡eth1

auto  eth1
iface  eth1  inet  static
address  192.168.1.10
netmask  255.255.255.0
network  192.168.1.0
gateway  192.168.1.254
broadcast  192.168.1.255
dns-nameserver 8.8.8.8

:wq 存盘退出

3. 配置永久dns

vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)

nameserver x.x.x.x
nameserver x.x.x.x

4.  重新启动网络服务:

sudo  /etc/init.d/networking  restart

5.  使eth1生效,激活eth1

ifup  eth1

6.  开启网内的访问权限:

sudo  ufw  allow  from  192.168.1.250/24

说明:192.168.1.250为另一台ubuntu server 16.04的IP地址

7.  切换到ubuntu server 16.04虚拟机上,输入命令:

sudo  ufw  allow  from  192.168.1.10/24

说明:192.168.1.10是ubuntu desktop上刚添加的网卡eth1的IP地址

8. 两台虚拟机均重新启动

9.  从ubuntu server服务器ping 客户端ubuntu desktop

ping  192.168.1.10    连通

从ubuntu desktop客户端ping 服务器ubuntu server

ping  192.168.1.250. 连通

10.  修改客户端ubuntu desktop的eth0的IP地址:

ifconfig  eth0  192.168.1.100  netmask  255.255.255.0

再看一下是否被修改

ifconfig  -a

11.  使修改后的IP地址生效:

ifconfig  eth0  up

互相ping   仍然可以ping通

二、Ubuntu16.04

1.配置DNS

sudo vi /etc/resolvconf/resolv.conf.d/base #(这个文件默认是空的)

在里面写入需要设置的DNS信息,如:nameserver 8.8.8.8

如果有多个DNS就一行一个

修改好保存,然后使其生效

sudo resolvconf -u

三、Ubuntu18.04

在 Ubuntu 18.04 LTS 中配置 IP 地址的方法和以往使用的配置方法有很大的不同。和旧版本的不同之处在于,Ubuntu 18.04 使用 Netplan 来配置 IP 地址,Netplan 是一个新的命令行网络配置工具。其实在 Ubuntu 17.10 的时候 Ubuntu 开发者就已经介绍过 Netplan 了。接下来要介绍的新的 IP 配置方法不会再用到 /etc/network/interfaces 这个文件,取而代之的是一个 YAML 文件。默认的 Netplan 配置文件一般在 /etc/netplan 目录下。

在这篇教程中,我们会去学习在 Ubuntu 18.04 LTS 的最小化服务器中配置静态 IP 和动态 IP 。

1.静态IP

找到ip地址默认配置文件:

root@cnhshacdc1vh106:/etc/netplan# ls
50-cloud-init.yaml

此配置文件初始内容如下:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
          ens33:
                dhcp4: true

配置静态ip地址如下(注意:在 Ubuntu 18.04 里,这个配置文件的每一行都必须靠空格来缩进,不能用 TAB 来代替,否则配置会不起作用。如上所示的配置文件中每行的缩进都是靠空格键实现的;另外子网掩码不再是netmask形式,而是掩码位数的数字):

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
     ens33: #配置的网卡名称,使用ifconfig -a查看得到,且必须是空格缩进,netplan只认空格
       dhcp4: false #no-dhcp4开启 true-dhcp4开启
       dhcp6: false #no-dhcp6开启 ture-dhcp6关闭
       addresses: [192.168.239.135/24, ] #设置本机IP及掩码,这个逗号可加可不加,若同时设置ipv4和ipv6参考:[192.168.239.135/24, "2001:1::1/64"]
       gateway4: 192.168.239.254 #设置ipv4的默认网关
       gateway6: 2001:1::1 #设置ipv6的默认网关
       
       nameservers:  #设置DNS服务器
         addresses: [114.114.114.114,8.8.8.4]  #多个DNS服务器之间用逗号隔开

使应用配置生效:

root@cnhshacdc1vh106:/etc/netplan# netplan apply

查看配置结果,如下即生效:

root@cnhshacdc1vh106:/etc/netplan# ifconfig
enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 161.92.142.31  netmask 255.255.252.0  broadcast 161.92.143.255
        inet6 fe80::e2d5:5eff:feae:c17f  prefixlen 64  scopeid 0x20<link>
        ether e0:d5:5e:ae:c1:7f  txqueuelen 1000  (Ethernet)
        RX packets 7437487  bytes 3888770451 (3.8 GB)
        RX errors 0  dropped 87037  overruns 0  frame 0
        TX packets 10589  bytes 950975 (950.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 11  memory 0x92f00000-92f20000

2.动态IP

与静态ip相似,只不过编辑配置文件,将DHCP功能打开,不设置ip地址网关和掩码,使其自动获取,如上此文件的默认配置状态。

3.DNS配置

查看/etc/resolv.conf文件内容:

root@iZad705ziue25txnr4sv5lZ:~# cat /etc/resolv.conf 
# 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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.

nameserver 127.0.0.53
options timeout:2 attempts:3 rotate single-request-reopen

可以看出,此文件由systemd-resolved管理,查看systemd-resolved状态:

root@iZad705ziue25txnr4sv5lZ:~# systemctl status systemd-resolved

所以配置dns可以在systemd-resolved的配置文件/etc/systemd/resolved.conf中进行配置

root@iZad705ziue25txnr4sv5lZ:~# more /etc/systemd/resolved.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
DNS=114.114.114.114
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

配置后需要重启systemd-resolved服务器生效,然后再通过

root@iZad705ziue25txnr4sv5lZ:/etc/systemd/network# systemctl status systemd-resolved

然后再查看DNS信息如下,Global DNS Server就被修改为114.114.114.114

root@iZad705ziue25txnr4sv5lZ:~# systemd-resolve --status
Global
         DNS Servers: 114.114.114.114
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test

重启DNS:

root@iZad705ziue25txnr4sv5lZ:~# /etc/init.d/resolvconf restart
[ ok ] Restarting resolvconf (via systemctl): resolvconf.service.

4.问题追踪

如果在应用配置的时候有出现问题的话,可以通过如下的命令来查看刚才配置的内容出了什么问题。

root@ubuntu01:~# netplan --debug apply
** (generate:2914): DEBUG: 10:26:02.384: Processing input file /etc/netplan/01-network-manager-all.yaml..
** (generate:2914): DEBUG: 10:26:02.384: starting new processing pass
** (generate:2914): DEBUG: 10:26:02.385: ens33: setting default backend to 2
** (generate:2914): DEBUG: 10:26:02.385: Configuration is valid
** (generate:2914): DEBUG: 10:26:02.385: Generating output files..
** (generate:2914): DEBUG: 10:26:02.385: networkd: definition ens33 is not for us (backend 2)
DEBUG:no netplan generated networkd configuration exists
DEBUG:netplan generated NM configuration changed, restarting NM
DEBUG:ens33 not found in {}
DEBUG:Merged config:
network:
  bonds: {}
  bridges: {}
  ethernets:
    ens33:
      addresses:
      - 192.168.239.135/24
      dhcp4: false
      dhcp6: false
      gateway4: 192.168.239.254
      nameservers:
        addresses:
        - 114.114.114.114
        - 8.8.6.6
  vlans: {}
  wifis: {}

DEBUG:Skipping non-physical interface: lo
DEBUG:device ens33 operstate is up, not changing
DEBUG:{}
DEBUG:netplan triggering .link rules for lo
DEBUG:netplan triggering .link rules for ens33
赞(0) 打赏
未经允许不得转载:琼杰笔记 » Ubuntu各版本配置静态IP地址方法总结

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册