Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I change server name? 1

Status
Not open for further replies.
Jul 4, 2003
19
US
Hello all,
I'm trying to create a script (with virtually no scripting experience) to convert the /etc/hosts file to reflect a new name for the server. Any ideas on how to go about doing that? Any info would be much appreciated. Thanks.
 

Use vi

(man vi)


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Which OS? Regardless, you'll probably also need to make several other changes to get this to work.
 
This is on a Unix machine running Solaris 7. I could do it using vi but that takes a while and we are going to be constantly changing the name of the machine so I'd like to script it so it can be done quickly each time. Thanks
 
assuming you change the name of server 1.2.3.4 often:
Code:
sed 's/^1.2.3.4.*/1.2.3.4\tnew.name.here alias/' /etc/hosts
but this isn't changing the file itself.

Code:
#!/bin/bash
name=$1
aliasname=$2
ip=192.168.1.1
cp /etc/hosts /etc/hosts.backup
sed 's/^'$ip'.*/'$ip'\t'$name' '$aliasname'/' /etc/hosts.backup > /etc/hosts

Errorchecking (missing arguments) is left as an excercise to you :)

seeking a job as java-programmer in Berlin:
 
It's not just [tt]/etc/hosts[/tt] you'll need to change. Also look for a file called [tt]/etc/hostname.<dev>[/tt] where [tt]<dev>[/tt] is the network device. This will have a single entry that is the hostname. This must correspond to the primary IP for the machine in the [tt]/etc/hosts[/tt] file.

In other words (assuming your network device is an "hme"), the following commands would do it (borrowing from stefanwagner's script)...
Code:
#!/bin/ksh
NEW=${1}
IP=192.168.1.2
DOMAIN=.mydomain.com
TIMESTAMP=$(date '+%Y%m%d_%H%M%S')

cp /etc/hosts /etc/hosts.${TIMESTAMP}
sed "^/^${IP}.*/${IP}\t${NEW}.${DOMAIN} ${NEW}" /etc/hosts.${TIMESTAMP} > /etc/hosts

echo ${NEW} > /etc/hostname.hme0
This will allow the new name to survive a reboot. Without it the networking will break.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top