Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change the hostname in linux

6 views
Skip to first unread message

guddu

unread,
Jun 23, 2008, 1:17:06 AM6/23/08
to
Hi,
I am trying to change the host name of a linux machine.
I have modified /etc/hosts as follows :

127.0.0.1 localhost.localdomain localhost
##< IP ADDRESS> <OLD HOSTNAME>
<IP ADDRESS> <NEW HOSTNAME>

/etc/nsswitch.conf is modified as follows :
#hosts: db files nisplus nis dns
##hosts: files nisplus dns
hosts: files


/etc/sysconfig/network
##HOSTNAME=<OLD_HOSTNAME>
HOSTNAME=<NEW_HOSTNAME>

Then I have executed these on the shell :
hostname <new_HOSTNAME>

service xinetd restart


After executing these steps, my machine is not taking the new
hostname.
If I execute
#hostname

I get hostname as (none)

Can anyone tell me what is that which I am missing here.

Thanks in Advance!

guddu

unread,
Jun 23, 2008, 1:17:12 AM6/23/08
to

Nilantha Silva

unread,
Jun 23, 2008, 6:09:06 PM6/23/08
to
just write whatever hostname you want in the /etc/hostname file!!!!!
that's all dude!! if you use the hostname command, then it changes the
hostname for the current session only... when you reboot, it'll be reset
back to the old name.... but if you edit the /etc/hostname file, it'll
be your active hostname until you change it back..... and you don't need
to edit anything else....
have fun.

Nilantha

--------------------------------------------------------
"An expert is a person who has made all the mistakes
that can be made in a very narrow field." - Neils Bohr
--------------------------------------------------------

Francis Litterio

unread,
Jun 23, 2008, 8:06:39 PM6/23/08
to
Nilantha Silva wrote:

> just write whatever hostname you want in the /etc/hostname file!!!!! that's all
> dude!!

Dude, you did't tell him that the change doesn't take effect until the
next boot.
--
Fran

guddu

unread,
Jun 24, 2008, 12:44:52 AM6/24/08
to
Hi,
I had tried all that..and it didnt work. So I had to put a query here.
I had even rebooted the machine after modifying /etc/hosts/.

BTW, in my current Red Hat Linux, there is no /etc/hostname.
Its only /etc/hosts.

Dildo Bogumil di Boscopelo

unread,
Jun 24, 2008, 2:30:00 AM6/24/08
to
guddu wrote:

> Hi,
> I had tried all that..and it didnt work. So I had to put a query here.
> I had even rebooted the machine after modifying /etc/hosts/.
>
> BTW, in my current Red Hat Linux, there is no /etc/hostname.
> Its only /etc/hosts.
>

IIRC, go edit /etc/sysconfig/network. At reboot, RedHat reads the HOSTNAME
param written in there and rewrites /etc/hostname.

--
SF

Games are very educational. Scrabble teaches us vocabulary, Monopoly teaches
us cash-flow management, and Dungeons & Dragons teaches us to loot dead bodies.

Nilantha Silva

unread,
Jun 24, 2008, 5:29:27 PM6/24/08
to
sorry man... I only noticed that once I clicked "send"!!! I didn't want
to write it in another post and I thought he'd be smart enough to try a
reboot if he didn't notice the change immediately! anyway.. reboot is
not necessary... you can change your runlevel to 1 (depends on the
distro though! I'm using Debian etch) and change it back to whatever
runlevel you want... that should do the trick.... that's the best thing
about gnu/linux... only on very rare occasions you'll need to reboot!

NS

--------------------------------------------------------
"An expert is a person who has made all the mistakes
that can be made in a very narrow field." - Neils Bohr
--------------------------------------------------------

Nilantha Silva

unread,
Jun 24, 2008, 5:39:03 PM6/24/08
to
did you create a file then???? (/etc/hostname)......... try creating one
if you didn't... normally that's where the value of the $HOSTNAME env.
variable is stored... if you don't have it... you must get a null value
for the $HOSTNAME try "echo $HOSTNAME" if you get a blank line that
means the variable isn't initialized... if that's the case, create
/etc/hostname and it's contents should be the hostname you want... lets
say.. "MyHost"... just write MyHost in a file and save it in /etc/hostname
that should do it. if it doesn't work...reply to this post and I'll
check in our campus labs.. we have fedora installed and on some machines
redhat 9... I'm using debian so.. there might be something going on
there... but generally creating the file should solve your problem if
your $HOSTNAME is blank...

NS

Sebastian "lunar" Wiesner

unread,
Jun 24, 2008, 2:32:04 PM6/24/08
to
Nilantha Silva <nilanth...@gmail.com>:

> sorry man... I only noticed that once I clicked "send"!!! I didn't want
> to write it in another post and I thought he'd be smart enough to try a
> reboot if he didn't notice the change immediately! anyway.. reboot is
> not necessary... you can change your runlevel to 1 (depends on the
> distro though! I'm using Debian etch) and change it back to whatever
> runlevel you want...

"hostname --file /etc/hostname" will be sufficient, no need to reboot.

Btw, now that you mention Debian: /etc/hostname is a Debian-specific thing.
This file is nowhere standardised, other distributions are free to read the
hostname from whatever file they want. Gentoo for instance
uses /etc/conf.d/hostname. /etc/hostname doesn't exist there, and is just
ignored by the init scripts if created. Other distributions use different
files, RedHat and Suse have these settings under /etc/sysconfig, to give
another example.

Summarizing, you can't set the hostname permanently in a
platform-independent way.

Anyway its not a good idea to mingle in configuration files without the user
being involved. This happening on production systems can have bad
consequences to the functionality of the network and the mood of the
responsible admins ;)

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)

