Solved

hostname change script on rhel

Posted on 2014-03-10
3
418 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, EE MVE 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 62

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.:

803 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