{"id":19,"date":"2015-09-04T13:33:14","date_gmt":"2015-09-04T13:33:14","guid":{"rendered":"http:\/\/www.asianux.org.vn\/?p=19"},"modified":"2015-09-04T13:33:14","modified_gmt":"2015-09-04T13:33:14","slug":"how-to-set-a-default-gateway-on-centos","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/09\/04\/how-to-set-a-default-gateway-on-centos\/","title":{"rendered":"How to set a default gateway on CentOS"},"content":{"rendered":"<div>\n<p>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.<\/p>\n<\/div>\n<div>\n<p>If you wish to change a default gateway temporarily at run time, you can use<span class=\"Apple-converted-space\">\u00a0<\/span><tt>ip<\/tt><span class=\"Apple-converted-space\">\u00a0<\/span>command.<\/p>\n<\/div>\n<div>\n<p>First things first. To check what default gateway you are using currently:<\/p>\n<\/div>\n<div class=\"console\" data-blogger-escaped-style=\"background: rgb(0, 153, 204); border: 0px none; color: white; font-family: monospace; font-size: 13px; font-style: inherit; font-weight: inherit; margin: 0px 0px 15px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline; white-space: nowrap;\">\n<p>$ ip route show<\/p>\n<\/div>\n<pre data-blogger-escaped-style=\"background: rgb(238, 238, 238); border: 1px dashed rgb(204, 204, 204); margin: 0px 0px 15px; overflow: auto; padding: 10px;\">192.168.91.0\/24 dev eth0  proto kernel  scope link  src 192.168.91.128\n169.254.0.0\/16 dev eth0  scope link  metric 1002\n<strong>default via 192.168.91.2 dev eth0 <\/strong>\n<\/pre>\n<div>\n<p>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.<\/p>\n<\/div>\n<div>\n<p><strong>In order to change a default gateway to another IP address<\/strong>:<\/p>\n<\/div>\n<div class=\"console\" data-blogger-escaped-style=\"background: rgb(0, 153, 204); border: 0px none; color: white; font-family: monospace; font-size: 13px; font-style: inherit; font-weight: inherit; margin: 0px 0px 15px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline; white-space: nowrap;\">\n<p>$ sudo ip route replace default via 192.168.91.10 dev eth0<\/p>\n<\/div>\n<div>\n<p>Obviously, a default gateway&#8217;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.<\/p>\n<\/div>\n<pre data-blogger-escaped-style=\"background: rgb(238, 238, 238); border: 1px dashed rgb(204, 204, 204); margin: 0px 0px 15px; overflow: auto; padding: 10px;\">RTNETLINK answers: No such process\n<\/pre>\n<div>\n<p>Also, keep in mind that the default route change made by<span class=\"Apple-converted-space\">\u00a0<\/span><tt>ip<\/tt><span class=\"Apple-converted-space\">\u00a0<\/span>command will be lost after rebooting.<\/p>\n<\/div>\n<div>\n<p><strong>In order to set a default gateway permanently on CentOS<\/strong>, you will need to update \/etc\/sysconfig\/network accordingly.<\/p>\n<\/div>\n<div class=\"console\" data-blogger-escaped-style=\"background: rgb(0, 153, 204); border: 0px none; color: white; font-family: monospace; font-size: 13px; font-style: inherit; font-weight: inherit; margin: 0px 0px 15px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline; white-space: nowrap;\">\n<p>$ sudo vi \/etc\/sysconfig\/network<\/p>\n<\/div>\n<pre data-blogger-escaped-style=\"background: rgb(238, 238, 238); border: 1px dashed rgb(204, 204, 204); margin: 0px 0px 15px; overflow: auto; padding: 10px;\">GATEWAY=192.168.91.10\n<\/pre>\n<div>\n<p>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.<\/p>\n<\/div>\n<div>\n<p>Another option to set a default gateway persistently on CentOS is to edit \/etc\/sysconfig\/network-scripts\/ifcfg-&lt;default_interface_name&gt;, and add &#8220;GATEWAY=&lt;gateway_ip&gt;&#8221; there. If the default interface is &#8220;eth0&#8221;, you will need to edit \/etc\/sysconfig\/network-scripts\/ifcfg-eth0. If you choose to use this method, you need to refer to<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/xmodulo.com\/2013\/01\/how-to-configure-networking-in-centos-desktop-with-command-line.html\" data-blogger-escaped-target=\"_blank\" target=\"_blank\" rel=\"noopener\">this post<\/a><span class=\"Apple-converted-space\">\u00a0<\/span>to get familiar with this option.<\/p>\n<\/div>\n<div>\n<p>Whether you edit \/etc\/sysconfig\/network or \/etc\/sysconfig\/network-scripts\/ifcfg-ethX, don&#8217;t forget to restart<span class=\"Apple-converted-space\">\u00a0<\/span><tt>network<\/tt>service as follows, or reboot your CentOS for the change to take effect.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-he-thong"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}