LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   script to change IP and hostname under Solaris 10 (https://www.linuxquestions.org/questions/solaris-opensolaris-20/script-to-change-ip-and-hostname-under-solaris-10-a-445957/)

xpucto 05-18-2006 02:04 AM

script to change IP and hostname under Solaris 10
 
Hi!

I have to migrate from solaris 9 to solaris 10. The solaris 10 is ready and I would like to test it. Therefore I would like to write a script in order to change easily the IP address and the hostname. Quite a lot of files seem to be involved. Should I reither use "sys-unconfig" or edit directly /etc/hostname.*, /etc/hosts, /etc/inet/ipnodes and /etc/nodename (any other files?). Does anyone have maybe such a script?

Under http://sysunconfig.net/unixtips/sola...ame_script.txt
I found a script to change the hostname:

Quote:

#!/usr/bin/bash

# Sandra Henry-Stocker - v. 1.0
# David Cashion - v. 1.1 - Corrected errors and added ipnodes

# ---- prompt for new hostname, current hostname is default ----
oldName=`uname -n`
newName=`ckstr -p "Enter new Hostname [$oldName]" -d $oldName`

# ---- confirm change ----
ok=`ckyorn -p "Change hostname to $newName?"`

case $ok in
[Yy]*)
echo OK
;;
[Nn]*)
echo exiting without changes
exit 1
;;
esac

# ---- make changes to /etc files ----
perl -i -p -e "s/$oldName/$newName/" /etc/nodename
perl -i -p -e "s/$oldName/$newName/" /etc/hostname.*
perl -i -p -e "s/$oldName/$newName/g" /etc/inet/hosts
# If it is a softlink, this will change it into a real file, we don't want that
[ ! -h /etc/hosts ] && perl -i -p -e "s/$oldName/$newName/g" /etc/hosts
perl -i -p -e "s/$oldName/$newName/g" /etc/inet/ipnodes

# ---- make changes to /etc/net files ----
for hostsFile in `find /etc/net -name hosts -print`
do
perl -i -p -e "s/$oldName/$newName/g" $hostsFile
done

# ---- move or create /var/crash directory ----
if [ -d /var/crash/$oldName ]; then
mv /var/crash/$oldName /var/crash/$newName
else
mkdir /var/crash/$newName
chmod 700 /var/crash/$newName
fi
dumpadm -s /var/crash/$newName

# ---- finally, change hostname ----
uname -S $newName

