Solved

hostname change script on rhel

Posted on 2014-03-10
3
414 Views
Last Modified: 2014-03-10
I am trying to use this script to accomplish:
vi /etc/sysconfig/network
vi /etc/hosts
service network restart

It fails on line 21

#!/usr/bin/env bash
      
OLD_HOSTNAME="$( hostname )"
NEW_HOSTNAME="$1"
 
if [ -z "$NEW_HOSTNAME" ]; then
echo -n "Please enter new hostname: "
read NEW_HOSTNAME < /dev/tty
fi
 
if [ -z "$NEW_HOSTNAME" ]; then
echo "Error: no hostname entered. Exiting."
exit 1
fi
 
echo "Changing hostname from "$OLD_HOSTNAME" to "$NEW_HOSTNAME..."
 
hostname "$NEW_HOSTNAME"
sed -i "s/HOSTNAME=.*/HOSTNAME=$NEW_HOSTNAME/g" /etc/sysconfig/network
echo -e "$( hostname -I | awk '{ print $1 }' )\t$NEW_HOSTNAME" >> /etc/hosts
echo "Done."::q
0
Comment
Question by:Peter Kuczynski
3 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 39919142
Try this:

#!/bin/bash
	 
OLD_HOSTNAME="$(hostname)"
NEW_HOSTNAME="$1"
 
if [ -z "${NEW_HOSTNAME}" ]; then
echo -n "Please enter new hostname: "
read NEW_HOSTNAME
fi
 
if [ -z "${NEW_HOSTNAME}" ]; then
echo "Error: no hostname entered. Exiting."
exit 1
fi
 
echo "Changing hostname from ${OLD_HOSTNAME} to ${NEW_HOSTNAME}"
 
hostname "${NEW_HOSTNAME}"
sed -i "s/HOSTNAME=.*/HOSTNAME=${NEW_HOSTNAME}/" /etc/sysconfig/network
echo -e "$(hostname -I | awk '{ print $1 }')\t${NEW_HOSTNAME}" >> /etc/hosts
echo "Done."

Open in new window

0
 
LVL 61

Expert Comment

by:gheist
ID: 39919176
Kind of why go so complex?
echo HOSTNAME=$new_hostname >> /etc/sysconfig/network
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 39919319
Thank you all around!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now