IP Setting on CentOS6 using Shell Script

In this tutorial, I’ll show you that how we can do initial settings on CentOS6.x using shell script:

First create the script :

vi setting.sh

Screen Shot 2014-09-16 at 1.40.23 pm

Copy and Paste the following code into it (Modify the Hostname and IP information,as per your requirement):


if [ $# -eq 5 ]

echo ""
echo "Taking the backup and Changing the hostname from $(hostname) to $1 ..."

sed -i.bk "s/$(hostname)/$1/g" /etc/sysconfig/network

echo ""
echo "Backing up & Assigning the Static IP ..."
echo ""

cp /etc/sysconfig/network-scripts/ifcfg-$2 /etc/sysconfig/network-scripts/$2.bk

cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$2


echo "Changing the dns ..."
echo ""

sed -i.bk "s/nameserver.*/nameserver $3.$5/" /etc/resolv.conf

echo "Adding $1 as hostname to the /etc/hosts file .."
echo ""

sed -i.bk "/$(hostname)$/d" /etc/hosts
echo "$3.$4 $1" >> /etc/hosts

echo "Restarting the Network Service, Please connect it using the new IP Address if you are using ssh ..."

service network restart


echo "Usage: ip.sh <hostname> <interface> <baseip> <ipaddress> <gateway/dns>"
echo "Example: ip.sh testname eth0 10.10.10 41 1"


Then set the execute permission for your shell script:

chmod +x setting.sh

Screen Shot 2014-09-16 at 1.41.42 pm

Now, execute the shell script as sudo user:

sudo ./setting.sh

Screen Shot 2014-09-16 at 1.43.45 pm

Hope this will help you!

Please Remember me in your prayers!

Enjoy :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: