0

I am doing a project for uni and an unsure how to proceed i have been given the task of changing hosts name from one thing to another on a unix box I am logged in to the box as root

the host name is garnet.bct.bolton.ac.uk garnet I need to change .bct to .amt

The new host name should be garnet.amt.bolton.ac.uk garnet

I have multiple hostname in cat /etc/hosts and they all need changing from bct to amt

I think I need to use grep but that is as far as I have gotten

cat /etc/hosts | grep bct

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

1

Yo've not stated what your *nix is. But...

From what I can gather, you have two tasks there:

  1. Change the hostname of the unix box
  2. Change how that unix box resolves other systems on the network.

For the first, you need to edit the file /etc/hostname and change it.

nano /etc/hostname

For the second, you need to change all the instances of bct to amt in /etc/hosts. You can either do this with an interactive editor or you can script it with sed.

Interactively:

nano /etc/hosts

With sed:

First, run:

sed 's/\.bct\.bolton/\.amt\.bolton/g` /etc/hosts

and make sure that it looks ok. That simply shows you what it would change. To make the changes, add the -i option:

sed -i 's/\.bct\.bolton/\.amt\.bolton/g` /etc/hosts
garethTheRed
  • 33,957
  • thank you i will give it a try. i thought that sed only changed the display output though? also i dont know what *nix is –  Dec 02 '14 at 13:25
  • sed with no option will print the changed file to stdout (terminal normally). With the -i option it changes the file in-place. It's in the man page - man sed :-) – garethTheRed Dec 02 '14 at 13:28
0

Run this:

sed -i 's/\.bct/\.amt/g' /etc/hosts

Also, please be aware that if you are changing the hostname of the machine in which you are logged in, then making change in /etc/hosts alone will not suffice. If you are on Centos/Fedora/RHEL, the hostname has to be changed in the file /etc/sysconfig/network as well.

Sreeraj
  • 5,062
  • would i have to do the same if i am using linux minx? and thank you –  Dec 02 '14 at 13:26