linux永久添加路由命令
时间: 2024-09-10 18:02:54 浏览: 71
在Linux系统中,要永久添加路由,可以通过编辑网络配置文件来实现,这样即使在系统重启后,添加的路由也不会丢失。不同的Linux发行版可能会使用不同的配置文件和命令,这里提供一个常见的方法:
对于基于Debian或Ubuntu的系统,你可以使用`netplan`工具来配置网络。通常网络配置文件位于`/etc/netplan/`目录下,文件名可能为`01-netcfg.yaml`等。编辑对应的配置文件,添加需要的路由规则。例如:
```yaml
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: yes
routes:
- to: 192.168.2.0/24
via: 192.168.1.1
metric: 100
```
然后,应用配置:
```bash
sudo netplan apply
```
对于使用`NetworkManager`的系统,你可以使用`nmcli`命令来添加路由:
```bash
nmcli con mod <CON_NAME> +ipv4.routes "<DESTINATION> <PREFERRED_PREFIXLEN> <GATEWAY>"
nmcli con up <CON_NAME>
```
其中`<CON_NAME>`是连接名称,`<DESTINATION>`是目标网络,`<PREFERRED_PREFIXLEN>`是目标网络的子网掩码长度,`<GATEWAY>`是网关。
对于Red Hat系列的系统,如CentOS,可以通过`nmcli`命令或编辑`/etc/sysconfig/network-scripts/route-<interface>`文件来添加路由。
使用`nmcli`添加路由的示例:
```bash
nmcli con mod <CON_NAME> ipv4.routes "<DESTINATION> <PREFERRED_PREFIXLEN> <GATEWAY>"
nmcli con up <CON_NAME>
```
或者编辑文件的示例:
```bash
echo "ADDRESS0=<DESTINATION> NETMASK0=<NETMASK> GATEWAY0=<GATEWAY>" >> /etc/sysconfig/network-scripts/route-<interface>
```
确保替换`<CON_NAME>`、`<DESTINATION>`、`<NETMASK>`和`<GATEWAY>`为实际的值。
阅读全文
相关推荐


