# ---- confirm changes ----
uname -a
But I do not understand exactly how it works and do not know how to expand it so that it also changes the IP address (I haven't tested it yet though, and I actually do not know if it works).

Thanks for your help.

apt-get-dude 05-20-2006 07:55 PM

I would just go with sys-unconfig. It is safer in my opinion.

xpucto 05-24-2006 09:32 AM

Quote:

Originally Posted by apt-get-dude
I would just go with sys-unconfig. It is safer in my opinion.

But as far as I understand, sys-unconfig removes a lot of infos like the nameservers in /etc/resolv.conf or Regenerates keys for sshd! I do not want this! I just would like to test another ip and another hostname and have them back quickly if there is a problem.

could
Quote:

ifconfig bge0 plumb
be a possibility to change the IP?
Is there a command to change only the hostname? Is it enough to change it in /etc/hostname ? Or does it have to be changed in many other files?

Thanks.

mdhmi 05-24-2006 11:48 PM

I'm not a fan of sys-unconfig at all. Just change the /etc files and call it good.

Mark

AbrahamJose 05-29-2006 12:39 AM

related files
 
/etc/hosts
/etc/nodename
/etc/hostname.*
Plus files listed by
find /etc -name hosts -print

xpucto 06-06-2006 09:30 AM

Ok. I wrote a script. It seems to work. I tested it. Its purpose is to change quickly between 2 IPs and hostnames. I took all the related files listed by AbrahamJose and also added /etc/inet/ipnodes.

Quote:

#!/bin/bash

OPTIONS="Backup IP-myhost1 IP-myhost2 Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo The new IP and Hostname are:
cat /etc/hosts
cat /etc/nodename
cat /etc/hostname.bge0
cat /etc/inet/hosts
cat /etc/inet/ipnodes
echo done
echo You have to restart your computer!
exit

elif [ "$opt" = "Backup" ]; then
#Files'Backup
cp /etc/hosts /etc/hosts-back2
cp /etc/nodename /etc/nodename-back2
cp /etc/hostname.bge0 /etc/hostname.bge0-back2
cp /etc/inet/hosts /etc/inet/hosts-back2
cp /etc/inet/ipnodes /etc/inet/ipnodes-back2
echo Config Files have been backed-up

elif [ "$opt" = "IP-myhost1" ]; then
echo myhost1 has been chosen
cp /etc/hosts-myhost1 /etc/hosts
cp /etc/nodename-myhost1 /etc/nodename
cp /etc/hostname.bge0-myhost1 /etc/hostname.bge0
cp /etc/inet/hosts-myhost1 /etc/inet/hosts
cp /etc/apache2/httpd.conf-myhost1 /etc/apache2/httpd.conf
cp /etc/inet/ipnodes-myhost1 /etc/inet/ipnodes

echo The new IP and Hostname are:
cat /etc/hosts
cat /etc/nodename
cat /etc/hostname.bge0
cat /etc/inet/hosts
cat /etc/inet/ipnodes

elif [ "$opt" = "IP-myhost2" ]; then
echo myhost2 has been chosen
cp /etc/hosts-myhost2 /etc/hosts
cp /etc/nodename-myhost2 /etc/nodename
cp /etc/hostname.bge0-myhost2 /etc/hostname.bge0
cp /etc/inet/hosts-myhost2 /etc/inet/hosts
cp /etc/apache2/httpd.conf-myhost2 /etc/apache2/httpd.conf
cp /etc/inet/ipnodes-myhost2 /etc/inet/ipnodes

echo The new IP and Hostname are:
cat /etc/hosts
cat /etc/nodename
cat /etc/hostname.bge0
cat /etc/inet/hosts
cat /etc/inet/ipnodes


else
clear
echo bad option
fi
done


bathory 06-07-2006 03:12 AM

Just a remark.
There is no need to reboot for the changes to take effect. Just add to your script something like:
Code:

ifconfig bge0 down
ifconfig bge0 IP-myhost1 netmask xx.xx.xx.xx up


xpucto 06-07-2006 05:04 AM

Quote:

Originally Posted by bathory
Just a remark.
There is no need to reboot for the changes to take effect. Just add to your script something like:
Code:

ifconfig bge0 down
ifconfig bge0 IP-myhost1 netmask xx.xx.xx.xx up



I tried what you advice but it doesn't work. The server hangs and I have to set up everything back with the console since I loose every ssh connection and I cannot reconnect myself wether with the old nor with the new IP.

I also tried
Quote:

/sbin/ifconfig bge0 down
/sbin/ifconfig bge0 up
But it doesn't occure anything. I do not even lose the ssh-connection (shouldn't it be the case since the bge0 is being shut down?)

bathory 06-07-2006 07:41 AM

Oops, I thought that you're running the script locally and not through ssh.
Quote:

But it doesn't occure anything. I do not even lose the ssh-connection (shouldn't it be the case since the bge0 is being shut down?)
It should, but I guess this happens because, before the connection times out you bring back the interface up (keeping the old IP). To disable the interface you should run:
Code:

ifconfig bge0 unplumb
You can also try (locally of course) to run just:
Code:

ifconfig bge0 IP-myhost1 netmask xx.xx.xx.xx
This works for my sparc box and changes the IP instantly.

xpucto 06-08-2006 05:37 AM

Quote:

Originally Posted by bathory
Oops, I thought that you're running the script locally and not through ssh.

It should, but I guess this happens because, before the connection times out you bring back the interface up (keeping the old IP). To disable the interface you should run:
Code:

ifconfig bge0 unplumb
You can also try (locally of course) to run just:
Code:

ifconfig bge0 IP-myhost1 netmask xx.xx.xx.xx
This works for my sparc box and changes the IP instantly.

Thanks. I'm running through ssh though. Locally is a bit complicated and not very comfortable. So I guess I'll keep the restart option.


All times are GMT -5. The time now is 02:57 PM.