John Hasler

unread,
Jun 24, 2008, 5:13:49 PM6/24/08
to
Sebastian writes:
> Btw, now that you mention Debian: /etc/hostname is a Debian-specific
> thing.

/etc/hostname is not a Debian invention. It comes from Unix (though it has
now grown suffixes there).
--
John Hasler
jo...@dhh.gt.org
Dancing Horse Hill
Elmwood, WI USA

Sebastian "lunar" Wiesner

unread,
Jun 24, 2008, 6:04:56 PM6/24/08
to
John Hasler <jo...@dhh.gt.org>:

> Sebastian writes:
>> Btw, now that you mention Debian: /etc/hostname is a Debian-specific
>> thing.
>
> /etc/hostname is not a Debian invention. It comes from Unix (though it
> has now grown suffixes there).

Thanks for the correction. I just assume, that it was Debian-specific,
since it's the only linux distribution, if seen this file on.

Winfried Magerl

unread,
Jun 25, 2008, 11:35:25 AM6/25/08
to
Hi,

In article <g3rr3d$jb4$02$1...@news.t-online.com>,


Sebastian \"lunar\" Wiesner <basti....@gmx.net> wrote:
>John Hasler <jo...@dhh.gt.org>:
>
>> Sebastian writes:
>>> Btw, now that you mention Debian: /etc/hostname is a Debian-specific
>>> thing.
>>
>> /etc/hostname is not a Debian invention. It comes from Unix (though it
>> has now grown suffixes there).
>
>Thanks for the correction. I just assume, that it was Debian-specific,
>since it's the only linux distribution, if seen this file on.

SuSE:
/etc/HOSTNAME
SunOS-5.8:
/etc/hostname.<interface>

In both cases:
ls -l /etc/hostname
ls: cannot access /etc/hostname: No such file or directory

The user uses RedHat (whatever version) and I think it's much easier to
google for something like 'redhat set hostname' instead of getting a lot
of useless hints which might only with specific systems (including
all unix-derivates like SunOS, IRIX, SINIX, .....).

A quick google-search leads to /etc/sysconfig/network for RedHat.

The file /etc/sysconfig/network does not exist on SuSE and SunOS-5.8 which
naturally leads to the conclusion that neither /etc/hostname nor
/etc/sysconfig/network are standardised on any unix-clone.

If someone wants more detailed information about the setting of the
hostname, something like this might help (SuSE as an example):

