HOWTO_static_ip_with_dhcp_router
Contents |
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 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0 loopback * 255.0.0.0 U 0 0 0 lo default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 |
As can be seen, the default route is to 192.168.1.1, 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.
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 192.168.1.2 to 192.168.1.51.) Let's assume you want the address of a Linux machine on the network to be 192.168.1.101, edit /etc/conf.d/net like so:
| File: /etc/conf.d/net |
config_eth0=( "192.168.1.101/24" ) routes_eth0=( "default gw 192.168.1.1" ) |
Or if you'd like to be more specific:
| File: /etc/conf.d/net |
config_eth0=( "192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255" ) routes_eth0=( "default gw 192.168.1.1" ) |
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 |
nameserver 192.168.1.1 |
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" wpa_timeout_eth2=60 # Enable arping mode config_eth2=( "arping" ) # Define the gateways you want to configure gateways_eth2="192.168.1.1" # Define the IP and netmask,default route and DNS server when using gateway 192.168.1.1 # At home, you want IP 192.168.1.102 config_192168001001=( "192.168.1.102 netmask 255.255.255.0" ) routes_192168001001=( "default via 192.168.1.1" ) dns_servers_192168001001=( "192.168.1.1" ) # If any of the above profiles fail use DHCP fallback_eth2=( "dhcp" ) iwconfig_eth2="mode managed" |
DHCP
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 hotplug_eth0="yes" 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
- Configure a wireless connection
- Gentoo networking guide
- Static IP Address Problems (SOLVED) on the Gentoo Forums
Created by NickStallman.net, Luxury Homes Australia
Real estate agents should be using interactive floor plans.
New! Real Estate SMS for properties.
