# Switching to a static IP (within your network)

## StringCheesian

Switching to a static IP (within your network) by a dummy for dummies

If you're reading this, you're probably using a dynamic IP with DHCP - especially if you just followed the install instructions and emerged dhcpcd. This means Gentoo asks your router to assign it an IP address every time you connect or boot, just like a default Windows install does.

A static IP is when you set an IP address to use. A static IP means others on your home/work network can connect to you using the same IP that their browser/ftp client remembers for them. It also frees you from DHCP and the 2 or so seconds of delay it typically involves.

Step 1: Find out what kind of IP addresses your router gives out

Open a terminal and type:

```
su

(type your root password)

ifconfig
```

It will probably show something like this:

```
lo        Link encap:Local Loopback

(...)

eth0      Link encap:Ethernet  HWaddr 00:1d:6a:44:e1:09

          inet addr:172.16.0.2  Bcast:172.16.255.255  Mask:255.255.0.0

(...)
```

The inet addr part shows the IP address you're using now.

Step 2: Choose your new permanent IP address

My router assigns everything addresses like 172.16.0.xx. So an address like 172.16.2.100 is safe because my router will never use that one for another computer (note the ".2." instead of ".0.").

If your router gives addresses like "192.168.1.101", then you might use "192.168.0.42". Just avoid "xx.xx.0.1" - that's usually the router's IP address.

WARNING: If your address is something strange, not one of the usual 3 kinds: "192.168.x.x", "172.16.x.x", or "10.x.x.x", you might want to ask an expert about your network before trying this.

Step 3: Find out what DNS servers you're using now

While connected, take a look at /etc/resolv.conf

It will probably look something like this:

```
# Generated by dhcpcd

# /etc/resolv.conf.head can replace this line

nameserver 66.28.0.45

nameserver 66.28.0.61

# /etc/resolv.conf.tail can replace this line
```

These are the IP addresses of the DNS servers your Gentoo contacts to resolve domains like gentoo.org into IP addresses. Your dynamic IP Gentoo most likely erases this file every time you disconnect. So keep a copy for later.

Step 4: Make backups and prepare

You can completely undo everything by restoring /etc/conf.d/net, so it's good to back it up:

```
cp /etc/conf.d/net /etc/conf.d/net~
```

Step 5: Change your /etc/conf.d/net to look like this: *Quote:*   

> config_eth0=( "172.16.2.100 netmask 255.255.0.0" )
> 
> routes_eth0=( "default gw 172.16.0.1" )
> 
> dns_servers_eth0=( "66.28.0.45 66.28.0.61" )

 

The bold parts need to be replaced with your info. Change the first bold IP address to the one you chose, and the second bold one to the address of your router. For dns_servers_* you can copy in the addresses from your old /etc/resolv.conf (from Step 3) and/or from OpenDNS.

Change "eth0" to "wlan0" or whatever yours is named. Delete or comment out any other stuff in the file.

Step 6: Switch to static IP

Substitute "eth0" for your interface name.

```
/etc/init.d/net.eth0 restart
```

If all went well you should still have network and internet access just as before.

Further reading:

http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=8#doc_chap2_pre5

History:

2008-Mar-18 Minor grammar fix and improved DNS and /etc/resolv.conf stuff (thanks danomac!)Last edited by StringCheesian on Thu Mar 19, 2009 7:09 am; edited 3 times in total

----------

## muhsinzubeir

great guide thanks....I have seen occasionally some router support static ip, if thats an option to someone  :Very Happy: 

----------

## MaximeG

Hi,

I usually use StaticIP for my desktops PC at home even if my routeur provides DHCP, so thanks for the guide  :Wink: 

Regards,

Maxime

----------

## danomac

Just a tip: you should really set your dns servers as well:

```

dns_servers_eth0=( "<ip>" )

```

If you have multiple interfaces it's quite possible that one will clobber your DNS servers, and then when you reboot they'll be lost.

----------

## furanku

Wouldn't it be much better to solve the problem at it's source: The router?

