Howto: Ubuntu Linux convert DHCP network configuration to static IP configuration

My friend wanted to know how to change or convert DHCP network configuration to static configuration. After initial installation, he wanted to change network settings. Further, his system is w/o GUI system aka X Windows. Here is quick way to accomplish the same:

Your main network configuration file is /etc/network/interfaces

Desired new sample settings:
=> Host IP address 192.168.1.100
=> Netmask: 255.255.255.0
=> Network ID: 192.168.1.0
=> Broadcast IP: 192.168.1.255
=> Gateway/Router IP: 192.168.1.254
=> DNS Server: 192.168.1.254

Open network configuration file
$ sudo vi /etc/network/interfacesOR$ sudo nano /etc/network/interfaces

Find and remove dhcp entry:
iface eth0 inet dhcp

Append new network settings:

iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254

Save and close the file. Restart the network:
$ sudo /etc/init.d/networking restart

Task: Define new DNS servers

Open /etc/resolv.conf file
$ sudo vi /etc/resolv.conf

You need to remove old DNS server assigned by DHCP server:
search myisp.com
nameserver 192.168.1.254
nameserver 202.54.1.20
nameserver 202.54.1.30

Save and close the file.

Task: Test DNS server

$ host cyberciti.biz

Network command line cheat sheet

You can also use commands to change settings. Please note that these settings are temporary and not the permanent. Use above method to make network changes permanent or GUI tool as described below.

Task: Display network interface information

$ ifconfig

Task: Take down network interface eth0 / take a network interface down

$ sudo ifconfig eth0 downOR $ sudo ifdown eth0

Task: Bring a network interface eth0 up

$ sudo ifconfig eth0 upOR$ sudo ifup eth0

Task: Change IP address and netmask from command line

Activate network interface eth0 with a new IP (192.168.1.50) / netmask:
$ sudo ifconfig eth0 192.168.1.50 netmask 255.255.255.0 up

Task: Display the routing table

$ /sbin/route OR$ /sbin/route -n
Output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
localnet        *               255.255.255.0   U     0      0        0 ra0
172.16.114.0    *               255.255.255.0   U     0      0        0 eth0
172.16.236.0    *               255.255.255.0   U     0      0        0 eth1
default         192.168.1.254   0.0.0.0         UG    0      0        0 ra0

Task: Add a new gateway

$ sudo route add default gw 172.16.236.0

Task: Display current active Internet connections (servers and established connection)

$ netstat -nat

Task: Display open ports

$ sudo netstat -tulpOR$ sudo netstat -tulpn

Task: Display network interfaces stats (RX/TX etc)

$ netstat -i

Task: Display output for active/established connections only

$ netstat -e
$ netstat -te
$ netstat -tue

Where,

  • -t : TCP connections
  • -u : UDP connections
  • -e : Established

Task: Test network connectivity

Send ICMP ECHO_REQUEST to network hosts, routers, servers etc with ping command. This verifies connectivity exists between local host and remote network system:
$ ping router
$ ping 192.168.1.254
$ ping cyberciti.biz

See simple Linux system monitoring with ping command and scripts for more information.

Task: Use GUI (Graphical Configuration) network Tool

If you are new, use GUI configuration tool, type the following command at terminal:
$ network-admin &

Above command is Ubuntu’s GUI for configuring network connections tool.

Final tip – Learn how find out more information about commands

A man page is your best friend when you wanted to learn more about particular command or syntax. For example, read detailed information about ifconfig and netstat command:
$ man ifconfig
$ man netstat

Just get a short help with all command options by appending –help option to each command:
$ netstat --help

Find out what command is used for particular task by searching the short descriptions and manual page names for the keyword:
$ man -k 'delete directory'
$ apropos -s 1 remove

Display short descriptions of a command:
$ whatis rm
$ whatis netstat

