395

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

name@server-name.belkin ~>

How can I remove ".belkin"?

Jorge Castro
  • 73,717
Wolfy
  • 8,050

14 Answers14

462

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,185
213

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,954
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,323
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.

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.

14

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
  • 84,513
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,610
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
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,504
Terry Wang
  • 10,185
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
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
  • 5