linux怎么设置网关?

linux怎么设置网关?,第1张

如下:

linux设置默认网关以root用户为例,执行下面的命令

# route add default gw xxx.xxx.xxx.xxx(网关的IP)

如果用户想更改网关,编辑/etc/sysconfig/network-scripts/ifcfg-eth0文件,将对应的网关设置修改之后,执行下面的命令即可:

#/etc/init.d/network restart

设置默认网关命令如下\x0d\x0aroute add default gw 192.168.10.1\x0d\x0a设置特定路由网关命令如下\x0d\x0aroute add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.10.1\x0d\x0aroute命令说明\x0d\x0a\x0d\x0aroute命令用来显示或修改ip的路由表。\x0d\x0aroute add [-net|-host] [网域或主机] netmask [mask] [gw|dev]\x0d\x0a -net:表示后面接的路由为一个网域;\x0d\x0a -host :表示后面接的为连接到单部主机的路由;\x0d\x0a netmask :与网域有关,可以设定 netmask 决定网域的大小;\x0d\x0a gw :gateway 的简写,后续接的是 IP 的数值喔,与 dev 不同;\x0d\x0a dev :如果只是要指定由那一块网路卡连线出去,则使用这个设定,后面接 eth0 等

一,使用 route 命令添加路由信息注意,使用route 命令添加的路由,机器重启或网卡重启后路由会失效。//添加到主机的路由 # route add –host 192.168.1.11 dev eth0 # route add –host 192.168.1.12 gw 192.168.1.1 //添加到网络的路由 # route add –net 192.168.1.11 netmask 255.255.255.0 eth0 # route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1 # route add –net 192.168.1.0/24 eth1 //添加默认网关 # route add default gw 192.168.2.1 //删除路由 # route del –host 192.168.1.11 dev eth0二,在linux下设置永久路由的方法:1,在/etc/rc.local文件中添加方法:route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.2.254相关阅读:linux中rc.local自启动详解linux下/etc/rc.local开机不执行问题的解决办法2,在/etc/sysconfig/network里添加到末尾方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev3,修改static-routes文件 /etc/sysconfig/static-routes。(如果没有就新建一个)any net 192.168.3.0/24 gw 192.168.3.254 any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129 如果在rc.local中添加路由会造成NFS无法自动挂载问题,所以使用static-routes的方法是最好的。无论重启系统或重启网络:service network restart均可以生效。解决NFS问题的描述:按照linux启动的顺序,rc.local里面的内容是在linux所有服务都启动完毕,最后才被执行的,其中的内容是在netfs之后才被执行的。即在netfs启动时,服务器上的静态路由是没有被添加的,所以netfs挂载不能成功。static-routes是什么文件?这个是network脚本执行时调用的一个文件,这个文件的放置在/etc/sysconfig目录下,在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以上脚本就是添加静态路由的方法,static-routes写法为:any net 192.168.0.0/16 gw 网关ip


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/6072432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-13
下一篇 2023-03-13

发表评论

登录后才能评论

评论列表(0条)

保存