On Debian/Ubuntu, here's how to do it.
Assuming that you want sub.domain.com:
Code:
sudo vi /etc/hosts
Or if you prefer, you can use other editors such as nano.
Put the following line somewhere in the file:
Code:
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.)
Code:
sudo vi /etc/hostname
Again, replace with your favorite editor.
Replace the entire contents of the file (if any) with:
Code:
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:
Code:
sudo /etc/init.d/hostname.sh start
Now log out and log back in. You should see the new hostname.