LINUX怎么用命令设置IP地址和DNS地址?
180
0
0
一、设置IP地址
临时设置(重启后失效)
对于基于Debian和Ubuntu的系统:
使用
ifconfig
命令(较旧版本):- 例如,要将
eth0
接口的IP地址设置为192.168.1.100
,子网掩码为255.255.255.0
,可以执行sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
。
- 例如,要将
使用
ip
命令(推荐):- 查看网络接口:
ip addr show
。 - 设置IP地址:
sudo ip addr add 192.168.1.100/24 dev eth0
(这里/24
表示子网掩码为255.255.255.0
)。
- 查看网络接口:
对于基于Red Hat和CentOS的系统:
- 同样可以使用
ip
命令,如sudo ip addr add 192.168.1.100/24 dev eth0
。
- 同样可以使用
永久设置(重启后仍然有效)
编辑网络配置文件。
在Debian和Ubuntu系统中,编辑
/etc/network/interfaces
(较旧版本)或者/etc/netplan/*.yaml
(新版本)。对于
/etc/network/interfaces
,添加类似如下内容:auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1
对于
/etc/netplan/*.yaml
,例如00 - installer - config.yaml
,添加:network: version: 2 renderer: networkd ethernets: eth0: dhcp4: false addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]
在Red Hat和CentOS系统中,编辑
/etc/sysconfig/network - scripts/ifcfg - eth0
文件,添加或修改如下内容:TYPE = Ethernet BOOTPROTO = none DEFROUTE = yes IPV4_FAILURE_FATAL = no IPV6INIT = yes IPV6_AUTOCONF = yes IPV6_DEFROUTE = yes IPV6_FAILURE_FATAL = no NAME = eth0 UUID = xxxxxxxx - xxxx - xxxx - xxxx - xxxxxxxxxxxx DEVICE = eth0 ONBOOT = yes IPADDR = 192.168.1.100 NETMASK = 255.255.255.0 GATEWAY = 192.168.1.1
二、设置DNS地址
临时设置(重启后失效)
使用
nmcli
命令(如果安装了NetworkManager):- 例如,设置DNS为
8.8.8.8
和8.8.4.4
:sudo nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"
,然后sudo nmcli con up eth0
。
- 例如,设置DNS为
直接编辑
/etc/resolv.conf
文件(不推荐用于长期稳定的网络环境,因为可能会被网络管理服务覆盖):- 添加
nameserver 8.8.8.8
和nameserver 8.8.4.4
。
- 添加
永久设置(重启后仍然有效)
- 在基于Debian和Ubuntu的系统中,如前面提到的在
/etc/netplan/*.yaml
文件中设置nameservers
部分。 - 在Red Hat和CentOS系统中,可以在
/etc/sysconfig/network - scripts/ifcfg - eth0
文件中添加DNS1 = 8.8.8.8
和DNS2 = 8.8.4.4
。
- 在基于Debian和Ubuntu的系统中,如前面提到的在
0
快来点个赞吧
发表评论
评论列表