2

For example.

[root@ip-10-0-7-225 ~]#

I edited /etc/hosts but it didn't work.

1
  • 5
    It's your prompt, look up PS1. Also don't edit /etc/hosts when you don't know what you're doing.
    – 123
    Commented Feb 15, 2016 at 10:22

2 Answers 2

3

As noted, the problem is the hostname. CentOS (unlike Debian) may get that information from

/etc/sysconfig/network

e.g., a line like

HOSTNAME=myhostname.mydomainname

Further reading: 28.1.21. /etc/sysconfig/network

0
2

The part that comes after @ in PS1 by default is your hostname.

My default PS1 in Armbian (Debian) is

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Where \h is shorthand for the hostname.

To change the name, either you:

  • change your prompt via PS1;
  • request a change to your DNS to the network team in an organisation setting;
  • or you ignore the hostname that DHCP client gives you (for instance with a server directly connected to an ISP).

To change it immediately to a new name (lost at logout time):

PS1='\u@new_name'

Where \u is shorthand for the logged in user.

PS1 is fine to change it on the go, I do not recommend changing the default PS1 permanently as the proper method.

As such, to change it in runtime via hostname you have to do:

sudo hostname new_name.new_domain

And then logout and login.

You also have to edit /etc/hostname to change the default name server when rebooting.

sudo vi /etc/hostname

Keep however in mind that disconnecting and connecting the network interface, and then logout and login, will revert to the old name if you have not configured your dhcp client to ignore the name you receive via DHCP.

For more info, please peruse this stack exchange question:

Why is bash's prompt variable called PS1?

4
  • I do not recommend changing the default PS1 permanently as the proper method. why ?
    – 123
    Commented Feb 15, 2016 at 11:04
  • You may change it, if you keep in mind it is a kludge. At the end of the day, it is so much better to change the machine name, as you may have problems with scripts and other functionalities in the long run. Commented Feb 15, 2016 at 11:07
  • @RuiFRibeiro How would changing $PS1 give someone problems with scripts? Commented Feb 15, 2016 at 16:01
  • @TeresaeJunior My fault, was not clear. None, but it will make any problem with the hostname less visible, especially for novice users. Commented Feb 15, 2016 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .