Linux静态路由配置
2024-08-24 12:19:31
配置了多块网卡后,需要指定数据包由哪块网卡发送,否则可能无法访问内网,这就要用到静态路由了。
配置静态路由有多种方式:
1、修改 /etc/rc.local 文件,这样每次重启后就会自动添加,如:
echo "route add default gw 10.0.2.2 dev eth0" >> /etc/rc.local
echo "route add -net 192.168.100.0 netmask 255.255.255.0 dev eth1" >> /etc/rc.local
此方法有个弊端:使用 service network restart 重启网络后,静态路由失效
2、[推荐]查看网络启动脚本 : /etc/init.d/network 发现有如下命令:
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi
if [ -f /etc/sysconfig/static-routes ] , -f 意思是存在 /etc/sysconfig/static-routes 且为普通文件,则执行下面的语句
grep "^any" /etc/sysconfig/static-routes 将 any 开头的行取出
while read ignore args 执行后 ignore="any" args=其他
/sbin/route add -$args 添加路由的命令 现在可以加入我们自己的静态路由,查看 static-routes 格式如下: any net 192.168.100.0 netmask 255.255.255.0 dev eth1
any net 0.0.0.0 netmask 0.0.0.0 gw 10.0.2.2 dev eth0 然后重启网络,路由还在:
[root@centos1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 0.0.0.0 255.255.255.0 U eth1
10.0.2.0 0.0.0.0 255.255.255.0 U eth0
169.254.0.0 0.0.0.0 255.255.0.0 U eth0
169.254.0.0 0.0.0.0 255.255.0.0 U eth1
0.0.0.0 10.0.2.2 0.0.0.0 UG eth0
最新文章
- MlLib--逻辑回归笔记
- [mobi文件格式详细介绍一]_[资料篇]
- VNC server grey screen under ubuntu 14.04
- LintCode ";k Sum"; !!
- LINUX系统备份
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
- sql server sys.object表字段说明
- 小程序bindtap和cachetap的区别
- openstack网络基础:网络叠加模式VLAN、VxLAN、GRE
- 清北澡堂 Day2 下午 一些比较重要的数论知识整理
- centos7 设置nginx和php开机自启动
- poj2886(线段树求序列第k小)
- linuxDNS配置
- Mysql Window 解压版 忘记密码
- Echarts地图展示及属性分析
- 《Gradle权威指南》--Android Gradle多项目构建
- spring整合ssmXML版
- webpack打包jQuery,jQuery未定义
- mysql5.7 安装错误解决
- VS2013 生成事件,删除不必要的DLL