/etc/hosts

Hi, I want to modify my host so that when I'm sitting at the console it is does not look like:

nsajeff@li61-78:/etc$

I want this instead:

$user@ottawahosts.com

This is my /etc/hosts file

127.0.0.1 localhost

97.107.131.78 ottawahosts.com

Thanks for your help.

13 Replies

Ummm…ya, I posted that. I'm just not sure WHAT to edit.

echo "ottawahosts.com" > /etc/hostname hostname -F /etc/hostname

Then logout and login again.

EDIT: changed cat to echo, obviously …

@saman007uk:

cat "ottawahosts.com" > /etc/hostname
hostname -F /etc/hostname

Then logout and login again.

Thanks saman007uk. I'll try that.

nsajeff@li61-78:/etc$ sudo cat "ottawahosts.com" > /etc/hostname

-bash: /etc/hostname: Permission denied

nsajeff@li61-78:/etc$ sudo cat "ottawahosts.com" > /etc/hosts

-bash: /etc/hosts: Permission denied

nsajeff@li61-78:/etc$ hostname -F /etc/hosts

hostname: the specified hostname is invalid

hostname: you must be root to change the host name

nsajeff@li61-78:/etc$ sudo hostname -F /etc/hosts

[sudo] password for nsajeff:

hostname: the specified hostname is invalid

nsajeff@li61-78:/etc$

Kind of obvious, but you don't seem to have enough permissions! Try

sudo su -

to become root, and then try again.

Besides, you're doing it wrong. It's /etc/hostname, not /etc/hosts. They're two different files, and the latter has nothing to do with the procedure you're supposed to be following.

On a different note, I don't think it's a good idea to use your main domain as your hostname. It sometimes causes weird issues with Apache which can be hard to troubleshoot. Make up a subdomain or something. (The console will usually only display the first part of it, though. Otherwise your command prompt becomes too long!)

@hybinet:

Kind of obvious, but you don't seem to have enough permissions! Try

sudo su -

to become root, and then try again.

Besides, you're doing it wrong. It's /etc/hostname, not /etc/hosts. They're two different files, and the latter has nothing to do with the procedure you're supposed to be following.

On a different note, I don't think it's a good idea to use your main domain as your hostname. It sometimes causes weird issues with Apache which can be hard to troubleshoot. Make up a subdomain or something. (The console will usually only display the first part of it, though. Otherwise your command prompt becomes too long!)

Clearly it isn't that obvious, I have full root privileges through sudo. I ran those commands as root also and had the same problem. /etc/hostname does not exist on my system.

Thanks for clarifying that its a bad idea though. I'll stick with the linode hostname for now.

@nsajeff:

@hybinet:

Kind of obvious, but you don't seem to have enough permissions! Try

sudo su -

to become root, and then try again.

Besides, you're doing it wrong. It's /etc/hostname, not /etc/hosts. They're two different files, and the latter has nothing to do with the procedure you're supposed to be following.

On a different note, I don't think it's a good idea to use your main domain as your hostname. It sometimes causes weird issues with Apache which can be hard to troubleshoot. Make up a subdomain or something. (The console will usually only display the first part of it, though. Otherwise your command prompt becomes too long!)

Clearly it isn't that obvious, I have full root privileges through sudo. I ran those commands as root also and had the same problem. /etc/hostname does not exist on my system.

No, sudo doesn't always work exactly the same as actually being the root user, this is one of those cases. Do a sudo su -
and run the command again

edit: Sudo is only sudo-ing the echo command, not the > command.

@nsajeff:

nsajeff@li61-78:/etc$ sudo cat "ottawahosts.com" > /etc/hostname

-bash: /etc/hostname: Permission denied

nsajeff@li61-78:/etc$ sudo cat "ottawahosts.com" > /etc/hosts

-bash: /etc/hosts: Permission denied

nsajeff@li61-78:/etc$ hostname -F /etc/hosts

hostname: the specified hostname is invalid

hostname: you must be root to change the host name

nsajeff@li61-78:/etc$ sudo hostname -F /etc/hosts

[sudo] password for nsajeff:

hostname: the specified hostname is invalid

nsajeff@li61-78:/etc$
First, the original advice was in error - you wanted to use echo, not cat:

echo "domain.com" >/etc/hostname

Second, as mentioned already, the sudo command you used didn't affect the redirection so you were trying to write the file as your regular user.

Third, apparently the hostname file lives somewhere else for your system. On gentoo it is /etc/conf.d/hostname - maybe you need to look around to find it on yours.

And lastly, changing config files by blindly overwriting an existing file is a recipie for disaster. IMHO you should fire up a text editor of your choice to make these kinds of changes. As a good example, the gentoo /etc/conf.d/hostname file I mentioned would be quite broken if you replaced it as you were trying to do.

On Debian/Ubuntu, here's how to do it.

Assuming that you want sub.domain.com:

sudo vi /etc/hosts

Or if you prefer, you can use other editors such as nano.

Put the following line somewhere in the file:

your.ip.address sub.domain.com sub

Note that there are three entries and two spaces. It is important that the fullly qualified domain name comes first, then just the subdomain part of it. (I said above that /etc/hosts has little to do with what you're trying to do, but if you use a subdomain it's useful to take this step.)

sudo vi /etc/hostname

Again, replace with your favorite editor.

Replace the entire contents of the file (if any) with:

sub

That is, just the subdomain part of your hostname. Some tutorials tell you to put the entire domain name there, but in my opinion that's wrong. Others may disagree, though.

Finally, run the following command:

sudo /etc/init.d/hostname.sh start

Now log out and log back in. You should see the new hostname.

Thank you hybinet. That is exactly what I was looking for. It also fixed the pesky problem of telling me it couldn't resolve my host when using sudo.

I followed all of the instructions (didn't do that before, if you saw my earlier post), and was able to resolve the problem.

I was even able to get it to work by just specifying the machine name in the "localhost" portion of my /etc/hosts. First line is loopback, second line is external IP.

127.0.0.1 localhost customized_name

44.33.22.11 external.domain.name

doing that, plus putting customized_name in /etc/hostname worked.

Result: command prompt = username@customized_name >

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct