Ubuntu18.04 Server设置静态IP

前言:Ubuntu18.04 server安装成功之后,有分配到ip,可以上网。但是遇到重启路由,Ubuntu server端ip地址可能会变掉。鉴于在局域网内,一般还是选择固定server端ip。

方案:下面有两种方式。ifconfig or ip a查询当前的网卡enp0s31f6,or ens33 ..

  • Version >=ubuntu18.04,增加透过netplan来设定固定ip的方式,sudo vim /etc/netplan/01-network-manager-all.yaml(文件名可能不同),改完保存,然后sudo netplan apply。
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens33:
      dhcp4: no
      addresses: [192.168.1.123/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 114.114.114.114]

 注意yaml语法格式,之后要加空格

  • Version < ubuntu18.04 (or ubuntu18,这里仅示意Ubuntu18之后有改变,不确定具体的版本),通过修改/etc/network/interfaces,并配置dns 【将resolved.conf里的DNS注释拿掉,加入需要的dns】,设定完之后,reboot重启,不要单单的/etc/init.d/networking restart,这样不一定起作用。
  1. sudo vim /etc/network/interfaces
  2. sudo vim /etc/systemd/resolved.conf【修改/etc/resolve.conf不起作用,reboot之后又会被默认值覆盖】
#auto lo
#iface lo inet loopback
auto enp0s31f6
iface enp0s31f6 inet static
address 192.168.1.111
netmask 255.255.255.0
gateway 192.168.1.1
#  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=8.8.8.8 114.114.114.114
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

小结:Ubuntu18.04网络配置有些改变,server已经可以采用netplan配置,也挺方便;注意传统的配置ip/dns的方式,有所改变,/etc/resolvconf/resolv.conf.d/base 已经没有了,需要修改/etc/systemd/resolved.conf。

参考

https://blog.csdn.net/qq_42975842/article/details/81705244

https://blog.csdn.net/qq_36937342/article/details/80876385

 

 

你可能感兴趣的:(Linux,other)