CentOS 6: Setting a Static IP address

CentOS 6: Setting a Static IP address

This is a quick tutorial on how to set a static IP address using the terminal within CentOS 6.

Set the Static Address

1. Check which interface(s) you want to set as static.

eth0 Link encap:Ethernet HWaddr 4C:72:B9:D1:D3:26
inet addr:192.168.1.58 Bcast:5.135.164.255 Mask:255.255.255.0
inet6 addr: fe80::4e72:b9ff:fed1:d326/64 Scope:Link
inet6 addr: 2001:41d0:8:e77b::1/64 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:640586231 errors:0 dropped:0 overruns:0 frame:0
TX packets:1476012131 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:98117934406 (91.3 GiB) TX bytes:953623859810 (888.1 GiB)
Interrupt:20 Memory:fe500000-fe520000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:219027567 errors:0 dropped:0 overruns:0 frame:0
TX packets:219027567 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1033429521495 (962.4 GiB) TX bytes:1033429521495 (962.4 GiB)

2. Next edit the config file for the required interface.

vi /etc/sysconfig/network-scripts/ifcfg-eth0

3. Edit the config file to look similar to the following.

Change the BOOTPROTO from “dhcp” to “static”. Also adding IPADDR, NETMASK, BROADCAST and NETWORK variables and make sure ONBOOT is set to yes.

UUID="e88f1292-1f87-4576-97aa-bb8b2be34bd3"
NM_CONTROLLED="yes"
HWADDR="D8:D3:85:AE:DD:4C"
BOOTPROTO="static"
DEVICE="eth0"
ONBOOT="yes"
IPADDR=192.168.1.2
NETMASK=255.255.255.0
BROADCAST=192.168.1.255
NETWORK=192.168.1.0
GATEWAY=192.168.1.1

Set the Default Gateway

Set the default gateway to ensure that CentOS can access the internet.

vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=server
GATEWAY=192.168.1.1

Apply the Settings

Restart the network service to apply the settings.

service network restart

Configure DNS

Make sure the correct DNS servers are configured to allow CentOS to resolve domains.

vi /etc/resolv.conf
nameserver 192.168.1.1
nameserver 8.8.8.8