Ubuntu: Changing Hostname from Command Line

As described at it’s straight forward:

sudo /bin/hostname mynewhostname

Supplying the path to the binary is for security reasons, I guess, to make sure we have the right bin (eventhough it could have been replaced there, too…).

But the people at debianadmin.com forgot to mention in order to avoid “hostname: Unknown Host” you have to

sudo edit /etc/hosts

and change it there, too!


  1. Satish Thakur said,

    Tuesday, 20th Dec 2011 at 09:32

    Dear Friends,
    How i can configure hostname by command line in Ubuntu? Which commnad and configuration file? like /etc/sysconfig/network..??

  2. James said,

    Friday, 5th Aug 2011 at 01:51

    sudo perl -p -i -e ‘s/oldname/newname/’ /etc/hosts

    (oops, dropped the trailing slash in the regex)

  3. James said,

    Friday, 5th Aug 2011 at 01:49

    sudo hostname newname
    sudo sh -c ‘hostname > /etc/hostname’
    sudo perl -p -i -e ‘s/oldname/newname’ /etc/hosts

    for a completely hands-off approach.

    I normally reboot after doing this, but if you can’t reboot I advise you restart each service

  4. Monday, 25th Apr 2011 at 14:35

    sudo /bin/hostname mynewhostname
    sudo /bin/hostname > /etc/hostname
    sudo nano /etc/hosts

  5. JJ Gunn said,

    Monday, 11th Apr 2011 at 22:25

    Changed hostname in all 3 places hostname (newname) /etc/hosts and /etc/hostname reboot. Name stays the same. This isn’t working for me

  6. Mike Bethany said,

    Tuesday, 1st Mar 2011 at 18:24

    Thanks for the info.

    There is a problem though. You have to edit the /etc/hostname file too or the next time you reboot you’ll be back to the old name.

    I’d also suggest in your example you use a real console editor name like “nano” instead of “edit” since that might confuse a novice (like me). If you run “man edit” it shows info for RUN-MAILCAP so…

    Anyway, the example should add:

    $ nano /etc/hostname

    Thanks again for the info!

  7. Addinall said,

    Wednesday, 3rd Nov 2010 at 05:52

    NICE page. You have shamed me into touching mine again!

  8. Wednesday, 28th Apr 2010 at 15:59

    […] Ubuntu: Changing Hostname from Command Line […]

  9. Marcin Szamotulski said,

    Monday, 4th May 2009 at 18:42

    I had to change my host name, and I followed these instructions. Everything works ok (sysctl kernel.hostname give the new hostname) but the cron which is trying to send emails to the old hostname and I get a lot of email notifications. How do cron access for hostname (I do not see this in man pages)?
    I’ve checked for any env. variables and there is nothing what contains my old hostname. I also made grep -r oldhostname in \etc directory (a desperaccy act) and I do not find my old hostname.

    Thanks for any help.

    • Raghu Prasad said,

      Monday, 24th Aug 2009 at 14:29

      Have you tried re-starting the cron daemon?

      /etc/init.d/cron restart


Leave a Reply to James Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: