如何在 Ubuntu Server 18.04 上设置静态 IP

fin*_*l20 41 server networking ip 18.04

我看到有人说设置静态ip的文件还在 /etc/network/interfaces

而且我看到其他人说 18.04 现在开始了/etc/netplan(人们似乎对此不满意)

我试过把这个:

version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.1.9/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]
Run Code Online (Sandbox Code Playgroud)

在我/etc/netplan/50-cloud-init.yaml和做中,sudo netplan apply但这只会终止服务器与互联网的连接。

Lud*_*uty 30

/etc/netplan/50-cloud-init.yaml由于使用了 CloudInit 并将生成该文件,因此所有告诉您直接编辑的答案都是错误的。在 Ubuntu 18.04.2 中,它清楚地写在文件中:

$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eno1:
            dhcp4: true
    version: 2
Run Code Online (Sandbox Code Playgroud)

因此,/etc/cloud/cloud.cfg.d/如果您仍想使用 CloudInit ,则不应编辑该文件,而应编辑该文件。

另一种方法是首先通过创建一个空文件/etc/cloud/cloud-init.disabled(参见https://cloudinit.readthedocs.io/en/latest/topics/boot.html)完全禁用 CloudInit ,然后其他答案就可以了。在 Ubuntu 18.04.2 下,我不得不使用dpkg-reconfigure cloud-init它来考虑文件/etc/cloud/cloud-init.disabled. 我觉得这有点奇怪。

我建议您重命名文件(不是正确的名称,因为50-cloud-init.yaml让我们认为它仍然使用 CloudInit)。

然后你可能会得到一个/etc/netplan/01-netcfg.yaml包含以下配置的文件名。请注意networkd渲染器的使用,而不是NetworkManager因为配置在服务器上。

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: no
      addresses: [192.168.1.246/24]
      gateway4: 192.168.1.1
      nameservers:
         addresses: [192.168.1.1]
Run Code Online (Sandbox Code Playgroud)

  • 它工作得很好。这应该是最好的答案。不应修改所述的 50-cloud-init.yaml。 (3认同)
  • 如果仍在使用 CloudInit,根据 veperr 的回答(至少对我在 Ubuntu Server 18.04.3 上),您需要执行 `sudo cloud-init clean -r` 来进行更改。 (3认同)

Ben*_*ari 16

这是在Ubuntu-Server 18.04 和 20.04 中设置静态 IP 指令

$ sudo nano /etc/netplan/50-cloud-init.yaml
Run Code Online (Sandbox Code Playgroud)

然后替换您的配置,例如,以下几行:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:  # Your ethernet name.
     dhcp4: no
     addresses: [192.168.1.137/24]
     gateway4: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]
Run Code Online (Sandbox Code Playgroud)

应用更改:

$ sudo netplan apply
Run Code Online (Sandbox Code Playgroud)

如果您遇到一些问题,请执行:

$ sudo netplan --debug apply
Run Code Online (Sandbox Code Playgroud)

[注意]:

  • /24 相当于 255.255.255.0
  • ens160 是您的以太网名称,您可以使用 $ ifconfig
  • Ubuntu 16.04 和 14.04网络接口配置有不同的方法。
  • 该文件采用YAML 格式:使用空格,不使用制表符。

  • 我不会这样做,因为该文件是由 CloudInit 生成的 (5认同)

小智 8

我找到了另一种使用 cloud-init 的方法。

  1. 编辑文件/etc/cloud/cloud.cfg.d/50-curtin-networking.cfg- 内容似乎与 /etc/netplan.conf 中的内容相同。
  2. cloud-init使用以下命令清理、重新启动并重新初始化:

    sudo cloud-init clean -r
    
    Run Code Online (Sandbox Code Playgroud)
  3. 就是这样!您的系统将重新启动,cloud-init将重新初始化并获取更改/etc/cloud/cloud.cfg.d/50-curtin-networking.cfg并将其应用于/etc/netplan/50-cloud-init.yaml,一切都会好起来的。用 验证ifconfig


bad*_*007 7

Ubuntu 18.04 现在使用 Netplan 来配置网络接口,所以配置必须在文件中完成/etc/netplan/50-cloud-init.yaml,文档建议不要再乱用旧文件/etc/network/interfaces。我已经在我的 Ubuntu Server 虚拟机上使用了这个配置,到目前为止它可以工作,只要确保信息正确;该optional: true设置通过不验证接口是否连接来加快启动时间,这是默认值,也不需要声明未使用的值,例如 DHCP,如果它们不存在,它们将被视为禁用,也是Ubuntu Server 中的默认渲染器是networkd这样的,因此无需声明它。从你的帖子中获取信息,它应该是这样的:

network:
    ethernets:
        eht0:
            addresses:
            - 192.168.1.9/24
            gateway4: 192.168.1.1
            nameservers:
                addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]
            optional: true
    version: 2
Run Code Online (Sandbox Code Playgroud)

保存文件后,运行sudo netplan --debug applydebug标志将输出更多信息并有助于检测任何错误。检查以太网电缆,如果在虚拟检查 VM 配置。如果使用 WLAN,我已经读到设置起来有点棘手,但我还没有使用此服务器版本设置连接到 WiFi 的机器。

如果你想了解更多关于 Netplan 的信息,有一个网站,它有一些基本的配置示例。

https://netplan.io/


Hub*_*leT 5

配置文件为YAML 格式:配置文件时不要使用TAB。它只适用于SPACE