ubuntu配置静态路由及重启生效

简介:

ubuntu配置静态路由及重启生效

第一种方法:使用route命令(添加临时路由)

添加到主机的路由

# route add -host 192.168.1.123 dev eth0

# route add -host 192.168.1.123 gw 192.168.1.1

添加到网络的路由

# route add -net 192.168.1.123 netmask 255.255.255.0 eth0

# route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1

# route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 eth1

# route add -net 192.168.1.0/24 eth1

添加默认网关

# route add default gw 192.168.1.1

删除路由

# route del -host 192.168.1.11 dev eth0

# route del -net 192.168.1.123 netmask 255.255.255.0


第二种方法: 修改/etc/rc.local

修改/etc/rc.local,只是要注意的一点是不要卸载 "exit 0"的后面

vi /etc/rc.local

route add default gw 192.168.1.1

这样在系统启动的时候会自动加入相关的路由设置

注:

(1)如果某个系统服务,比如说是NFS服务,这个服务是在启动network服务之后,在执行rc.local之前,如果你设置的有自动挂载的nfs,那么,这里链路的不通畅,会造成挂载的失败。

(2)如果你重启了网络服务器,那么路由就失效了,这个时候你不得不重新加载这个文件,但是如果你是远程操作的呢?所以,这个方法不是非常的不推荐



第三种方法:修改interfaces文件

up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

可以看到这个格式和route命令的唯一区别在于,在前面多了一个up

下面列出我的interface文件仅供参考,由于安全因素,这里的ip我都用xx替代了:

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).


# The loopback network interface

auto lo

iface lo inet loopback


# The primary network interface

auto eth0

iface eth0 inet static

address xxx.xxx.xxx.xxx

netmask xxx.xxx.xxx.xxx

gateway xxx.xxx.xxx.xxx


auto eth0:0

iface eth0:0 inet static

address xxx.xxx.xxx.xxx

netmask xxx.xxx.xxx.xxx


#dns-nameservers 202.102.224.68 202.102.227.68

up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0

up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0

up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0


第四种:在/etc/sysconfig/network

network里添加到文件末尾,格式如下

vi /etc/sysconfig/network

GATEWAY=gw-ip 或者 GATEWAY=gw-dev

注:这种方式从外观上来看只能用于添加默认网关了对于多路网络环境貌似不太适合的样子


第五种:直接写入ifcfg文件

在配置ip地址的时候直接将GATEWAY的配置写入ifcfg文件,形式:GATEWAY=gw-ip

适合添加默认路由


第六种:写入/etc/sysconfig/static-routes文件

默认在/etc/sysconifg目录中是没有这个文件的,需要我们手工创建,对这个文件的调用在下面:

/etc/init.d/network:

159 # Add non interface-specific static-routes.

160 if [ -f /etc/sysconfig/static-routes ]; then

161 grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do

162 /sbin/route add -$args

163 done

<span style="font-fam










本文转自 运维小当家 51CTO博客,原文链接:http://blog.51cto.com/solin/1900865,如需转载请自行联系原作者
目录
相关文章
|
21天前
|
Ubuntu 安全 Linux
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
Linux(34)Rockchip RK3568 Ubuntu22.04和Debian 10上配置远程桌面工具
68 0
|
3月前
|
网络协议 Java 应用服务中间件
Springboot+ubuntu+Let‘s Encrypt配置https
Springboot+ubuntu+Let‘s Encrypt配置https
40 0
|
3月前
|
Ubuntu
百度搜索:蓝易云【Ubuntu开机自启服务systemd.service配置教程】
现在,你的服务将在Ubuntu开机时自动启动,并在之后的启动中持续运行。记得根据你的实际需求修改 `your_service_name.service`文件中的相关信息。
64 2
|
4月前
|
存储 网络协议 Ubuntu
如何在Ubuntu安装配置SVN服务端并实现无公网ip访问内网资料库
如何在Ubuntu安装配置SVN服务端并实现无公网ip访问内网资料库
77 0
|
4月前
|
Ubuntu 安全 网络安全
百度搜索:蓝易云【Ubuntu系统SSH服务端配置】
现在,你已经成功在Ubuntu系统上配置了SSH服务端。这将允许其他计算机通过SSH协议连接到你的Ubuntu系统,并进行远程管理和操作。请注意,远程访问有安全风险,建议在生产环境中采取必要的安全措施来保护系统。
41 3
|
4月前
|
存储 Ubuntu
百度搜索:蓝易云【在ubuntu系统下安装配置onedrive步骤】
现在,你已经成功在Ubuntu系统下安装和配置了OneDrive,可以使用该工具与OneDrive云端存储进行同步。
43 0
|
4月前
|
Ubuntu
百度搜索:蓝易云【Ubuntu系统新增硬盘,配置自动挂载教程】
现在,您的新硬盘应该已经成功配置为开机自动挂载到 `/mnt/new_disk`目录下了。在每次开机后,Ubuntu会自动将该硬盘挂载到指定的挂载点,您可以在该挂载点下访问和使用新硬盘。
42 0
|
3月前
|
负载均衡 Ubuntu 应用服务中间件
|
19天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
32 1
|
21天前
|
Ubuntu Linux 测试技术
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试(下)
Linux(32)Rockchip RK3568 Ubuntu22.04上部署 Docker: 详细配置与功能测试
50 1