Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Switching to a static IP (within your network)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
StringCheesian
l33t
l33t


Joined: 21 Oct 2003
Posts: 887

PostPosted: Wed Mar 18, 2009 8:28 am    Post subject: Switching to a static IP (within your network) Reply with quote

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:
Code:
su
(type your root password)
ifconfig

It will probably show something like this:
Code:
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:
Code:
# 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:
Code:
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.
Code:
/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
Back to top
View user's profile Send private message
muhsinzubeir
l33t
l33t


Joined: 29 Sep 2007
Posts: 948
Location: /home/muhsin

PostPosted: Wed Mar 18, 2009 1:02 pm    Post subject: Reply with quote

great guide thanks....I have seen occasionally some router support static ip, if thats an option to someone :D
_________________
~x86
p5k-se
Intel Core 2 Duo
Nvidia GT200
http://www.zanbytes.com
Back to top
View user's profile Send private message
MaximeG
l33t
l33t


Joined: 15 Apr 2008
Posts: 722
Location: Belgium

PostPosted: Wed Mar 18, 2009 3:12 pm    Post subject: Reply with quote

Hi,

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

Regards,
Maxime
_________________
Future is wide open.
Back to top
View user's profile Send private message
danomac
l33t
l33t


Joined: 06 Nov 2004
Posts: 881
Location: Vancouver, BC

PostPosted: Wed Mar 18, 2009 3:28 pm    Post subject: Reply with quote

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

Code:

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.
Back to top
View user's profile Send private message
furanku
l33t
l33t


Joined: 08 May 2003
Posts: 905
Location: Hamburg, Germany

PostPosted: Tue Mar 24, 2009 9:33 am    Post subject: Reply with quote

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.

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.
Back to top
View user's profile Send private message
StringCheesian
l33t
l33t


Joined: 21 Oct 2003
Posts: 887

PostPosted: Thu Mar 26, 2009 8:59 am    Post subject: Reply with quote

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.

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.

I'm sure you're right, but this way involves more tinkering with Gentoo and less mucking about with router configuration :). 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.
Back to top
View user's profile Send private message
MaximeG
l33t
l33t


Joined: 15 Apr 2008
Posts: 722
Location: Belgium

PostPosted: Thu Mar 26, 2009 3:51 pm    Post subject: Reply with quote

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 :)

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 ;)

Regards,
Maxime
_________________
Future is wide open.
Back to top
View user's profile Send private message
danomac
l33t
l33t


Joined: 06 Nov 2004
Posts: 881
Location: Vancouver, BC

PostPosted: Thu Mar 26, 2009 4:50 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
furanku
l33t
l33t


Joined: 08 May 2003
Posts: 905
Location: Hamburg, Germany

PostPosted: Thu Mar 26, 2009 6:20 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
danomac
l33t
l33t


Joined: 06 Nov 2004
Posts: 881
Location: Vancouver, BC

PostPosted: Thu Mar 26, 2009 8:36 pm    Post subject: Reply with quote

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.)
Back to top
View user's profile Send private message
furanku
l33t
l33t


Joined: 08 May 2003
Posts: 905
Location: Hamburg, Germany

PostPosted: Fri Mar 27, 2009 9:02 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
danomac
l33t
l33t


Joined: 06 Nov 2004
Posts: 881
Location: Vancouver, BC

PostPosted: Fri Mar 27, 2009 10:19 pm    Post subject: Reply with quote

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.) ;) I've seen very few that came with that ability out-of-the-box.
Back to top
View user's profile Send private message
MaximeG
l33t
l33t


Joined: 15 Apr 2008
Posts: 722
Location: Belgium

PostPosted: Sat Mar 28, 2009 6:02 pm    Post subject: Reply with quote

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. ;) But it's honestly a very common feature.

Regards,
Maxime
_________________
Future is wide open.
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Tue Apr 07, 2009 4:24 pm    Post subject: Reply with quote

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.
_________________
My political stance/bias
slycordinator != slycoordinator
Back to top
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Sat May 30, 2009 2:51 am    Post subject: D-Link DI-524 does this Reply with quote

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.
Back to top
View user's profile Send private message
spofforth
n00b
n00b


Joined: 16 Nov 2019
Posts: 12

PostPosted: Mon Sep 27, 2021 10:41 pm    Post subject: Reply with quote

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.

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.


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.
Back to top
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Mon Sep 27, 2021 10:54 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4133
Location: Bavaria

PostPosted: Tue Sep 28, 2021 9:25 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Tue Sep 28, 2021 2:09 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21607

PostPosted: Tue Sep 28, 2021 3:57 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Tue Sep 28, 2021 4:07 pm    Post subject: Reply with quote

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"
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4133
Location: Bavaria

PostPosted: Tue Sep 28, 2021 5:58 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Tue Sep 28, 2021 6:03 pm    Post subject: Reply with quote

Yes, the Edgerouter is Linux based. The DLINK was highly rated in it's day.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum