在 Linux 系统中设置 IP 地址的方法有多种,具体取决于你使用的发行版和网络管理工具,以下是几种常见的方法:
使用ip
命令(临时设置)

1、查看当前网络接口:
```bash
ip link show
```
这将列出所有网络接口及其状态。
2、设置 IP 地址:
```bash
sudo ip addr add 192.168.1.10/24 dev eth0

```
其中192.168.1.10
是你要设置的 IP 地址,/24
是子网掩码,eth0
是网络接口名称。
3、删除 IP 地址(如果需要):
```bash
sudo ip addr del 192.168.1.10/24 dev eth0
```
使用ifconfig
命令(临时设置)
1、查看当前网络接口:

```bash
ifconfig -a
```
2、设置 IP 地址:
```bash
sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0
```
其中192.168.1.10
是你要设置的 IP 地址,255.255.255.0
是子网掩码,eth0
是网络接口名称。
3、启用或禁用网络接口:
```bash
sudo ifconfig eth0 up
sudo ifconfig eth0 down
```
使用nmtui
工具(基于 NetworkManager,图形界面)
1、启动 nmtui:
```bash
sudo nmtui
```
2、选择 "Edit a connection",然后按回车键。
3、选择一个连接并编辑,或者创建一个新的连接。
4、配置 IPv4 设置:
选择 "IPv4 Settings"。
选择 "Manual"。
输入你要设置的 IP 地址、子网掩码和网关。
5、保存更改。
修改/etc/network/interfaces
文件(永久性设置,适用于 Debian/Ubuntu)
1、编辑/etc/network/interfaces
文件:
```bash
sudo nano /etc/network/interfaces
```
2、添加或修改以下内容:
```plaintext
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
```
3、重启网络服务:
```bash
sudo systemctl restart networking
```
修改/etc/sysconfig/network-scripts/ifcfg-eth0
文件(永久性设置,适用于 RHEL/CentOS)
1、编辑/etc/sysconfig/network-scripts/ifcfg-eth0
文件:
```bash
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
```
2、添加或修改以下内容:
```plaintext
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.10
PREFIX=24
GATEWAY=192.168.1.1
```
3、重启网络服务:
```bash
sudo systemctl restart network
```
通过以上方法,你可以在 Linux 系统中设置和管理 IP 地址,请根据你使用的发行版和需求选择合适的方法。
评论列表 (0)