2014年11月14日 星期五

[Linux 文章收集] How to set a default gateway on CentOS

Source From Here 
Preface 
A default gateway is a remote host or router that your Linux host forwards traffic to when the destination IP address of outgoing traffic does not match any route in your local routing table. Configuring a default gateway on CentOS is quite straightforward. If you wish to change a default gateway temporarily at run time, you can use ip command. 

First things first. To check what default gateway you are using currently: 
$ ip route show
192.168.91.0/24 dev eth0 proto kernel scope link src 192.168.91.128
169.254.0.0/16 dev eth0 scope link metric 1002
default via 192.168.91.2 dev eth0

According to the local routing table shown above, a default gateway is 192.168.91.2, and traffic is forwarded to the gateway via eth0. 

Change a default gateway to another IP address 
In order to change a default gateway to another IP address, try below command: 
$ sudo ip route replace default via 192.168.91.10 dev eth0 # Change default gw from 192.168.91.2 to 192.168.91.10

Obviously, a default gateway's IP address should come from the subnet associated with the interface connected to the default gateway, in this example, 192.168.91.0/24. Otherwise, the command will fail with the following error. 
RTNETLINK answers: No such process

Also, keep in mind that the default route change made by ip command will be lost after rebooting. 

Set a default gateway permanently on CentOS 
You will need to update /etc/sysconfig/network accordingly. 
$ sudo vi /etc/sysconfig/network
  1. ...  
  2. GATEWAY=192.168.91.10  

Again, be aware that the IP addressed specified here should match with the subnet (192.168.91.0/24) associated with a default route interface. 

Another option to set a default gateway persistently on CentOS is to edit /etc/sysconfig/network-scripts/ifcfg-<default_interface_name>, and add "GATEWAY=" there. If the default interface is "eth0", you will need to edit /etc/sysconfig/network-scripts/ifcfg-eth0. If you choose to use this method, you need to refer to this post to get familiar with this option. 

Whether you edit /etc/sysconfig/network or /etc/sysconfig/network-scripts/ifcfg-ethX, don't forget to restart network service as follows, or reboot your CentOS for the change to take effect. 
$ sudo service network restart

Supplement 
How to configure network interfaces in CentOS 
How to assign multiple IP addresses to one network interface on CentOS 
How to configure networking in CentOS Desktop with command line 
How to run a startup script automatically after a ...work interface is up on CentOS 
How to install dummynet on CentOS

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...