BuyVM VPS 配置内网 IP

BuyVM 的 VPS 默认会提供一个内网 IP,但并没有绑定网卡,本文记录如何配置开启。

博主的几台 BuyVM VPS 都是 Las Vegas 的,系统均安装 Ubuntu 20.04,刚好有个服务要做负载均衡,可以用来尝试。

在 VPS 管理面板的“Networking”选项卡中,可以看到一个公网 IP 和一个 172 开头的内网IP,它们的 Status 都是开启状态。貌似默认情况下并没有在网卡中绑定这个内外IP,也可能是俺没有找到?因为几台 VPS 之间用内网 IP 无法 ping 通。

1、SSH 连接 VPS 后,使用 ip aifconfig 命令查看网卡详情,提示找不到 ifconfig 命令就先安装:apt install net-tools

看到默认只有一个网卡,绑定了公网 IP:

    root@localhost:~# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 209.202.122.123  netmask 255.255.255.0  broadcast 209.202.60.255
            inet6 fe80::216:c9ff:fe62:953f  prefixlen 64  scopeid 0x20<link>
            ether 00:16:c9:62:95:8f  txqueuelen 1000  (Ethernet)
            RX packets 76302  bytes 126219152 (126.2 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 43962  bytes 6028503 (6.0 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 319  bytes 26951 (26.9 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 319  bytes 26951 (26.9 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2、开始配置。网上有些教程让编辑 /etc/network/interfaces ,但在没有安装桌面环境的 Ubuntu 20.04 里找不到这个 /etc/network/interfaces 文件,包括 BuyVM VPS 中安装的 Ubuntu 20.04 里也没有,此时要用 Netplan 来配置(Netplan 是一个Linux 发行版上配置网络连接的命令行工具,使用 YAML 描述文件来配置网络接口)。
所以直接编辑相关配置文件:vi /etc/netplan/01-netcfg.yaml,其中 01-netcfg.yaml 可能每台机器不同。打开后如下,默认只配置了 eth0,在下方添加 eth1:dhcp4: yes 即可。因为 BuyVM 官方已为我们分配好了 IP 地址,这里 dhcp4: yes 即开启从 DHCP 服务器动态获取 IP 了;如需配置静态 IP 可看文末参考资料。

编辑前可先备份文件;添加时注意缩进,否则会失败。

    # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          dhcp4: yes
    # 添加下面两行,注意缩进。
        eth1:
          dhcp4: yes

编辑完成后,用 netplan apply 命令使配置生效,可能需要等待个几十秒。

成功后 ip aifconfig 命令再次查看网络连接,成功绑定局域网 IP 了,几台机器间也能成功 ping 通:

    root@localhost:~# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 209.202.122.123  netmask 255.255.255.0  broadcast 209.202.60.255
            inet6 fe80::216:c9ff:fe62:958f  prefixlen 64  scopeid 0x20<link>
            ether 00:16:c9:62:95:8f  txqueuelen 1000  (Ethernet)
            RX packets 87250  bytes 141805357 (141.8 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 48492  bytes 6463946 (6.4 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.16.196.14  netmask 255.255.0.0  broadcast 172.16.255.255
            inet6 fe80::216:deff:fe78:456  prefixlen 64  scopeid 0x20<link>
            ether 00:16:de:78:04:56  txqueuelen 1000  (Ethernet)
            RX packets 1373  bytes 298746 (298.7 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 14  bytes 1596 (1.5 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 325  bytes 27567 (27.5 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 325  bytes 27567 (27.5 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

参考资料:

如何在 Linux 上使用网络配置工具 Netplan

Netplan——新一代网络配置工具

BuyVM VPS 配置内网 IP》上有 1 条评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注