Solved

hostname change script on rhel

Posted on 2014-03-10
3
416 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

920 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

13 Experts available now in Live!

Get 1:1 Help Now