CentOS / Ubuntu how to change the IP address
FOR BEGINNERS ONLY.
In this small tutorial I’ll explain how to configure the network interface on CentOS and Ubuntu. This includes IP Address, Gateway, Broadcast, etc.
Changing the networking settings of a CentOS or Ubuntu server is an easy task, no matter if you know to work with a linux distribution or not.
So let’s get started.
CentOS
The first method I’ll use is the manual one, using an editor like nano.
The file you are going to edit is: /etc/sysconfig/network-scripts/ifcfg-eth0
nano /etc/sysconfig/network-scripts/ifcfg-eth0
And here is how you should edit this file:
DEVICE=eth0 BOOTPROTO=static BROADCAST=192.168.1.255 HWADDR=00:0E:0C:C1:E2:00 IPADDR=192.168.1.2 NETMASK=255.255.255.0 NETWORK=192.168.1.0 ONBOOT=yes
Now let me explain a bit some of this options.
DEVICE – the device you are configuring
BOOTPROTO=static – the ip configuration is set to static, no DHCP
IPADDR – the ip address assigned to this interface
BROADCAST, NETWORK and NETMASK – you can calculate this options using an online ipcalc
ONBOOT=yes – all this options will apply at system startup
HWADDR – this is the mac address of the interface
Now let’s edit /etc/sysconfig/network
nano /etc/sysconfig/network
Edit this file in order to look like this:
NETWORKING_IPV6=no NETWORKING=yes GATEWAY=192.168.1.1 HOSTNAME=ServerHostName
I guess there’s no need to discuss this options.
Don’t forget to replace 192.168.1.x with your gateway, do this for the previous file also.
If you need to change the dns ip addresses also:
nano /etc/resolv.conf
The file looks like this:
nameserver 192.168.1.1 nameserver 192.168.2.1
Don’t forget that all 192.168.x.x ip’s are used only as an example, replace them with your own.
As a final step you should restart the server or simply restart networking like so:
/etc/init.d/network restart
Now after you learned this the hard way, you can try the easy graphical tool, run:
system-config-network
Follow the steps and you are done.
Ubuntu
Ubuntu is a bit different then CentOS, that applies for configuration file location and format.
Let’s start by editing /etc/network/interfaces
nano /etc/network/interfaces
And here is an example of this file configured with an 192.168.x.x ip address:
# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 192.168.1.2 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.254 gateway 192.168.1.1 dns-nameservers 192.168.1.1
The description I gave for CentOS applies here also, with a few exceptions:
address – this is the ip address
dns-nameservers – this is the dns that usually is configured into /etc/resolv.conf
And in the end you need to reboot the server or restart networking like this:
/etc/init.d/networking restart
March 20, 2011
Thanks you so much. This is what i’m searching for.