386

When I connect to my server (ubuntu server 10.10), I get this:

name@server-name.belkin ~>

How can I remove ".belkin"?

Jorge Castro
  • 71,754
Wolfy
  • 7,920
  • 1
    Try editing /etc/ hosts. gksudo gedit /etc/hosts and replace server-name.belkin by whatever name you desire. – LFC_fan Oct 26 '10 at 08:39
  • 2
    Do you want to change the actual server name, or just the way it's displayed in the prompt? – Dave Jennings Oct 26 '10 at 18:07
  • 2
    Voting to reopen, because this is a superset (allows restart). – Ciro Santilli OurBigBook.com Feb 05 '17 at 17:27
  • how does this question get closed in favor of one asked *over a year* later? – warren Aug 24 '17 at 16:42
  • @warren the current consensus is to close by "quality": http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha Since "quality" is not measurable, I just go by upvotes. ;-) Likely it comes down to which question hit the best newbie Google keywords on the title. – Ciro Santilli OurBigBook.com Apr 19 '18 at 11:59
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:12

14 Answers14

454

You need to edit the computer name in two files:

/etc/hostname 

and

/etc/hosts

These will both need administrative access, so run

gksu gedit /path/to/file

Replace any instances of the existing computer name with your new one. When complete run

sudo service hostname start

The name will also be changed if you restart your computer.

See also:

richzilla
  • 12,045
  • 46
    After that, just sudo service hostname restart and the hostname has been changed without rebooting. – Anthony O. Nov 20 '13 at 00:37
  • 6
    In Ubuntu 14.04 there is no service 'hostname'. What can I do there to avoid reboot. – Arpad Horvath Jul 28 '14 at 06:39
  • 1
    @ArpadHorvath status --system hostname tells me that the service exists. So, sudo service hostname restart or sudo restart hostname should work. You probably ran service hostname status and it didn't find it. Read this to learn why. –  Oct 30 '14 at 12:07
  • @BrunoNova Neither of them works. – Arpad Horvath Oct 30 '14 at 21:35
  • 8
    sudo service hostname restart doesn't work on default install of ubuntu server 14.04 on AWS. I had to do full server restart – gerrytan Oct 30 '14 at 21:47
  • 1
    That's strange. I used those commands after changing the hostname in two 14.04 and 14.10 VMs (Desktop version) and it worked. But I probably should have said sudo service hostname start (instead of restart), since status --system hostname returns hostname stop/waiting. Also, the current terminal didn't show the new hostname, I had to open a new terminal or shell to see it. –  Oct 30 '14 at 22:21
  • 9
    On 14.04, I simply ran sudo hostname, and that did the trick. I didn't notice any immediate change, but when I opened a new terminal, I saw my hostname had indeed changed. – TSJNachos117 Nov 22 '14 at 08:33
  • 14
    sudo hostname new-host-name worked for me on ubuntu 13.10 – Lekhnath Jan 14 '15 at 12:04
  • 3
    On 15.10, sudo service hostname restart fails with Failed to restart hostname.service: Unit hostname.service is masked. – Ciro Santilli OurBigBook.com Nov 30 '15 at 11:17
  • @Lekhnath note: this only works until the next reboot. – Ciro Santilli OurBigBook.com Nov 30 '15 at 11:17
  • @CiroSantilli六四事件法轮功包卓轩 you also have to edit /etc/hosts and replace the old hostname with the new one, so that error goes away. – Thomas Ward Nov 30 '15 at 11:41
  • What does it take to get the change propagated to the DNS server of my home router? – Abhishek Anand Dec 24 '15 at 14:40
  • 1
    I see in /etc/hosts 127.0.0.1 localhost - where is the old hostname ? – Dejel Jan 06 '16 at 13:11
  • What if /etc/hosts does not contain the machine name? – 2540625 Mar 15 '16 at 17:23
  • @ThomasWard I did just that and the error doesn't go away. – KeyC0de Nov 11 '16 at 14:05
  • 1
    What to write in /etc/hosts? Why nobody says this explicitly?! – Dims Jul 28 '17 at 11:05
  • @Dims, it says in the answer Replace any instances of the existing computer name with your new one. – richzilla Jul 28 '17 at 12:53
  • 1
    @richzilla there are no instances of existing computer name in hosts by deafult – Dims Jul 28 '17 at 12:56
  • "Unable to resolve host" when trying sudo service hostname start and sudo hostname. I'm using Ubuntu 16.04 LTS – Fabián Sep 23 '17 at 17:20
  • @Fabián Make sure /etc/hosts was changed. – E235 Apr 08 '18 at 15:32
  • 1
    Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:13
