ifconfig
是一个用于配置和显示 Linux 内核中网络接口的命令行实用程序。要使用 ifconfig
设置 Linux 网络接口,请按照以下步骤操作:
打开终端(Terminal)。
首先,查看所有可用的网络接口。在终端中输入以下命令:
ifconfig -a
这将显示系统上所有可用的网络接口。
eth0
的接口,请执行以下命令:sudo ifconfig eth0
注意:根据您的 Linux 发行版和网络环境,接口名称可能有所不同(例如 wlan0
、ens33
等)。
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
将 192.168.1.100
替换为您想要的 IP 地址,将 255.255.255.0
替换为您想要的子网掩码。
sudo ifconfig eth0 up
sudo ifconfig eth0 down
route
或 ip route
命令。例如,要将默认网关设置为 192.168.1.1
,请执行以下命令之一:sudo route add default gw 192.168.1.1
或者
sudo ip route add default via 192.168.1.1
/etc/resolv.conf
文件。使用文本编辑器打开文件,例如:sudo nano /etc/resolv.conf
在文件中添加以下行,将 8.8.8.8
替换为您选择的 DNS 服务器地址:
nameserver 8.8.8.8
保存并关闭文件。
请注意,使用 ifconfig
设置的网络配置在系统重启后将丢失。要永久保存网络配置,请考虑使用 netplan
(适用于大多数现代 Linux 发行版)或编辑 /etc/network/interfaces
文件(适用于较旧的发行版)。