Make sure you are logged in as root and move to /etc/sysconfig and open the network file in vi.
cd /etc/sysconfig vi network
Look for the HOSTNAME line and replace it with the new hostname you want to use. In this example I want to replace localhost with redhat9.
When you are done, save your changes and exit vi. Next we will edit the /etc/hosts file and set the new hostname.
In hosts, edit the line that has the old hostname and replace it with your new one.
Save your changes and exit vi. The changes to /etc/hosts and /etc/sysconfig/network are necessary to make your changes persistent (in the event of an unscheduled reboot).
Now we use the hostname program to change the hostname that is currently set.
And run it again without any parameters to see if the hostname changed.
Finally we will restart the network to apply the changes we made to /etc/hosts and /etc/sysconfig/network.
service network restart