# how do I change default gateway? manually and automatically

## stringchopper

How do I change the default gateway, shown from "route"

```
Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

172.16.0.0      *               255.255.0.0     U     0      0        0 eth0

loopback        *               255.0.0.0       U     0      0        0 lo

default         172.16.0.1      0.0.0.0         UG    0      0        0 eth0

```

When I activate my wireless with wpa_supplicant, I want to use the wireless router as the default.  Wireless, on my laptop, is on a different network: 

192.168.1.0/24  = wireless network

192.168.1.1      = wireless router (want this to be default only when wireless is on)

172.16.0.0/16   = wired network (I need this to access shares on the LAN, through my eth0 interface, but not for internet)

172.16.0.1       = wired router

here's my /etc/conf.d/net file

```

modules=( "wpa_supplicant" )

wpa_supplicant_wlan0="-Dwext"

config_eth0=( "dhcp" )

config_wlan0=( "192.168.1.92/24" )

routes_wlan0=( "default via 192.168.1.1" )

```

currently, I'm not sure that the "config_wlan0" lines do anything.  Only eth0 comes up at boot, whether it is plugged in or not.  My wireless is "on" (physical button) most of the time when I'm home, but it'd be really annoying to wait for it at boot time when I'm somewhere other than home.

Currently, every time I turn on wireless at home, I'm taking eth0 down through ifconfig to clear the routing table, then bringing wlan0 up, running wpa_supplicant, running wpa_cli and manually reassociated with wireless router, then running dhcpcd wlan0 (yes, I have to run dhcpcd to get 192.168.1.92 assigned, I suppose because it's in the pool range, yet it is a static reserved IP, assigned by mac address)

sorry for being a bit wordy! :)  TIA

----------

## pianosaurus

If you need dhcp on wlan0, then you are correct in that your config_wlan0 is useless the way it is now. I would recommend setting it to "dhcp" like you do with eth0. It shouldn't run dhcp unless it has associated with an AP, so there shouldn't be a long wait at boot time anyway. If I'm wrong, and there is, you can work around it by lowering the timeout:

```
config_wlan0=( "dhcp" )

dhcpcd_wlan0="-t 10"
```

----------

## pianosaurus

Also, you can set the default gateway manually with route add default 192.168.1.1 (edit: no you can't; see defenderBGs post below), though I don't know if there is an easy way to do this automatically. You may also have to use route del to remove the old default first, but I don't think so. I don't have the opportunity to test it at the moment.

----------

## defenderBG

The correct command would be:

route add default gw 192.168.1.1

and if you want to set a new gw, you have to delete the older one (if you had any).

The command looks like the one above, but yet you need to replace add with del.

You can find out what the default gw is with /sbin/route -n

Automaticly would be with dhcp (emerge dhcp or dhcpcd) then dhclient or dhcpcd <interface>

----------

## pianosaurus

 *defenderBG wrote:*   

> The correct command would be:
> 
> route add default gw 192.168.1.1

 

Oops, yes you are right. Just a typo.

 *defenderBG wrote:*   

> You can find out what the default gw is with /sbin/route -n

 

Or just route, like he already did (see first code block of his post).

 *defenderBG wrote:*   

> Automaticly would be with dhcp (emerge dhcp or dhcpcd) then dhclient or dhcpcd <interface>

 

His problem is that two different interfaces sets two different gateways, both through dhcp. He doesn't want the default gateway to be overridden by the wired dhcp.

----------

## defenderBG

ok, I just read the name of the thread, your last post and answered  :Very Happy: 

you got me here, so its 1:1.

----------

## pianosaurus

 *defenderBG wrote:*   

> ok, I just read the name of the thread, your last post and answered 
> 
> you got me here, so its 1:1.

 

 :Twisted Evil:   Let the games begin!    :Very Happy: 

----------

## stringchopper

is there a way to "dynamically" assign a metric to the default gateway that is provided to both the eth0 and wlan0 interface.... instead of adding routes and deleting them through the route command?

ie, if my laptop wired is in (at home), use that gateway as default (0.0.0.0).  If and when the wireless comes up (at home, and both interfaces are active), use the gateway for wlan0 as default for internet traffic, and only use the 172.16.0.1 gateway for the traffic to the home LAN 

When on the road, of course, use whatever gateway is assigned at the public Access Point as the default.

I'm pretty sure the metric option in the route command would take care of this, if "metric" here works like it does in routing protocols.  But I don't want to mess with the route table every time I go out in public and have a different subnet, IP address and default gateway

EDIT: what I was trying to refer to as "metric" would more appropriately be termed "Administrative distance".  The AD, in network routing tables is different depending on which protocol put the route in the table, or whether it is physically connected or statically defined.  The lower AD is always chosen as the route to put into the routing table.  

so what I was thinking is, "wouldn't it be great if" I could have an AD assigned to each default route... say 3 for wired at home, and 2 for wireless (making it preferred), and then use a default lower setting for any default route when in public (so it beats both my home wired and wireless, and I wouldn't have to manually delete and add routes)

----------

## gimpel

I guess that's what networkmanager was made for.

----------

## stringchopper

 *gimpel wrote:*   

> I guess that's what networkmanager was made for.

 

Well, I do have that emerged, but when I run the GUI, there's nothing in it, and no way to edit/add anything.  I'll look into it.

At this point, if possible, I prefer to do it manually I think (if possible), so that I can learn about how these things work with linux.

----------

## stringchopper

I've noticed that if I start the laptop without the wired cat5 cable plugged in, then bring up wlan0 (using wpa_supplicant, wpa_cli, dhcpcd), the wireless router is the default gateway listed in output of "route".

If I subsequently plug in the cat5 cable, everything works automagically for the wired network.  Most importantly, the default gateway for the wired network is added to the route table with a metric (hop) of 1 instead of 0.  This accomplishes what I want to do.  

So now my question is, how do I ensure that eth0 stays down until wlan0 is up - or how do I ensure that wlan0 comes up first.  I think, but am not sure that this takes me back to having wlan0 being set up at boot (correct).  That, or leave them both down at boot, and write a script to bring them up in order.

so I recall doing something like "rc-update add net.eth0 default" when installing gentoo.  I never did this for wlan0.

Should I simply do this, below, in this specific order, to bring them both up at boot, with wlan0 coming up first?

```
rc-update del net.eth0 default

rc-update add net.wlan0 default

rc-update add net.eth0 default
```

or does the order of the contents in the /etc/conf.d/net file make a difference.

cat /etc/conf.d/net (mine is presently configured with eth0 first)

```

modules=( "wpa_supplicant" )

wpa_supplicant_wlan0="-Dwext"

config_eth0=( "dhcp" )

config_wlan0=( "dchp" )

```

----------

## pianosaurus

You could try, but I don't think it matters what order you add them to rc, since the service order is recalculated anyway. The order in /etc/conf.d/net doesn't matter, unfortunately.

----------

## UberLord

https://wiki.gentoo.org/wiki/Network_management_using_DHCPCD

----------

## krinn

Doctor UberLord Jones is looking for the lost ark?  :Smile: 

----------

## Dominique_71

 *UberLord wrote:*   

> https://wiki.gentoo.org/wiki/Network_management_using_DHCPCD

 

I just added a section about routing priority in that article.

----------