Most routers offer a configuration option to assign static IPs to clients by identifying them via their MAC Address. If you assign an arbitrary IP to you Linux box the way you suggest it, you are still depending on the routing tables of your router (that's why it's called "router"  :Wink:  ) and you can't guarantee that the router itself will treat self assigned IPs in your subnet the way you want. If several persons in your subnet assign the same IP to their computers you'll surely run into problems. There are several methods to assign a IP to a network device: DHCP, zeroconf, ... which avoid collisions.

But if you want a static IP you need a central instance which guarantees that that IP is unique in you network. You can either do that by hand, but most routers offer also exactly that feature: Give the network device with the mac adress XX:XX:XX:XX:XX exactly that IP. You can even still use dhcp and don't have to care about that when configuring the actual network devices, they will be automatically assigned the correct static IP by your router. If you add another device with a static IP, simply expand that table in you router, and you don't need to fiddle around with the devices, which mostly have a default setup to use dhcp.

Especially when you want to use a service like ssh from outside of you local network, you need to tell the router to forward the packets to which IP in your local network, and then you have to involve your router into the configuration of your network anyway. So I would say it's much cleaner, straight forward and easier to maintain to let the router do the work.

----------

## StringCheesian

 *furanku wrote:*   

> Wouldn't it be much better to solve the problem at it's source: The router?
> 
> Most routers offer a configuration option to assign static IPs to clients by identifying them via their MAC Address. If you assign an arbitrary IP to you Linux box the way you suggest it, you are still depending on the routing tables of your router (that's why it's called "router"  ) and you can't guarantee that the router itself will treat self assigned IPs in your subnet the way you want. If several persons in your subnet assign the same IP to their computers you'll surely run into problems. There are several methods to assign a IP to a network device: DHCP, zeroconf, ... which avoid collisions.
> 
> But if you want a static IP you need a central instance which guarantees that that IP is unique in you network. You can either do that by hand, but most routers offer also exactly that feature: Give the network device with the mac adress XX:XX:XX:XX:XX exactly that IP. You can even still use dhcp and don't have to care about that when configuring the actual network devices, they will be automatically assigned the correct static IP by your router. If you add another device with a static IP, simply expand that table in you router, and you don't need to fiddle around with the devices, which mostly have a default setup to use dhcp.
> ...

 

I'm sure you're right, but this way involves more tinkering with Gentoo and less mucking about with router configuration  :Smile: . It's also good for the obsessive or bored who want to shave maybe a second or two off their bootup time for no good reason.

----------

## MaximeG

Hi,

Personnally I like having my desktops and servers configured statically because they are very unlikely to change. It's not really a matter of gaining a couple seconds at boot, but more to be able to have a very easy and straight forward config file for the network. Something like :

ip1 name1

ip2 name2

This file you can reuse if you re-install or change router or anything. It's also easy to configure firewall this way.

The motto is : my network is static, so I configure it statically  :Smile: 

However, my laptop is configured to use dhcp since it's very likely to connect elsewhere (noticeably at work) with a complete different set-up.

Now, I'm not a network maniac. So the simpler the better, even if it's very "manual", is usually what I aim to while dealing with network at home.

My view would be completely different at work with high availability of servers and stuff though  :Wink: 

Regards,

Maxime

----------

## danomac

 *furanku wrote:*   

> Wouldn't it be much better to solve the problem at it's source: The router?
> 
> Most routers offer a configuration option to assign static IPs to clients by identifying them via their MAC Address. 

 

I've used several routers (some provided by ISPs, others are affordable home-based routers from D-Link/Linksys/etc) that don't support this. I have yet to see one in the price range of home users that does...

That aside, you can get that type of support by flashing it (I flashed mine with ddwrt) but most people that buy them off-the-shelf won't bother.

----------

## furanku

Hmm ...  I've had two Netgear Router (currently a WG 614 for 35€) and one D-Link, all in the lower price category, and all of them were able to assign IPs statically to MAC Adresses via dhcp. To be honest I can't think of a  usual private customer class route without that feature, can you be a bit more specific which one is missing that?

----------

## danomac

 *furanku wrote:*   

