Search this blog

Tuesday, October 7, 2008

how to set static IP on a CentOS machine

A friend asked me for help, setting up a CentOs machine for the installation of an Oracle Database. I emailed him a short/dirty version of the the following, and thought that this might help other inexperienced linux/centos users; truly experienced linux folk won't need this, naturally.

I'm assuming that you are working with the KDE desktop, but the same should apply to the Gnome desktop (I'm talking about the software term, not the magical term;  Gnome as a desktop environment has some advantages, but I belong to the KDE fans).

In the upper menu navigate to system/preferences/network connections - 

Choose system eth0 [assuming this is your network card]
Edit
Choose the ipv4 tab
Choose method: manual 
Press the "add" button and enter the following - 

  • static ip (a free and suitable one to your network)
  • subnet mask [wiki article]
  • gateway 
Move on to the dns servers field, and enter  the dns server (in many modern routers, the router serves both as a gateway and local dns server). 

Press "apply". 

Now you need to restart the network service so that the configuration will become effective. 

In the upper menu, navigate to 
system\ administration\services
Stop the Networkmanager service. 
Start it. 
(you could also use the Restart button)

And there you have - your system is reconfigured with a new, static, IP address. 

Several remarks
1. this is a short howto regarding IPv4. IPv6 setup in this context ain't that different but not within the scope of this post (one would assume that under IPv6 all addresses would be static, but old habits die hard...)

2. if you wish to learn what is happening under the hood, read the following chapter in the CentOS deployment guide, and take a look at the following files - 
  • /etc/resolv.conf
  • /etc/sysconfig/network
  • and at the files under the directory - 
  • /etc/sysconfig/network-scripts/
    bearing names in the pattern of 'ifcfg-<interface-name>'
you can also use the man command to learn a little about the ifconfig and the hostname  commands.
(this is a minimalistic list, assuming you have little time, and most probably skipped the "rtfm" bit).

3. why static ip ? 
1. for db servers, as well as many other servers who have a rich network life, this is the norm for many good considerations, which might justify another post, one day, regarding this norm, workarounds, and the questions of best practices.  
2. it eases work in local network environments where the DNS setup is shaky/nonexistent (this is a suprisingly common situation in small/messy companies, and is a growing reality in many houses which find themselves, unaware, with several network-aware computers or gadgets). I suspect that it might be a  widespread reality with in the foreseeable future, as home networks grow with smart phones and gadgets. As those machines are expected to be of a dual-network identitity - internet/phone-company related and local-network/home-router related, most chances are that for the average joe, the home-network will be a total mess (and a heaven for hackers, but this, again, is a subject for another post, another day, maybe).

4.  how to see IP address in centos, not from command prompt ? 
well, this issue is  an old one (see this 2005 discussion, one of my favorites, regarding seeing the IP address on Ubuntu). There are workarounds,  but as far as I can tell, this issue is still unanswered in the vanilla, out-of-the-box CentOS interface and most just use /sbin/ifconfig. Before you mock linux users for their ifconfig addiction, oh Windows-user, ask yourself - honestly - what is your day-2-day practice of seeing your machine's IP address in Windows (I'm willing to bet you CMD, and then ipconfig). 

[last reviewed/updated: 17/04/2013]