Visit Jeremy's Blog.
Go Back > Linux Answers > Networking
User Name


By jrwsod at 2009-12-21 11:07
I had searched for this answer for hours and finally was able to cobble together enough pieces of the puzzle to compile this "how to" document. There are many references to adding the DHCP_HOSTNAME parameter to the ifcfg-ethX file. While that approach will work for DHCP it will not for statically assigned IP addresses.

Please note: This "how to" only addresses registering a statically assigned IP dynamically with DNS - WINS/NETBIOS is not used. In this example I am using non-secure dynamic updates to a Windows 2003 DNS server that is active directory enabled. I have not used this with a DNS server that requires secure dynamic updates although there are options in nsupdate to facilitate this functionality (man nsupdate is your friend). In this example I have the DNS server to be updated already entered in the /etc/resolv.conf so I am not specifying it explicitly but that can be done as well. I am creating a record with a 1 day TTL, change that to match you standard TTL if needed. I am running this as root to avoid any confusion in the example but this command can be run as a regular user as well.

I’ll get right to the commands to do this manually so you can see how it works. Basically this is done using the command nsupdate.
When you type nsupdate you get taken to a command prompt >. There are multiple commands available. The ones we will use are update add, update delete, send, answer and quit.

[root@testmachine1 ~]# nsupdate 
> update delete testmachine1.domain1.local
> update add testmachine1.domain1.local 86400 A
> send
> answer
Outgoing update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id:  61616
;; flags: qr ; ZONE: 1, PREREQ: 0, UPDATE: 1, ADDITIONAL: 0
;domain1.local.                 IN      SOA

testmachine1.domain1.local. 86400 IN A

> quit
The first thing we want to do is remove any existing A records for the hostname we are using. Once that is done we want to register a new A record for this host. The commands will not be processed until a send command is entered. Lastly we run a answer command to see what the response was for the last command. You could, if you wanted to see what was happening at each step, use the send and answer commands after each update.

A script for this can be created as follows:
ADDR=`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e s/.*://`
HOST=`hostname -f`
echo "update delete $HOST A" > /tmp/nsupdate.txt
echo "update add $HOST 86400 A $ADDR" >> /tmp/nsupdate.txt
echo “send” >> /tmp/nsupdate.txt
nsupdate /tmp/nsupdate.txt
remember to chmod +x the actual script file to make it executable.

by allanf on Mon, 2010-02-15 21:46
Why using so many applications to get the IP Address?
You are using 4 applications when you do (single line):
ADDR=`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e s/.*://`
Why not only use 2 applications and do (single line also):
ADDR=`/sbin/ifconfig eth0 | sed -n '/inet addr:/s=[^:]*:\([^ ]*\).*=\1=;2p'`
To handle an unassigned address:
ADDR=`/sbin/ifconfig eth0 | sed -n '/inet addr:/p' | sed 's=[^:]*:\([^ ]*\).*=\1='`


All times are GMT -5. The time now is 02:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration