Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

hostname change script on rhel

Posted on 2014-03-10
3
Medium Priority
?
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 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

Technology Partners: 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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
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.
Suggested Courses

650 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