> Hmm ...  I've had two Netgear Router (currently a WG 614 for 35€) and one D-Link, all in the lower price category, and all of them were able to assign IPs statically to MAC Adresses via dhcp. To be honest I can't think of a  usual private customer class route without that feature, can you be a bit more specific which one is missing that?

 

I think we're going off-topic a bit, but my friend's Netgear router is not capable of it. Others that I've installed that don't have this feature are the wrt54g/wrt54gl, both of which I had to flash to get that functionality. There was another one (but I can't remember the make/model.)

----------

## furanku

Ok, sorry, I couldn't believe that but after downloading the manual for the Linksys wrt54g it seems like this router is in fact missing the IMHO useful feature to assign fixed IPs via dhcp via identifiying the client by its MAC address. In that case you have of course to turn off dhcp at the cliened and assign manually a static IP to it, if you want to use that.

----------

## danomac

 *furanku wrote:*   

> Ok, sorry, I couldn't believe that but after downloading the manual for the Linksys wrt54g it seems like this router is in fact missing the IMHO useful feature to assign fixed IPs via dhcp via identifiying the client by its MAC address. In that case you have of course to turn off dhcp at the cliened and assign manually a static IP to it, if you want to use that.

 

There's a lot of routers like that (95% of the routers I've seen.)  :Wink:  I've seen very few that came with that ability out-of-the-box.

----------

## MaximeG

Hi,

As far as I'm concerned, this feature is called "MAC address preference". Which means that if your router receive a request from MAC address 1, it will give back an IP and keep the preference MAC-IP in a file. This means that if you disconnect the client, and connect it a while later, it's very likely to receive the same IP.

Actually, it will always return the same IP address for the same MAC unless forced to do so (for instance if you exhaust your dhcp ip pool ) which is very unlikely if you're speaking of home network.

Such functionality is widely spread on nowadays routers, and never seen a dhcp capable routeur without MAC address preference. Ok, I've only used Linksys,D-Link and Philips routers so far, so I can't tell for other brands.  :Wink:  But it's honestly a very common feature.

Regards,

Maxime

----------

## slycordinator

 *MaximeG wrote:*   

> Hi,
> 
> As far as I'm concerned, this feature is called "MAC address preference". Which means that if your router receive a request from MAC address 1, it will give back an IP and keep the preference MAC-IP in a file. This means that if you disconnect the client, and connect it a while later, it's very likely to receive the same IP.

 I don't think that's what he's talking about.

You're talking about a client connecting to a router, then the router keeping track of the IP it fed the client, then for some predetermined time, it keeps feeding that same IP address.

But he was talking about having the MAC->IP setting stored permanently. So it wouldn't be the case that you are "very likely to receive the same IP" but are guaranteed it (assuming the MAC wasn't changed). This option is different in that, it is both stored permanently and will also be stored even if the router was rebooted.

----------

## Tony0945

D-Link DI-524 does this. In fact, you can tell the router to not connect to a MAC address that is not in the table, useful for security reasons.  The 524 is no longer made but was a cheap router. I can't believe that D-Link's successor routers don't have the static IP feature. 

It's still advantageous to set up static ip on your gentoo box and enter the addresses in your /etc/hosts.

I don't want to start a flame war, but I first bought a Linksys router and it wouldn't connect. I brought it back to Best Buy who gave me the D-link (same price) in exchange because they were out of stock on the Linksys.

----------

## spofforth

 *furanku wrote:*   

> Wouldn't it be much better to solve the problem at it's source: The router?
> 
> Most routers offer a configuration option to assign static IPs to clients by identifying them via their MAC Address. If you assign an arbitrary IP to you Linux box the way you suggest it, you are still depending on the routing tables of your router (that's why it's called "router"  ) and you can't guarantee that the router itself will treat self assigned IPs in your subnet the way you want. If several persons in your subnet assign the same IP to their computers you'll surely run into problems. There are several methods to assign a IP to a network device: DHCP, zeroconf, ... which avoid collisions.
> 
> But if you want a static IP you need a central instance which guarantees that that IP is unique in you network. You can either do that by hand, but most routers offer also exactly that feature: Give the network device with the mac adress XX:XX:XX:XX:XX exactly that IP. You can even still use dhcp and don't have to care about that when configuring the actual network devices, they will be automatically assigned the correct static IP by your router. If you add another device with a static IP, simply expand that table in you router, and you don't need to fiddle around with the devices, which mostly have a default setup to use dhcp.
> ...

 