207

hostnamectl set-hostname on 13.10+ desktop

This is the best way if you have systemd (13.10 onwards) and if cloud-init is not active (see below):

hostnamectl set-hostname 'new-hostname'

It:

  • does not require rebooting
  • persists after reboots

More info at: https://askubuntu.com/a/516898/52975

18.04 onwards: cloud-init

18.04 Introduced cloud-init which can control setting of the hostname so hostnamectl changes it won't stick after a reboot if cloud-init is installed. TODO: how to check if it is installed, is it installed by default on the desktop image or just server?

If you want hostnamectl changes to stay after a reboot, then you'll need to edit the cloud-init config files, disable cloud-init's hostname set/update module:

sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg

or disable cloud-init entirely:

sudo touch /etc/cloud/cloud-init.disabled

See also: How do I change the hostname without a restart?

72

It's quite easy:

  1. Edit /etc/hostname, make the name change, save the file.

  2. You should also make the same changes in /etc/hosts file

  3. Run sudo service hostname start

As long as you have no application settings depending on the 'old' hostname, you should be ok ;-)

Pavlos G.
  • 8,844
  • 32
    WARNING: If you do this without changing /etc/hosts accordingly you will be unable to use sudo because your hostname will fail to lookup – João Pinto Dec 06 '10 at 16:10
  • You're right regarding editing the /etc/hosts, i forgot about it (just added it to my answer), though sudo seems to be working fine for me without changing it (i've restarted the machine and it still works) – Pavlos G. Dec 06 '10 at 16:14
  • I don't have any reference to my hostname (new or old) in /etc/hosts, only localhost and some IPv6 rules. – Oli Dec 09 '10 at 09:30
  • networkmanager adds it, at least that happens in my machine. – Pavlos G. Dec 09 '10 at 10:30
  • I have xubuntu 10.10 and when did the second step, it told me to run sudo service hostname restart. – Andrei Vajna Jan 07 '11 at 10:20
  • 1
    It's ok, you can do that as hostname is now a service managed from upstart. It'll do exactly the same thing,restart the service. – Pavlos G. Jan 07 '11 at 13:11
  • 2
    Note: this will not work for ubuntu 14.x, as hostname is no longer in init.d – Rápli András Oct 09 '14 at 11:42
  • If you ever find you can't restart hostname simply reboot the machine with sudo reboot. – starbeamrainbowlabs May 18 '15 at 16:13
  • sudo: /etc/init.d/hostname: command not found – chovy Jan 08 '16 at 07:04
  • What do you mean "same changes" in /etc/hosts? This file has completely different format! – Dims Jul 28 '17 at 11:05
  • 1
    Ubuntu 16.04 LTS: Failed to start hostname.service: Unit hostname.service is masked. – Chloe Nov 01 '17 at 05:15
  • 3
    Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:13
26

It is safe to do, you just need to be sure you edit both the system hostname configuration file (/etc/hostname) and the hostname name resolution file (/etc/hosts). From a terminal execute the following:

sudo -s
editor /etc/hostname
editor /etc/hosts
shutdown -ry now
João Pinto
  • 17,159
19

In addition to editing /etc/hosts and /etc/hostname, various services might have issues with the change as well. Mysql and postfix are installed by default in ubuntu. A broken postfix won't affect most ubuntu users, since it's a background email server that isn't used by much.

Postfix:

sudo editor /etc/postfix/main.cf
sudo service postfix restart

The default config for mysql doesn't use hostname, so it will work fine as-is. If you have customized it, edit the files in /etc/mysql/ and restart the service.

You may also want to edit /etc/motd (message of the day), which is shown on virtual terminals and remote logins. That one won't harm anything though.

