Solved

hostname change script on rhel

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.

679 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