My Huawei router supposedly supports this, I have a subnet setup with DHCP leasing only half of the available IPs. I have one static IP configured via MAC address. Yes, the MAC is correct, yes, I tried both lower and upper-case. Nevertheless, the IP the host gets is always in the DHCP range and never the one I configured.

----------

## Tony0945

 *spofforth wrote:*   

> My Huawei router supposedly supports this, I have a subnet setup with DHCP leasing only half of the available IPs. I have one static IP configured via MAC address. Yes, the MAC is correct, yes, I tried both lower and upper-case. Nevertheless, the IP the host gets is always in the DHCP range and never the one I configured.

 

A bug. 

My ancient DLINK router does this. it's so old that I can't set the date correctly. it's off the router's upper limit.

I have a one year old Edgerouter-X that does this flawlessly also.

----------

## pietinger

 *spofforth wrote:*   

> My Huawei router supposedly supports this, I have a subnet setup with DHCP leasing only half of the available IPs. I have one static IP configured via MAC address. Yes, the MAC is correct, yes, I tried both lower and upper-case. Nevertheless, the IP the host gets is always in the DHCP range and never the one I configured.

 

Maybe the reason is ... a wrong subnet ... because STEP 2 in the first post is WRONG.

Wrong: If your router gives addresses like "192.168.1.101", then you might use "192.168.0.42". Just avoid "xx.xx.0.1" - that's usually the router's IP address.

Correct: If your router gives addresses like "192.168.1.101", then you might use "192.168.1.42". Just avoid "xx.xx.1.1" - that's usually the router's IP address.

The first example in step 2 can work, because 172.16.0.xx is a class-B-address, but 192.168.x.y is a class-C-address. See more here: https://en.wikipedia.org/wiki/Private_network and https://en.wikipedia.org/wiki/IP_address

----------

## Tony0945

pietinger,

Good catch.

But the example said " Mask:255.255.0.0 "  You are correct for. 255.255.255.0

OP should double check his router to see if it gives out 256 (really 254)  or 65,536 (really 65,534)

If it's an eight bit range, investigate whether the default address is changeable.

My DLINK's default is 192.168.0.1.

My Edgerouter's uses 192.168.1.1      I changed it to  192.168.0.2 .

Changing the default is a PITA because often other things need to be changed. I have lots of devices so I did it.

If yopu have fewer than five devices, just change your devices.

First check your Router's manual does it really give 65,000 addresses?

----------

## Hu

OP may not have that router anymore.  Prior to spofforth's raising this thread from the dead, the most recent post was from Tony back in 2009.

----------

## Tony0945

 *Hu wrote:*   

> OP may not have that router anymore.  Prior to spofforth's raising this thread from the dead, the most recent post was from Tony back in 2009.

 

I didn't even notice that!  My e-mail popped up and I answered it.  Perhaps I should mark this and similar threads as "stop watching"

----------

## pietinger

 *Tony0945 wrote:*   

> But the example said " Mask:255.255.0.0 "  You are correct for. 255.255.255.0

 

Tony, you are right ... IF ... he WOULD set this netmask. But you saw the netmask 255.255.0.0 for his 172-address in his post.

Anyway, I wanted to answer @spofforth with his router problem; and usually you cant change the netmask in SOHo-Routers - in most cases it is fixed 24 bit for a 192.168.x.y address. I have a Huawei-Router also (from german Telekom; its only renamed to Speedport but its a Huawei). I am only allowed to change the last two Bytes (x,y) of its own ip address and have no chance to set another netmask. I think you have a "better" router with more options.

P.S.: There is also no need to do supernetting a 192.168 address range with a netmask of 16 bit !Last edited by pietinger on Tue Sep 28, 2021 6:04 pm; edited 1 time in total

----------

## Tony0945

Yes, the Edgerouter is Linux based. The DLINK was highly rated in it's day.

----------

