Gentoo Wiki



Static IP

Sometimes you'll want to have a computer on the network that always has the same IP address. Usually a server of some sort or a machine that has a game on it that requires you to forward certain ports. Assigning a static IP address, an address that will never change, will save you from constantly updating details on your router.

Determining the Router's IP Address

The first thing that needs to be done is to determine your router's IP address. Route will help out:

Code: route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface     *        U     0      0        0 eth0
loopback        *            U     0      0        0 lo
default         UG    0      0        0 eth0

As can be seen, the default route is to, which is the router.

Assigning a Static IP Address

To assign a static IP address on a Linux machine, you'll need to edit /etc/conf.d/net.

Warning: If you assign a static IP address that is in the same range that your DHCP server will also assign, be sure to reserve that IP address for that machine. As how this can be done varies from router to router, please refer to your router's documentation. The safest thing to do is to assign an address that is out side of the DHCP IP address pool.

You'll want to assign an address that is outside the DHCP IP Address Pool. (For example, on Titan's router, the DHCP pool has a range from to Let's assume you want the address of a Linux machine on the network to be, edit /etc/conf.d/net like so:

File: /etc/conf.d/net
config_eth0=( "" )
routes_eth0=( "default gw" )

Or if you'd like to be more specific:

File: /etc/conf.d/net
config_eth0=( " netmask broadcast" )
routes_eth0=( "default gw" )

Defining the DNS Servers

Finally, you have to define your DNS server(s) in /etc/resolv.conf. The best thing to do is to define your ISP's DNS servers, but if you can't find that information, your router's IP address will most likely do the trick.

File: /etc/resolv.conf

Laptop: Static at Home and Dynamic Outside

This is a quick example of /etc/conf.d/net so that a Linux laptop can have a static IP address when you're at home and dynamically obtains an IP address when you're not at home. For more information, have a look at the article HOWTO Network profiles with arping. Here is the configuration example:

File: /etc/conf.d/net
# interface eth2 and wpa_supplicant
modules=( "wpa_supplicant" )
wpa_supplicant_eth2="-Dwext -c /etc/wpa_supplicant/wpa_supplicant.conf"

# Enable arping mode
config_eth2=( "arping" )

# Define the gateways you want to configure

# Define the IP and netmask,default route and DNS server when using gateway
# At home, you want IP
config_192168001001=( " netmask" )
routes_192168001001=( "default via" )
dns_servers_192168001001=( "" )

# If any of the above profiles fail use DHCP
fallback_eth2=( "dhcp" )
iwconfig_eth2="mode managed"


Now to modify your network configuration:

File: /etc/conf.d/net
config_eth0=( "dhcp" )
dhcp_eth0="nodns nontp nonis"

This assumes DHCP and a few other options. For more information see section 8.b of the Gentoo handbook.

Check for a /etc/init.d/net.eth0 symbolic link:

ls -l /etc/init.d/net.eth0

If it doesn't exist, create it:

ln -s /etc/init.d/net.lo /etc/init.d/net.eth0

Now start it:

/etc/init.d/net.eth0 start

If you want it to start on boot, add it to the default runlevel:

rc-update add net.eth0 default

Fallback route (hotplugd)

This will setup you eth0 device to try dhcp and fall back to a static network. For me this works well because at work they use DHCP and at home I have a static setup.

File: /etc/conf.d/net
# ETH0
configure_eth0=( "dhcp ")
fallback_eth0=( "XXX.XXX.XXX.XXX netmask XXX.XXX.XXX.XXX broadcast XXX.XXX.XXX.XXX" )
fallback_route_eth0=( "default via XXX.XXX.XXX.XXX" )

Startup sequency for wired and wireless connections

If you want to use the same startup sequency and run-level for both wired and wireless connections, you will want to lower the DHCP timeout. That way it won't hang on your wired interface when you want to use wireless (default timeout is 60 seconds). To do this, open up /etc/conf.d/net and add the following lines. This assumes that eth0 is your wired and eth1 is your wireless interface. If the timeout of 5 seconds is too short for your particular network setup, simply increase the number.

File: /etc/conf.d/net
config_eth0=( "dhcp" )
config_eth1=( "dhcp" )
dhcpcd_eth0="-t 5"
dhcpcd_eth1="-t 5"

See also

Retrieved from ""

Last modified: Sat, 23 Aug 2008 14:23:00 +0000 Hits: 2,092