linux怎么添加网关地址吗
linux怎么添加网关地址吗详细介绍
在 Linux 系统中,添加网关地址的方法会因使用的网络管理工具和系统版本的不同而有所差异。下面为你介绍几种常见的添加网关地址的方式。
临时添加网关地址
如果你仅需临时添加网关地址,当系统重启后设置就会失效,可使用ip命令或者route命令。
使用
ip命令
bash
route default via 网关地址 dev 网络接口名
例如,网关地址为192.168.1.1,网络接口名为eth0,则命令如下:
bash
route default via .1.1 dev eth0
使用
route命令
bash
route default gw 网关地址 网络接口名
例如,网关地址为192.168.1.1,网络接口名为eth0,则命令如下:
bash
route default gw .1.1 eth0
永久添加网关地址
如果你希望重启系统后网关地址依然生效,需要根据系统使用的网络管理工具进行相应配置。
基于
NetworkManager的系统
在这类系统中,可编辑网络连接配置文件来添加网关地址。
bash
nmcli connection modify 连接名 ipv4.gateway 网关地址nmcli connection modify 连接名 ipv4.method manual
nmcli connection up 连接名
例如,连接名为Wired connection 1,网关地址为192.168.1.1,则命令如下:
bash
nmcli connection modify ipv4.gateway .1.1nmcli connection modify ipv4.method manual
nmcli connection up
基于
netplan的系统
在 Ubuntu 18.04 及更高版本中,通常使用netplan来管理网络配置。可编辑/etc/netplan目录下的配置文件,示例如下:
yaml
networkd
no
192.168.1.100/24
192.168.1.1
8.8.8.8 8.8.4.4
在上述配置中,gateway4字段指定了网关地址。编辑完成后,应用配置:
bash
netplan apply
基于
ifupdown的系统
在 Debian、Ubuntu 较旧版本等使用ifupdown的系统中,可编辑/etc/network/interfaces文件,示例如下:
plaintext
auto eth0iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
编辑完成后,重启网络服务:
bash
eth0 eth0
912 ifdown eth0 && ifup eth0
你可以依据自己的系统情况选择合适的方法添加网关地址。