Ubuntu静态IP设置

2023-09-19 00:00:00
/
0 点赞
/
311 阅读
2023-09-19

根据系统情况选择不同的方法

要设置静态IP地址,你可以编辑/etc/network/interfaces文件或使用netplan工具来配置网络接口。下面我将为你提供两种方法。

方法一:使用/etc/network/interfaces文件

  1. 使用文本编辑器(如nanovim)打开/etc/network/interfaces文件:

    sudo nano /etc/network/interfaces
    
    1
  2. 在文件中找到你要配置静态IP的网络接口(通常是eth0enpXsY,其中X和Y是数字),并将其配置为静态IP。例如:

    auto eth0
    iface eth0 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        gateway 192.168.1.1
    
    1
    2
    3
    4
    5

    addressnetmaskgateway的值替换为你的网络配置。

  3. 保存并关闭文件。

  4. 重启网络服务以应用更改:

    sudo systemctl restart systemd-networkd
    
    1

方法二:使用netplan工具

  1. 使用文本编辑器打开/etc/netplan/目录下的配置文件(通常是以.yaml结尾的文件):

    sudo nano /etc/netplan/01-netcfg.yaml
    
    1
  2. 在文件中找到你要配置静态IP的网络接口(通常是eth0enpXsY,其中X和Y是数字),并将其配置为静态IP。例如:

# `gateway4` has been deprecated, use default routes instead.
# See the 'Default routes' section of the documentation for more details.

   network:
     version: 2
     renderer: networkd
     ethernets:
       eth0:
         dhcp4: no
         addresses: [192.168.1.100/24]
         # gateway4: 192.168.1.1
         routes:
           - to: default
             via: 192.168.1.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14

addressesgateway4的值替换为你的网络配置。

  1. 保存并关闭文件。

  2. 应用更改:

    sudo netplan apply
    
    1

无论你选择哪种方法,设置静态IP后,你的网络接口将使用指定的IP地址、子网掩码和网关。记得根据你的网络环境进行适当的配置。

版权属于:

那棵树看起来生气了

本文链接:

https://dengyb.com/archives/6.html(转载时请注明本文出处及文章链接)