在 Ubuntu 服务器 16.04.2 LTS 上设置静态 IP 地址

在 Ubuntu 服务器 16.04.2 LTS 上设置静态 IP 地址

在将 Ubuntu 16.04.2 LTS 服务器上的 IP 从 DHCP 更改为静态时,我遇到了奇怪的问题。

那是我的 /etc/网络/接口

# The primary network interface
auto enp2s0f0
iface enp2s0f0 inet static
               address 192.168.0.105
               netmask 255.255.255.0
               gateway 192.168.0.1

但每次我重启系统时都会在启动网络子系统时收到错误。当我尝试这样做时,sudo service networking start/restart我收到错误。

现在我已通过以下方式设置 IP:

sudo ifconfig enp2s0f0 192.168.0.105 netmask 255.255.255.0
sudo route add default gw 192.168.0.1 enp2s0f0

但这不是办法。只要我重新启动系统,它应该就可以正常工作,但现在我必须再次手动设置 IP。

不知道问题是什么。

● networking.service - Raise network interfaces
   Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: enabled)
  Drop-In: /run/systemd/generator/networking.service.d
           └─50-insserv.conf-$network.conf
   Active: failed (Result: exit-code) since Fri 2018-05-11 11:27:24 MSK; 42s ago
     Docs: man:interfaces(5)
  Process: 22614 ExecStart=/sbin/ifup -a --read-environment (code=exited, status=1/FAILURE)
  Process: 22609 ExecStartPre=/bin/sh -c [ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle (code=exited, status=1/FAILURE)
 Main PID: 22614 (code=exited, status=1/FAILURE)

May 11 11:27:24 SPECLAB systemd[1]: Starting Raise network interfaces...
May 11 11:27:24 SPECLAB sh[22609]: /etc/network/interfaces:2: misplaced option
May 11 11:27:24 SPECLAB sh[22609]: ifquery: couldn't read interfaces file "/etc/network/interfaces"
May 11 11:27:24 SPECLAB ifup[22614]: /etc/network/interfaces:2: misplaced option
May 11 11:27:24 SPECLAB ifup[22614]: /sbin/ifup: couldn't read interfaces file "/etc/network/interfaces"
May 11 11:27:24 SPECLAB systemd[1]: networking.service: Main process exited, code=exited, status=1/FAILURE
May 11 11:27:24 SPECLAB systemd[1]: Failed to start Raise network interfaces.
May 11 11:27:24 SPECLAB systemd[1]: networking.service: Unit entered failed state.
May 11 11:27:24 SPECLAB systemd[1]: networking.service: Failed with result 'exit-code'.

答案1

尝试按照下列方式设置您的网络接口:

auto enp2s0f0
iface enp2s0f0 inet static
        address 192.168.0.105
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1

不要在地址、网络等之前添加更多空白字符。我认为一个tab字符就足够了。

相关内容