Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Linux ›
How to assign a static IP address in Linux

How to assign a static IP address in Linux

October 17, 2011 Linux Jesin A 2 Comments

linux category thumbnail

This article explains assigning a static IP to your Linux machine through the command line. If you’re assigning a public IP address, you should’ve purchased it from your ISP. Assigning the IP address in Linux requires you to edit the network configuration file. The network interface files are located at different places according the Linux OS variant. This article will cover both Red Hat and Debian variants. You need to logged in as the root user to edit these files, or you should have sudo permissions.

Red Hat variants

Some of the Red Hat variant Linux OSes are CentOS, Fedora, SUSE Linux etc. If you’re using YUM to install packages then you have a Red Hat based OS. Use the VI editor and edit the following file

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

A machine having multiple Ethernet cards will have files named ifcfg-eth1, ifcfg-eth2 and so on. Add or edit the following lines

ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1

By default BOOTPROTO is set to dhcp which should be changed, setting ONBOOT to yes will activate this network adapter when Linux boots and the care should be taken when entering the NETMASK value. Because unlike Windows OSes which automatically assign Subnet masks based on the class of the IP address manual assigning is required in Linux. Even though the interface will work if classless NETMASK is entered, the system might have problems in communicating with other network devices as they have a classful subnet mask. Refer to the Wikipedia article on Classful network to check in which class your IP address lies.

Debian variants

If you use dpkg to install packages then you’re using a Debian based Linux OS. Using VI editor open the following file.

vi /etc/network/interfaces

If you’re using DHCP (which is the default) the following is shown

auto eth0
iface eth0 inet dhcp

To assign an IP address statically delete those lines and enter the following

iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1

Here again you should keep an eye when entering the netmask.

Assigning the DNS IP addresses

Assigning DNS addresses is similar in both types of Linux OSes. Edit the following file using the VI editor

vi /etc/resolv.conf

The nameserver keyword is used to mention the DNS service IP address. If more than one DNS IP is to be specified use multiple entries

nameserver 8.8.8.8
nameserver 8.8.4.4

Restarting the interface and verifying the settings

For the changes to apply the interface has to be shutdown and brought up

ifdown eth0
ifup eth0
ifconfig

The ifconfig command should display the IP address and netmask assigned by you. Ping different IP addresses to check connectivity. To check the DNS ping a hostname or a domain name

ping google.com

You should get a reply.

Related posts:

linux category thumbnailSetup a DHCP server on Linux linux category thumbnailHow to configure a Linux PPTP VPN client linux category thumbnailMount an ISO file in Linux linux category thumbnailSetup Linux DNS Server for Windows Active Directory linux category thumbnailConfigure Apache Web Server Load Balancing

Tags: linux, linux system files

Comments

  1. Stefan says

    October 18, 2011 at 3:54 am

    I have a related question.

    I have installed Ubuntu on several computers on a LAN. Somehow all of these computers have been assigned 192.168.x.x ip addresses, and these addresses do not collide.

    I did *not* assign these static private subset ip addresses myself.

    So I wonder how these are these static ip addresses in the private subnet determined? Does each Ubuntu computer simply scan, say by pinging, for others computers in the subnet. Here I am speculating!

    That is, if a computer doesn’t get a response from 192.168.1.13, perhaps the computer assumes that there is no computer with that assigned ip adress???? That approach doesn’t seem rock solid as two computers could look for 192.168.1.13 at the same time and both claim that static ip address as their own when they don’t get a response.

    Of course, I am just speculating! I actually do not know how Ubuntu assigns these static ip addresses in the private subnet, but I’d like to understand it better.

    Any ideas how Ubuntu computes and assigns these static ip addresses in the private subnet?

    Reply
    • A.Jesin says

      October 18, 2011 at 1:05 pm

      Hello Stefan, how did you check the IP address ? Using the ifconfig command ?
      Check out the following file
      /etc/network/interfaces
      If you find a line the line “iface eth0 inet dhcp” it means your computer has been configured to request IP address from a DHCP server. DHCP is a protocol which automatically assigns IP addresses to computers in the same physical segment.

      I’ll explain briefly how DHCP works

      1. When you switch on your Ubuntu PC it sends a broadcast packet containing its MAC address requesting an IP address.
      2. This packet is received by all computers in the same physical segment including the DHCP server.
      3. The DHCP server responds by sending the IP address configuration to the computer which sent that broadcast packet.

      This process is called DORA (Discover Offer Request Acknowledge)

      Most broadband modems/routers are configured as DHCP servers, so if you have one of these in your network setup it would’ve assigned IP addresses for all your computers.

      You can find out your DHCP server’s IP address by looking into the file
      /var/lib/dhcp/dhclient.leases

      To learn more about DHCP read the Wikipedia article Dynamic Host Configuration Protocol

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.