Linux offers an excellent collection of utilities, which can be use to finding the files and executables, remember you cannot memorize all the commands and files 😉

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
84 comments… add one
  • intosomethin Dec 28, 2014 @ 11:50

    In order to save beginners from using Vi(m) or Nano, I’d recommend to append the DHCP settings via “echo”:

    $ sudo echo “auto eth0niface eth0 inet dhcp” >> /etc/network/interfaces
    $ sudo ifdown eth0
    $sudo ifup eth0

    does the job w/o having to go into editors.

  • nava Oct 21, 2014 @ 4:25

    suppose i have two virtual network eth0 and eth0:1 as shown below

    iface eth0 inet dhcp

    auto eth0:1
    iface eth0:1 inet static
    address *.*.*.*
    netmask *.*.*.*
    gateway *.*.*.*

    can i set priority between two interfaces? If yes how?
    the PC should always go for eth0 when connected to network and eth0:1 if connected to cross over cable.

    • intosomethin Dec 28, 2014 @ 12:05

      @nava: “eth0:1” seems to be more like a bridged interface than a separate NIC. If you didn’t just make it up as an example. So 2 Interfaces demand two NIC’s (Network Interface Card) – either a physical or a virtual one. Why would you use a Cross-Overcable in the first place?
      Install SSH and Download a small software piece called “PuTTy” on your Winclient and you can just copy the stuff via scp or sftp. Here a quick guide:
      # Installing SSH
      $ sudo apt-get install ssh
      $ sudo service ssh restart # just in case, it didn’t start
      $ sudo netstat -tlpn # this should show whether ssh is listening on Port 22 (the line should be there)
      then simply transfer data by:
      $ ssh @
      The first time you do this, you gotta confirm and type “yes” once asked for it.
      Then you can browse remotely with cd, ls, pwd and locally with the same commands just with an “l” for “local” in front of them: lcd, lls, lpwd. “get” and “put” downloads and uploads stuff from/to the current local and remote directories you are in. Quite the ssesion with “exit” or CTRL+D …hope that helps.

    • intosomethin Dec 28, 2014 @ 12:09

      the ssh@ line got caught by the html, it should be:

      $ ssh username@remotehostname # (or better Ip)

    • intosomethin Dec 28, 2014 @ 12:13

      PLEASE IGNORE OR DELETE comments 81 & 82
      @nava: “eth0:1″ seems to be more like a bridged interface than a separate NIC. If you didn’t just make it up as an example. So 2 Interfaces demand two NIC’s (Network Interface Card) – either a physical or a virtual one. Why would you use a Cross-Overcable in the first place?
      Install SSH and Download a small software piece called “PuTTy” on your Winclient and you can just copy the stuff via scp or sftp. Here a quick guide:
      # Installing SSH
      $ sudo apt-get install ssh
      $ sudo service ssh restart # just in case, it didn’t start
      $ sudo netstat -tlpn # this should show whether ssh is listening on Port 22 (the line should be there)
      then simply transfer data by:
      $ sftp username@remotehostname # (or better Ip)
      The first time you do this, you gotta confirm and type “yes” once asked for it.
      Then you can browse remotely with cd, ls, pwd and locally with the same commands just with an “l” for “local” in front of them: lcd, lls, lpwd. “get” and “put” downloads and uploads stuff from/to the current local and remote directories you are in. Quite the ssesion with “exit” or CTRL+D …hope that helps.

  • abhishek Oct 10, 2013 @ 20:49

    Can you suggest a similar method for wireless interface on ubuntu I want to set static ip on ubuntu I want to setup static ip on my wireless interface

    I followed a guide here

    
    /etc/network/interfaces files has following entries
    
    auto lo
    iface lo inet loopback
     auto lo
       iface lo inet loopback
    
    auto wlan0
    iface wlan0 inet static
          address 192.168.1.7
          netmask 255.255.255.0
          broadcast 192.168.1.255
          gateway 192.168.1.1
          wpa-ssid bsnl2
          wpa-psk  e61b988cf433a0275de2a0ea569417479adb1b49698f432152cc1c5a901369ce
    

    but upon reboot I am totally disconnected from my wireless network ,
    do I need to do some thing else for giving the wireless interface a static ip

  • abhishek Oct 10, 2013 @ 16:57

    I want to setup dhcp and static IP I have 2 lan card, Ubuntu 12.04 and it is a laptop the wireless interface I want to give a static IP and lan to have a dynamic ip how should I go about making changes in /etc/network/interfaces file
    the wlan0 is wireless interface and eth0 is lan card that is what ifconfig reports

    till now I tried

    auto lo
    iface lo inet static
    address 192.168.1.7
    netmask 255.255.255.0
    gateway 192.168.1.1
    network 192.168.1.0
    broadcast 192.168.1.255
    dns-nameservers 127.0.0.1
    dns-search home.lan
    dns-domain home.lan
    and
    auto lo
    iface lo inet loopback

    auto wlan0
    iface wlan0 inet static
    address 192.168.1.7
    netmask 255.255.255.0
    gateway 192.168.1.1
    network 192.168.1.0
    broadcast 192.168.1.255
    dns-nameservers 127.0.0.1
    dns-search home.lan
    dns-domain home.lan

    these both seem to have broken one or the other service.

  • udin May 17, 2013 @ 15:56

    Thank you… Your tutorial very useful anytime.. 🙂
    Keep posting..

  • adhitya christiawan nurprasetyo Apr 22, 2013 @ 3:53

    dont forget to add this line below to automatically fire up the device.
    auto eth0

  • Coldnorth Feb 5, 2013 @ 16:42

    Excellent guide, thanks for putting it together. I was having trouble keeping my IP’s static and had to use WICD to set them after every reboot. 5 minutes after reading this everything works perfectly.

  • sachin Sep 14, 2012 @ 13:13

    in my pc there is no dhcp … there is something like this ..

    iface lo inet loopback

    so now wat i have to do to change my IP?

  • me Feb 11, 2011 @ 19:13

    tnx..

  • Peter Draganov Nov 18, 2010 @ 19:20

    Will you add to this howto the need to add following line to /etc/network/interfaces as suggested by 2 people back in 2007 and 2009?

    auto eth0

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.