Debian / Ubuntu Linux Setting a Default Gateway

by Vivek Gite · 16 comments

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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 16 comments… read them below or add one }

1 Rajesh 02.28.08 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 ??

2 shutty 07.16.08 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

3 Petrus 09.02.08 at 10:09 am

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

4 vivek 09.02.08 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/

5 Mauro 10.18.08 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

6 vivek 10.18.08 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

7 Mauro 10.18.08 at 5:26 pm

vivek, many many many thanks. It works!

8 adam 10.30.08 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

9 Kuldeep Yadav 03.06.09 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.

10 Peter Holmes 09.01.09 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?

11 catalin 09.16.09 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?

12 Stuart Smith 10.13.09 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.

13 joshlondon 11.24.09 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?

14 Stuart Smith 11.26.09 at 2:33 am

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

15 Josh 11.26.09 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.

16 Daniel Smith 03.20.10 at 12:07 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All