# fgrep -i hostname /etc/init.d/*
[....]
/etc/init.d/boot.localnet:# Description: setup hostname and yp
/etc/init.d/boot.localnet: # set hostname and domainname
/etc/init.d/boot.localnet: XHOSTNAME=""
/etc/init.d/boot.localnet: test -f /etc/HOSTNAME && {
/etc/init.d/boot.localnet: XHOSTNAME=`cat /etc/HOSTNAME`
/etc/init.d/boot.localnet: test -n "$XHOSTNAME" && {
/etc/init.d/boot.localnet: echo -n Setting up hostname \'${XHOSTNAME%%.*}\'
/etc/init.d/boot.localnet: hostname ${XHOSTNAME%%.*}
[......]

It's trivial:
if /etc/HOSTNAME exists and is not empty then set the hostname
without domain attached.

regards

winfried

--
Winfried Magerl - Internet Administration
Siemens IT Solutions and Services, 81739 Munich, Germany
Internet-Mail: winfrie...@siemens.com

Nilantha Silva

unread,
Jun 25, 2008, 5:19:39 PM6/25/08
to
So, in other words... if you create the file (if it doesn't exist) will
solve the problem... and wasn't that what I was saying????????????????
it seems to me that dude doesn't have common sense...(no offense) if the
file doesn't exist, and somebody tells you that file is used to set the
hostname, why not create it and see what happens???????? why are people
so afraid to try something out??????? experiment dude.. experiment...
like Neils Bohr's quote say... it'll make you an expert!

NS

--------------------------------------------------------
"An expert is a person who has made all the mistakes
that can be made in a very narrow field." - Neils Bohr
--------------------------------------------------------

John Hasler

unread,
Jun 25, 2008, 12:17:04 PM6/25/08
to
winfried writes:
> The user uses RedHat (whatever version) and I think it's much easier to
> google for something like 'redhat set hostname' instead of getting a lot
> of useless hints which might only with specific systems (including
> all unix-derivates like SunOS, IRIX, SINIX, ...

Threads drift. The OP's problem ceased to be the subject of the
discussion.

Nilantha Silva writes:
> So, in other words... if you create the file (if it doesn't exist) will
> solve the problem... and wasn't that what I was saying???????????????? it
> seems to me that dude doesn't have common sense...(no offense) if the
> file doesn't exist, and somebody tells you that file is used to set the
> hostname, why not create it and see what happens?

If his distribution doesn't use the file creating it will have no effect.

BTW please don't top post.

Jerry Peters

unread,
Jun 25, 2008, 4:23:56 PM6/25/08
to
"Sebastian \"lunar\" Wiesner" <basti....@gmx.net> wrote:
> John Hasler <jo...@dhh.gt.org>:
>
>> Sebastian writes:
>>> Btw, now that you mention Debian: /etc/hostname is a Debian-specific
>>> thing.
>>
>> /etc/hostname is not a Debian invention. It comes from Unix (though it
>> has now grown suffixes there).
>
> Thanks for the correction. I just assume, that it was Debian-specific,
> since it's the only linux distribution, if seen this file on.
>
Slackware uses /etc/hostname also.

Jerry

Nilantha Silva

unread,
Jun 26, 2008, 4:43:35 AM6/26/08
to
John Hasler wrote:
> If his distribution doesn't use the file creating it will have no effect.
>
> BTW please don't top post.

sorry man, I'm new to this usenet thing.... I just thought this'll be a
good place to share what I know and learn something new... that's why I
started to post on newsgroups... I really didn't know there was a
certain protocol when replying to posts.. Won't happen again!

Well... back to the subject, if his distro doesn't use the /etc/hostname
file, he can always make the distro use it can't he? by modifying the
proper runlevel script or by creating a new "S" script in the proper
runlevel???? that has to work..... given that his hostname command works!

Bernhard Agthe

unread,
Jun 26, 2008, 4:50:00 AM6/26/08
to
Hi,

> I am trying to change the host name of a linux machine.

man hostname

Ciao...


Douglas O'Neal

unread,
Jun 26, 2008, 8:13:14 AM6/26/08
to
On 06/26/2008 04:43 AM, Nilantha Silva wrote:
> <snip>

>
> Well... back to the subject, if his distro doesn't use the /etc/hostname
> file, he can always make the distro use it can't he? by modifying the
> proper runlevel script or by creating a new "S" script in the proper
> runlevel???? that has to work..... given that his hostname command works!

He could do that, and do it again after he upgrades (or patches) his
system, and do it again when he upgrades again...

Better to learn how his disto sets the hostname and use that
mechanism.

Doug

Nilantha Silva

unread,
Jun 26, 2008, 5:18:07 PM6/26/08
to

I guess you're right... but that said, he can write his own "S" script
and reuse it every time he needs to..... that's an option.. cuz the
original question was that he couldn't get his distro to set the
hostname using its methods right... and if the original writer of this
post still reads these replies, it'll be nice if you can tell us which
version of RedHat you're using...

Douglas O'Neal

unread,
Jun 26, 2008, 12:29:58 PM6/26/08
to
On 06/26/2008 05:18 PM, Nilantha Silva wrote:
> Douglas O'Neal wrote:
>> On 06/26/2008 04:43 AM, Nilantha Silva wrote:
>>> <snip>
>>>
>>> Well... back to the subject, if his distro doesn't use the
>>> /etc/hostname file, he can always make the distro use it can't he? by
>>> modifying the proper runlevel script or by creating a new "S" script
>>> in the proper runlevel???? that has to work..... given that his
>>> hostname command works!
>>
>> He could do that, and do it again after he upgrades (or patches) his
>> system, and do it again when he upgrades again...
>>
>> Better to learn how his disto sets the hostname and use that
>> mechanism.
>>
>
> I guess you're right... but that said, he can write his own "S" script
> and reuse it every time he needs to..... that's an option.. cuz the
> original question was that he couldn't get his distro to set the
> hostname using its methods right... and if the original writer of this
> post still reads these replies, it'll be nice if you can tell us which
> version of RedHat you're using...
>
> NS


He would also have to find the right timing to start the script. Too
early and the distro's scripts will overwrite the hostname. Too late
and you might have network services start that base some info on the
hostname (apache comes to mind).

If the OP runs out of man pages to read (or just doesn't read them), he
can try
find /etc -type f -exec grep hostname {} /dev/null \;
for hints as to where the hostname is set on his system.

Doug

0 new messages