New user's registration have been closed due to high spamming and low trafic on this forum. Please contact forum admins directly if you need an account. Thanks !

Static IP for computer X with DHCP?

Got problems with your B2 or B3? Share and get helped!
Post Reply
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Static IP for computer X with DHCP?

Post by artoo »

Is there a way to configure my B3 to give computer X the same IP number every time?
This could be done with my router, just by clicking on the computer/MAC address and then set what IP it should get.
If not, can you add this to the wish list?
Cheeseboy
Posts: 789
Joined: 08 Apr 2007, 12:16

Re: Static IP for computer X with DHCP?

Post by Cheeseboy »

This is a bit ugly, but it was the best I could come up with when I needed what you ask for.

Look in /var/lib/dnsmasq/dnsmasq.leases for the computer you wish to have a constant IP address.
You might have to look for the MAC address...
Stop dnsmasq and then edit the file, updating the first column of the row for the machine in question.
This is the expiry date, in UNIX-time/epoch format. Set it to a date well in the future, then restart dnsmasq. That way the client should get the same address even if it is not used in the network for long periods of time.

To convert epoch format to a more human-friendly format:

Code: Select all

niklas@b3:~$ date -d @1348404305
Sun Sep 23 14:45:05 CEST 2012
And the other way around:

Code: Select all

niklas@b3:~$ date -d "2015-01-01" +%s
1420066800
Please be careful. Always make a backup before you change system files.
Also make sure you have a way to restore your backup if something bad happens. This might require you to boot using a USB-stick, then mount /dev/sda1 and reverse your changes.
Binkem
Posts: 388
Joined: 10 Jul 2008, 02:26

Re: Static IP for computer X with DHCP?

Post by Binkem »

Why not just assign the fixed ip to your computer? Then there's no need to ask dhcp for an adress?
Gordon
Posts: 1461
Joined: 10 Aug 2011, 03:18

Re: Static IP for computer X with DHCP?

Post by Gordon »

That *is* ugly. And it probably won't work as expected.

Here's what you can do (don't use "bubba" for Hostname!):

Code: Select all

echo "dhcp-host=<MAC-address>,<Fixed_IP>" > /etc/dnsmasq.d/<Hostname>.conf
(if you don't know the MAC-address you can find it in the leases file - it's the second column)

Restart dnsmasq to activate the rule
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

I could use static IP, but that is not the solution I want. There are many computers/network adapters that I want to manage this way. Manual config is not the right way to go.

Working as sysadm has made me want to automate in every possible way, from network settings to software installation. ;)
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

Gordon wrote:That *is* ugly. And it probably won't work as expected.

Here's what you can do (don't use "bubba" for Hostname!):

Code: Select all

echo "dhcp-host=<MAC-address>,<Fixed_IP>" > /etc/dnsmasq.d/<Hostname>.conf
(if you don't know the MAC-address you can find it in the leases file - it's the second column)

Restart dnsmasq to activate the rule

Now we're getting somewhere, I'll try this right away! :)

I'm used to NIS+DNS+DHCP at work (but no, I'm no *nix admin, I just learned which files to edit), we use static IP with DHCP for all our clients (and some of the less important servers).
Cheeseboy
Posts: 789
Joined: 08 Apr 2007, 12:16

Re: Static IP for computer X with DHCP?

Post by Cheeseboy »

Thanks Gordon!
I'll have to give that a go myself :-)
Gordon
Posts: 1461
Joined: 10 Aug 2011, 03:18

Re: Static IP for computer X with DHCP?

Post by Gordon »

Do note that it is not required to create separate files for each host you want to configure this way. I don't even think the files need to be named *.conf, so there's a lot of ways to approach this. e.g. you could name the files <hostname>.<IP> for a direct view. I myself don't have many hosts that I need to configure with a fixed IP (the LAN attached JetDirect printer is one of them of course) and placed all the hosts in a single file.

Here's a snippit from my /etc/dnsmasq.d/hosts.conf:

Code: Select all

# Laserprinter - NPI3F6C38 - HP 2100 printer
dhcp-host=00:10:83:3f:6c:38,192.168.10.50

# Squeezebox Touch (Assurancetourix)
dhcp-host=00:04:20:22:b6:5d,192.168.10.40

# Squeezebox Radio (Maestria)
dhcp-host=00:04:20:27:8d:20,192.168.10.41
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

I think I did what you told me to, but I dont get any IP from the HTPC at the moment.
What did I do wrong?

Code: Select all

root@b3:/etc/dnsmasq.d# ls -l
total 12
-rw-r--r-- 1 root root 211 Sep 19  2010 README
-rw-r--r-- 1 root root 207 Sep 14 17:06 bubba.conf
-rw-r--r-- 1 root root 101 Sep 14 17:54 hosts.conf
root@b3:/etc/dnsmasq.d# cat hosts.conf
#PCDAVID
dhcp-host=00:1b:11:ba:da:cb,192.168.10.50

#HTPC
dhcp-host=00:16:d4:06:49:58,192.168.10.100
root@b3:/etc/dnsmasq.d# /etc/init.d/dnsmasq restart
Restarting DNS forwarder and DHCP server: dnsmasq.
Gordon
Posts: 1461
Joined: 10 Aug 2011, 03:18

Re: Static IP for computer X with DHCP?

Post by Gordon »

Don't know, looks good to me. Could be a problem with the DHCP client though. If it's a Windows machine try the following sequence in a cmd box:

Code: Select all

nbtstat -RR
ipconfig /release
ipconfig /renew
Can you verify that UDP ports 67:68 are up?
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

It is Windows, and I have released and renewed.
I don't get any IP address from my android phone either. Do I dare to restart B3 or will I get a box with no working network?

How can I completely restart the DHCP service? (or is that what I did with /etc/init.d/dnsmasq restart?)
Is there a log I can read? I looked in /var/log but I could not identify any dns or dhcp log.

I have now removed the new file (hosts.conf) and just want everything back to normal again, then when I know how to restart the service Ill give it another try.
Gordon
Posts: 1461
Joined: 10 Aug 2011, 03:18

Re: Static IP for computer X with DHCP?

Post by Gordon »

I'm currently not able to verify, but if there's no separate log for dnsmasq messages should appear in the system log. You could check this right after restarting dnsmasq to see if there are configuration issues.

Are both addresses you specified in the dhcp lease range? You can check in the bubba.conf file in that same directory.
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

Now my other client tried to renew and failed so I had to set a static IP temporary.
I restarted my B3 and the same after restart. I set my B3 as the DNS-server and when I do that I can't go anywhere, so it seem to be a problem with the DNS service, which is strange since I havnt edit any other files than the new file I created and deleted when it didnt work.
artoo
Posts: 16
Joined: 12 Aug 2011, 13:20

Re: Static IP for computer X with DHCP?

Post by artoo »

I solved the second problem by changing the profile for the network to "Automatic network settings" and then back to "Router + Firewall + Server". Then everything started to work again.
Now Ill give it another try with those static IPs.

Thank you for your time!
Post Reply