Debian / Ubuntu Linux Setting a Default Gateway

by on April 18, 2007 · 22 comments· LAST UPDATED October 18, 2008

in , ,

Q. How do I setup a default gateway on Debian or Ubuntu Linux? My router IP is 192.168.1.254 and I'd like to setup this as a default gateway for Debian Linux.

A. You can use command line tool such as
a] ip command - show / manipulate routing, devices, policy routing and tunnels

b] route command - show / manipulate the IP routing table

c] Save routing information to a configuration file so that after reboot you get same default gateway.

ip command to set a default router to 192.168.1.254

Login as the root and type:
# ip route add default via 192.168.1.254
OR
$ sudo ip route add default via 192.168.1.254

route command to set a default router to 192.168.1.254

Login as the root and type:
# route add default gw 192.168.1.254
OR
$ sudo route add default gw 192.168.1.254

Save routing information to a configuration file /etc/network/interfaces

Open /etc/network/interfaces file
# vi /etc/network/interfaces
OR
$ sudo vi /etc/network/interfaces
Find eth0 or desired network interface and add following option
gateway 192.168.1.254
Save and close the file. Restart networking:
# /etc/init.d/networking restart
OR
$ sudo /etc/init.d/networking restart

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 22 comments… read them below or add one }

1 Rajesh February 28, 2008 at 5:31 pm

I also want to set my defualt DNS server and proxy settings, just like in windows. How do I do that ??

Reply

2 Alec May 20, 2013 at 3:25 pm

Rajesh,

In Ubuntu, you can either add the ‘nameserver’ lines in /etc/resolv.conf like “shutty” shows, or use the recommended method in Ubuntu Precise (12.04 LTS) by adding “dns-nameservers ” and “dns-search ” in /etc/network/interfaces, like so:

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.3.1
netmask 255.255.255.0
# dns-* options are implemented by the resolvconf package, if installed

dns-nameservers 8.8.8.8
dns-search lab.internal

Reply

3 shutty July 16, 2008 at 1:01 pm

Rajesh,

for dns, edit file /etc/resolv.conf and add lines
nameserver 1.1.1.1
nameserver 2.2.2.2
(where 1.1.1.1 and 2.2.2.2 are your nameservers)

for proxy, use your browser configuration

Reply

4 Petrus September 2, 2008 at 10:09 am

How do I add a second gateway on higher metric incase the first goes down?

Reply

5 nixCraft September 2, 2008 at 10:46 am

Petrus,

You need to install routed and configure /etc/gateway. Another recommended option is install zebra / Quagga server and configure it. Both allows you to define and configure various routing parameters including metric.

http://www.quagga.net/

Reply

6 Mauro October 18, 2008 at 10:05 am

How can I get (in a variable) the current default gateway? Is there a sort of “default gateway enviroment variable” that I can read?
Thanks

Reply

7 nixCraft October 18, 2008 at 12:01 pm

Run following to display default gateway:
ip route show | grep default | awk '{ print $3}'
Use following to store gateway IP in a variable called GW:
GW=$(ip route show | grep default | awk '{ print $3}')
echo $GW

Reply

8 Mauro October 18, 2008 at 5:26 pm

vivek, many many many thanks. It works!

Reply

9 adam October 30, 2008 at 6:22 am

For two interfaces that both do dhcp the metrics on the default gateway can be added like this.

auto eth1
iface eth1 inet dhcp
metric 10

auto eth2
iface eth2 inet dhcp
metric 20

Reply

10 Kuldeep Yadav March 6, 2009 at 2:30 pm

Is there any way,so that we can give proxy addresss frm command line apart from browser.because if we give this from command line,it will be applicable to all system.

Reply

11 Peter Holmes September 1, 2009 at 11:30 am

I have two interfaces, eth0 & eth1. eth0 fixed IP 192.168.1.20 hardwired to test machine with fixed IP 192.168.1.6, (not easily altered).
I want eth1 to connect to internet via a DHCP Server Router. Without eth1 enabled there is no problem but as soon as I enable eth1 as DHCP client I lose any access to 192.168.1.6
Have spent many hours altering the route(s), etc., with no success.
Do I have to use ipchains or can it be achieved with route?

Reply

12 catalin September 16, 2009 at 9:42 am

I am having some routing problems with Ubuntu 9.04:
Whenever I start up my system, the Internet won’t work unless I do:
"sudo route add default gw 192.168.1.254"
I tried to alter /etc/network/interfaces, but with no luck. After restart, it doesn’t work. executing route prints:

catalin@lolinux:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 wlan0

After I manually add the gateway, everything is fine, and route prints another gateway:

catalin@lolinux:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 wlan0
default dsldevice.lan 0.0.0.0 UG 0 0 0 wlan0

Is there any difference from what I altered and the kernel routing table? If there is, how do I modify that? Or would a startup script be a better solution?

Reply

13 Stuart Smith October 13, 2009 at 2:30 pm

I have the “gateway 192.168.1.1″ in my network file but I’m still required to manually set the route after reboot. This behavior began only after I “bonded” my two NIC’s. The routing table before the manual route command shows
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 bond0

and after adds this one line just below the 169.254… line
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 bond0

Am I missing something or am I experiencing a true bug?

I’m using Kubuntu 9.04 Jaunty.

Reply

14 joshlondon November 24, 2009 at 1:39 am

I am having the same problem..I have to manually set my default gateway after each boot up even though all my network settings include it. How do I get it to stick?

Reply

15 Stuart Smith November 26, 2009 at 2:33 am

I eventually removed Network Manager. Set everything up manually and it works fine.

Reply

16 Josh November 26, 2009 at 4:04 am

I just switched to static ip’s and set everything through wicd..works perfectly. Also I used the network admin tool and I believe that screwed up lots of things.

Reply

17 Daniel Smith March 20, 2010 at 12:07 pm

How can I find Gateway server !? to add the numbers to the address code!?

Reply

18 Mahmoud Ibrahim February 20, 2011 at 1:49 pm

How can i use 2 gateway , I have a complex network using 2 ISP and Linux as a cache and firewall and a router.

i WANT lINUX router to be able to use both ISP in same time and could nat global IP.

Reply

19 Wael Hawa July 25, 2011 at 8:55 pm

I am using my nanostation 5 as a router… but I can’t receive ping from the ISP gateway… even though I have my ip in the same range. I found this site but the GW isn’t adding… Here are my tables

Destination Gateway Genmask Flags Metric Ref Use Iface
10.64.64.64 * 255.255.255.255 UH 0 0 0 ppp0
10.29.136.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default * 0.0.0.0 U 0 0 0 ppp0

My ip range is 10.29.136.0… I need to add 10.29.136.1 as GW

Reply

20 TooMeeK March 20, 2012 at 10:27 am

Hehe
funny thing here! very old Debian distro, I’m trying to add new default gateway via.. tun interface (VPN), but it’s refusing to add anything :D

ip route
10.10.0.2 dev tun0 proto kernel scope link src 10.10.0.1
10.1.0.0/24 via 10.10.0.2 dev tun0
10.0.0.0/16 dev eth1 proto kernel scope link src 10.0.0.254
192.168.0.0/16 dev eth0 proto kernel scope link src 192.168.33.100
default via 192.168.0.1 dev eth0

ping 10.1.0.1
PING 10.1.0.1 (10.1.0.1) 56(84) bytes of data.
64 bytes from 10.1.0.1: icmp_seq=1 ttl=64 time=0.726 ms
64 bytes from 10.1.0.1: icmp_seq=2 ttl=64 time=0.544 ms
64 bytes from 10.1.0.1: icmp_seq=3 ttl=64 time=1.60 ms
64 bytes from 10.1.0.1: icmp_seq=4 ttl=64 time=1.58 ms

sudo ip route add default via 10.1.0.1
RTNETLINK answers: No such process

sudo route add default gw 10.1.0.1
SIOCADDRT: Nie ma takiego procesu

Reply

21 TooMeeK March 20, 2012 at 11:01 am

solved, I’ve used 10.10.0.2 as gateway which is IP alias of VPN

Reply

22 Michael April 14, 2012 at 6:11 pm

add the following to the bottom of your /etc/network/interfaces file

# Static route
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx dev ethX

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: