HostOnNet Blog

How to Change Hostname and IP on a CentOS Server

UPDATE 2017-03-21 CentOS 7 come with hostnamectl command, that allow you to easily set server hostname with out manually editing any of these files.

RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME="plain.domainname.com"
GATEWAY="111.222.333.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"

TCP/IP Network Configuration Files

This configures Linux so that it knows which DNS server will be resolving domain names into IP addresses

vi /etc/resolv.conf
search name-of-domain.com - Name of your domain or ISP's domain if using their name server
nameserver XXX.XXX.XXX.XXX - IP address of primary name server
nameserver XXX.XXX.XXX.XXX - IP address of secondary name server

Locally resolve node names to IP addresses

vi /etc/hosts
::1             localhost6.localdomain6 localhost6
# Do not remove the following line, or various programs
# that require network functionality will fail.
111.222.333.444            yourname.server.com yourname
127.0.0.1               localhost 

Configuration settings for your first ethernet port

vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
BROADCAST=111.222.333.255
IPADDR=111.222.333.444
NETMASK=255.255.255.0
NETWORK=111.222.333.0
ONBOOT=yes

And to finalize , you need to restart your network

service network restart

Posted in CentOS, Linux, Web Server