I just need to rename the hostnames of all the Desktops based on their IP Addresses. For example if the IP address is 172.29.72.25 hostname should be assigned as node25, if the IP address is 172.29.72.26 hostname should be node26 etc etc. I have keybased ssh access to all these hosts. I thing this is possible via bash script, can someone help me? Thanks in advance.
-
Is there a reason you're doing this by ssh-ing into a bunch of machines instead of configuring a DHCP server with hostname options? – RobotHumans Jan 29 '12 at 06:47
-
We have given IP based access to most of the machines and that is why i haven't used DHCP server. – karthick87 Jan 31 '12 at 04:04
-
Valid, I just don't see the distinction between a DHCP server with static MAC to IP mappings handing out hostnames as well. – RobotHumans Jan 31 '12 at 06:02
-
When you use SSH to connect to these hosts, are you also able to use the same username and password to connect to each of them? And is this user able to use sudo? – Feb 01 '12 at 21:06
-
I don't know if this setup is going to grow at all in the future, but unless you want to ask this question again in a year but with an order of magnitude more units I'd suggest a DHCP server. – Huckle Feb 03 '12 at 04:14
-
@Christopher yes we use same username & password to connect to this machine. – karthick87 Feb 04 '12 at 12:27
1 Answers
Hostname is stored in /etc/hostname
Where address
is a list of your IP addresses :
for A in `cat address` ; do NODE="node`echo $A | cut -d. -f4`" ; ssh $A "echo $NODE > /etc/hostname" ; done
For each IP, we cut the last field off, prefix it with "node" and write it to /etc/hostname
Now, that ought to work, but it will only work if your user has write privileges to /etc/hostname, which by default, standard users don't have on Ubuntu.
What you can do is feed your password to sudo via STDIN ; e.g. echo "password" | sudo -S
; note that this is not secure, because you'll leave your password in your shell history. Find it with the history
command and delete this with history -d <historyId>
.
Don't, of course, write your password to a plain text file in your home folder...
So, altogether
for A in `cat address` ; do
NODE="node`echo $A | cut -d. -f4`"
ssh $A "echo 'myPassword' | sudo -S echo $NODE > /etc/hostname"
done
-- edit
If you want to rewrite the hosts file on each machine, you could add a little awk to the mix ; replace the pattern 127\.0\.0\.
with one that actually matches your subnet. It will write the remaining lines of the file untouched.
/^127\.0\.0\./ {
split($1, quads, /\./)
print $1 "\tnode" quads[4]
next
}
{ print $0 }
This will replace all hostnames for IP addresses in your subnet with "nodeN" where N is the last quad of the IP address.
So we add these commands to the for A
loop above (compressing the awk script onto a single line and escaping characters as required). We don't need to be root to read /etc/hosts
, but we do need to be root to replace it after we write it's replacement to /tmp
ssh $A "awk '/^127\.0\.0\./ { split(\$1, quads, /\./) ; print \$1 \"\tnode\" quads[4] ; next } { print \$0 }' /etc/hosts > /tmp/hosts.new"
ssh $A "echo 'myPassword' | sudo -S mv /etc/hosts /etc/hosts.old"
ssh $A "echo 'myPassword' | sudo -S mv /tmp/hosts.new /etc/hosts"

- 5,216
-
It wont work i guess. Because we need to change the hostname in
/etc/hosts
also. Pls check and update your answer. – karthick87 Jan 24 '12 at 16:03 -
See already static IP is configured in all the machines i dont want to assign IP's now. I just want to change the hostnames alone. – karthick87 Jan 25 '12 at 13:52