CentOS - Set A Static IP
This tutorial explains the configuration of a static IP address (IPv4 and IPv6), the hostname and nameservers on CentOS. To be compatible with Desktop and Server Systems, we do the configuration on the shell.
1 Preliminary Note
Suppose you are working in a data center & your boss puts a minimal CentOS server setup & you need to configure it in the running environment. Yes it is little painstaking, but not very tough task. In my case I have a dumb CentOS server which was installed by someone in his networking environment & I want to make it functional in my static IP environment. Suppose I have a vacant IP 192.168.0.100 & I will implement it in my environment. My IP details are as follows:
IPv4
- IP address: 192.168.0.100
- Subnet: 255.255.255.0
- Gateway: 192.168.0.1
IPv-6
- IP address: 2001:db8::c0ca:1eaf
- Netmask 64
- Gateway 2001:db8::1ead:ed:beef
DNS resolving nameservers
- Nameserver 1: 8.8.8.8
- Nameserver 2: 8.8.4.4
2 Implementation
I will do a configuration file editing with the editor like vi. But you may use any other shell editor like nano or joe instead. The file name for the first network card (eth0) is /etc/sysconfig/network-scripts/ifcfg-eth0
I will first make backup of my original file as /etc/sysconfig/network-scripts/ifcfg-eth0.bak & then proceed for the changes in /etc/sysconfig/network-scripts/ifcfg-eth0
mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
vi /etc/sysconfig/network-scripts/ifcfg-eth0
I will change the file like this:#My IP description # IPv-4 DEVICE="eth0" NM_CONTROLLED="yes" ONBOOT=yes HWADDR=20:89:84:c8:12:8a TYPE=Ethernet BOOTPROTO=static NAME="System eth0" UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 IPADDR= 2001:db8::c0ca:1eaf NETMASK=255.255.255.0
For IPv6 You just have to add the entries below:
vi /etc/sysconfig/network
[...] NETWORKING_IPV6=yes
vi /etc/sysconfig/network-scripts/ifcfg-eth0
[...] #IPv-6 IPV6INIT=yes IPV6ADDR=2001:db8::c0ca:1eaf IPV6_DEFAULTGW=2001:db8::1ead:ed:beef
3 DNS configuration
DNS can be added in the file /etc/resolv.confvi /etc/resolv.conf
[...] nameserver 8.8.8.8 nameserver 8.8.4.4
You can add 2 or more namserver lines. Your system will try the other ones in case that the first nameserver is unreachable.
4 Hostname
In my case the hostname is server1.example.com to set the hostname, I will edit the /etc/hosts file:
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 192.168.0.100 server1.example.com server1 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
and the resolv.conf file:
vi /etc/resolv.conf
NETWORKING=yes HOSTNAME=server1.example.com GATEWAY=192.168.0.1 [...]
and reboot the server.
reboot
Check your hostname with the command below. The new hostname will be applied after reboot only
hostname
5 Advanced networking and virtual network interfaces
I am using CentOS Linux and I would like to create alias for eth0:0 so that I can have multiple IP address. I will implemented by creating a file as follows:
vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
#IP Aliasing DEVICE="eth0:0" BOOTPROTO="static" HWADDR=20:89:84:c8:12:8a NM_CONTROLLED="no" ONBOOT="yes" TYPE="Ethernet" IPADDR=192.168.0.108 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=8.8.8.8 DNS1=8.8.4.4
Here I have done the IP aliasing for the IP 192.168.0.108, it could vary as per your requirement.
6 Services
After any change in the networking files you need to restart the network services as follows:/etc/init.d/network restart
After the service restart you can check the changes as:
ifconfig
The output will confirm the changes done statically. It will be almost similar like this:
root@server1:~# ifconfig
eth0 Link encap:Ethernet HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.100 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: 2001:db8::c0ca:1eaf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:200197 errors:0 dropped:67 overruns:0 frame:0
TX packets:69689 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:64103748 (64.1 MB) TX bytes:14106191 (14.1 MB)
eth0:0 Link encap:Ethernet HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0 Link encap:Ethernet HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.100 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: 2001:db8::c0ca:1eaf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:200197 errors:0 dropped:67 overruns:0 frame:0
TX packets:69689 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:64103748 (64.1 MB) TX bytes:14106191 (14.1 MB)
eth0:0 Link encap:Ethernet HWaddr 20:89:84:c8:12:8a
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:65536 Metric:1
RX packets:10365 errors:0 dropped:0 overruns:0 frame:0
TX packets:10365 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:875114 (875.1 KB) TX bytes:875114 (875.1 KB)
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:10365 errors:0 dropped:0 overruns:0 frame:0
TX packets:10365 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:875114 (875.1 KB) TX bytes:875114 (875.1 KB)
No comments