Other services that you may have installed that would need fixing are apache, bind9, etc. In each case, find and edit the hostname in their config and restart the service.

mullens
  • 3
  • 2
15

Another better and safe way to rename hostname

Install ailurus

  • Add the PPA and update your repository

    sudo add-apt-repository ppa:ailurus && sudo apt-get update

  • Install ailurus

    sudo apt-get install ailurus

  • After installation it will be found under Applications>>System Tools>>Ailurus
    alt text

karthick87
  • 81,947
  • 5
    Why is this getting negative votes? Should be better to configure something with a dedicated tool than muck around on the command line and break things in the process. – endolith Jan 20 '11 at 03:49
  • 43
    Installing such a big software just to change a hostname is a bit of an overkill – Nemo Jun 17 '11 at 01:56
  • 11
    I agree that this is overkill -- in particular because the software not available in the repositories but has to be installed from a PPA... – Marcel Stimberg Jul 05 '11 at 16:55
  • @karthick87 perhaps mention in your answer that this is a large piece of software (with other configuration options)? - personally I agree with endolith that a tool with a single, simple change point is better than lots of command line operations (I use Ubuntu Tweak, also currently available from a PPA) – lofidevops Oct 14 '11 at 07:19
  • 1
    I think that you can break a lot more things (and more easily) using this tool, than just replacing a word in a couple of files. It exposed too many things to you. – gerlos Jan 20 '16 at 18:53
15

The host name uniquely identifies your computer on the local network (and possibly on the Internet as well) so it's not a good idea to change it unless you know what you are doing.

But you can change the shell prompt not to display the .belkin (domain name part):

export PS1='\u@\h \w> '

See the bash man page and specifically the section on prompting for more information.

10

Use the hostname command to change your hostname

sudo hostname newname

However, this does not edit your hosts file, which you must do so as to make sure that your computer recognizes itself

gksudo /etc/hosts

And add a new entry for your hostname pointing to 127.0.0.1

127.0.0.1 oldname newname

You could remove the old entry as well, but I prefer to keep it there.

Nemo
  • 9,460
9

If you don't want to play with a text editor, Ubuntu Tweak (grab the deb from their website) has that as one of the little things you can play with (along with lots of other little tweaks that you might want to make but don't really want to play around with the terminal and the files themselves).

dkuntz2
  • 489
  • Out of curiosity, is there any particular reason why Ubuntu Tweak is not in the software repos? – Olivier Lalonde Dec 07 '10 at 22:31
  • They never got around to adding it/haven't been accepted. You can add their repo to your list either right after starting tweak or through the terminal/repo list. – dkuntz2 Dec 07 '10 at 23:40
  • You can also add Ubuntu Tweak as a PPA with sudo add-apt-repository ppa:tualatrix/ppa then sudo apt-get update then sudo apt-get install ubuntu-tweak – lofidevops Oct 14 '11 at 07:15
  • 1
    note that since ~13.04 Ubuntu Tweak no longer provides this option – lofidevops Jun 15 '14 at 11:32
7

The following command change the hostname on the fly but to make it permanent, you have to edit /etc/hostname:

echo 'new_hostname' > /proc/sys/kernel/hostname

Open a new terminal session and you'll see it right away.

With systemd in place, the proper way to do it is

hostnamectl set-hostname "new_name"
wjandrea
  • 14,236
  • 4
  • 48
  • 98
Terry Wang
  • 9,775
4

If you want a GUI assisted process install Ubuntu-Tweak. Among other uses of this app is the ability to change computer name through tab "Computer-Details" -> "Hostname"

13east
  • 1,945
4

SystemSettings -> Details -> Overwiev (default opened in U16.04) - Device Name.

but additionally you must change name in /etc/hosts. Ubuntu BUG()?

2

Open a terminal and

sudo sed -i '1s/.*/desired-name/g' /etc/hostname

# you need restart to effect with...

sudo shutdown -r 0
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:12
1

For some reason all answers are about changing the hostname. However the goal of just showing the first part of the hostname can be achieved in other way.

You just need to find a place in your .bashrc file where the PS1 is set and replace \H with \h.

man bash - prompting

trolzen
  • 121