Using shell script to change hostname(Fedora Core6)
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using shell script to change hostname(Fedora Core6)
I'm trying to make shell script which can change about 500 client's hostname.
I believe that rewriting /etc/sysconfig/network is needed to change hostname.
Does anyone have any idea to rewrite /etc/sysconfig/network by use of shell script??
#!/bin/sh
NEWHOST=$1
if [ $# -ne 1 ]
then echo "Usage: scriptname new_hostname"
exit 1
fi
sed -i.BAK -e "s/HOSTNAME=.*/HOSTNAME=$NEWHOST/" /etc/sysconfig/network
This script takes exactly one argument, like
Code:
./scriptname host.domain.com
If there are more arguments than one, or none at all, it exits. Otherwise, it replaces the line
HOSTNAME=<whatever>
with
HOSTNAME=<the supplied argument>
in /etc/sysconfig/network, making a backup of the original contents of the file to /etc/sysconfig/network.BAK
But I have more than 500 clients...
It seems very hard to give them hostname one at a time.
And I also have to change ip address at the same time.(without using DHCP)
Hmm. No, not the simplest. I can't think of a simple method for changing the IPs and hostnames of 500 client machines short of using DHCP. Even there, you would have to touch 500 boxes to enable DHCP on them.
Are the clients already configured to contact the server in anyway? If not, I just can't see how you going to get around having to touch all 500 machines.
How are you planning on assigning the IP addresses? Is there any kind of relationship to the current IP address and the new one that you want to assign? Is the new hostname based on the new IP address, or is there a relationship between new and old hostnames?
If there are such relationships, the changes could be made accordingly. Still, you would have to download and run the script on each client.
Havent played with vista yet, but since you can mount up the file system, is there a way you could
latch onto the hostname from the vista partition, and update the linux hostname ?
If you could find the vista hostname in some config file, or even a log file of some kind, you would then
be able to run 1 script to update them all.
Also curious as to why you have 2 IPs for each box. I usually set up my dualboot machines with the
same IP for each OS, since only one will be running at a time anyways....
>Also curious as to why you have 2 IPs for each box. I usually set up my dualboot machines with the
same IP for each OS, since only one will be running at a time anyways....
Yes I think the same way. It's nonsense.
But customer wants this system... Nothing I said made difference.
>Is the Vista boot already properly configured with the hostname and IP?
A few remarks:
1. The IP Linux = IP Win +100 rule can't apply to all hosts (the last number in the IP only ranges from 0 to 255). So, please provide the full IP numbering scheme for all your hosts.
2. Small enhancement to zaichik's script:
Code:
#!/bin/sh
NEWHOST=$1
if [ $# -ne 1 ]
then echo "Usage: $0 new_hostname" #better use $0 or `basename $0` here,
#just in case you rename the script later on
exit 1
fi
sed -i.BAK -e "s/HOSTNAME=.*/HOSTNAME=$NEWHOST/" /etc/sysconfig/network
3. Do you have a list of the current hostnames?
And is there some way to remotely execute commands on them (ie login via ssh or rsh)?
If you could ssh to them, then you could use "scp" to distribute your script to all hosts and then use "ssh" to run it on each of the hosts, in a simple for-loop. If you could use rsh, maybe "rdist" can help as well.
sub='192.168.10.';
for (( i=100; i<=200; i=$i+1 )); do #to loop over all hosts, ie per sub-domain
num=`printf '%3.3d' $i`;
hostnum=(($i -100));
hostnum=`printf '%2.2d' $hostnum`;
scp yourScript.sh you@${sub}{num}:/some/path
#now that we've copied your script to the remote hosts, we can execute it on each host
ssh you@lin${num}.your.domain "/some/path/yourScript.sh lin${hostnum}"
if (( $? != 0 )); then
echo "Remote command failed on iteration $i";
fi;
done
As I'm not sure that I got all the syntax right, you might want to replace the remote execution of yourScript.sh with a harmless echo command first, like:
and see if that prints the right commands to execute.
Of course, you can encapsulate the above for-loop in a second loop that runs over all 4 sub-domains (ie for (( net=10; net<60; net=$net+10 )); do sub="192.168.${net}"; ... done)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.