当前位置:首页 > linux > 正文

linux配置ip地址的方法

  • linux
  • 2024-05-03 02:02:16
  • 6298

1. 使用 ifconfig 命令
ifconfig [interface] [IP address] [netmask]
例如:
ifconfig eth0 192.168.1.100 255.255.255.0
2. 使用 nmcli 工具
nmcli con mod [interface] ipv4.address [IP address]/[netmask]
例如:
nmcli con mod eth0 ipv4.address 192.168.1.100/24
3. 使用 ip 命令
ip addr add [IP address]/[netmask] dev [interface]
例如:
ip addr add 192.168.1.100/24 dev eth0
4. 使用 netplan
Netplan 是一个用于管理 Linux 网络配置的新工具。 要使用 netplan,您需要创建一个 YAML 配置文件(通常位于 /etc/netplan 中)。
以下是示例配置文件:
yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
应用 netplan 配置:
sudo netplan apply
5. 使用 Systemd 网络服务
sudo systemctl enable systemd-networkd.service
sudo systemctl start systemd-networkd.service
创建网络配置文件(通常位于 /etc/systemd/network 中):
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
重新加载网络服务:
sudo systemctl reload systemd-networkd.service
注意事项:
确保选择的 IP 地址未被网络上的其他设备使用。
配置 netmask 以匹配您的子网掩码。
如果需要,请配置默认网关。
更改 IP 地址后,可能需要重新启动网络服务或重新启动系统才能使其生效。