温馨提示×

Linux下如何设置网络接口IP地址

小樊
82
2024-09-07 09:43:40
栏目: 智能运维

在 Linux 下,您可以使用命令行工具来设置网络接口的 IP 地址

  1. 临时设置网络接口 IP 地址:

使用 ifconfig 命令可以临时设置网络接口的 IP 地址。例如,要将 IP 地址设置为 192.168.1.10,子网掩码为 255.255.255.0,可以使用以下命令:

sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0

请注意,这种方法设置的 IP 地址在系统重启后会失效。

  1. 永久设置网络接口 IP 地址:

要永久设置网络接口的 IP 地址,需要编辑网络配置文件。对于基于 Debian 的系统(如 Ubuntu),网络配置文件位于 /etc/network/interfaces。对于基于 RHEL 的系统(如 CentOS、Fedora),网络配置文件位于 /etc/sysconfig/network-scripts/ifcfg-eth0

以下是一个基于 Debian 的系统的示例配置:

auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

以下是一个基于 RHEL 的系统的示例配置:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

编辑完成后,保存并关闭文件。然后,重启网络服务以应用更改:

对于基于 Debian 的系统:

sudo systemctl restart networking

对于基于 RHEL 的系统:

sudo systemctl restart network

现在,您已经成功设置了网络接口的 IP 地址。您可以使用 ifconfigip addr show 命令查看新的 IP 地址。

0