nixCraft Poll

Topics

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

Posted by Vivek Gite [Last updated: July 27, 2007]

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,

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 ;)

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. George Says:

    Thank you nixcraft!

    You are not only teaching excellent networking specifics, but, most importantly, also making me understand how to do this on my own. Not too many guys out there are able to do this as you have done.

  2. Rafael Says:

    Thank you, this made it work for me. I had the problem of needing to use static ip address, for various other lan requirements.

    Although, one minor glitch creeped up .. after I had edited the /etc/network file. My gateway is set to 192.168.1.1, rather than … 1.254, and so that is what I had set in the network file. After editing this file I still had no internet connection, but I could ping the gateway. But I was able to connect after modifying the setting in the gui: System–>Admin–>Network–>eth0–>gateway address. It had a default setting of 192.168.1.254, changing it to my actual gateway fixed it.

  3. Grady Lambert Says:

    A very good tutorial. Thank you.
    I have been struggling with networking for sometime. You have helped to clear up a lot of confusion.

  4. Shelly Says:

    Hi and thanks for this article.
    Why do you change the 255 to 254 please?
    Do I need to do that if setting a server box up within a network with other multiple OS’s?

  5. steppa Says:

    NetworkManager is fine for varying wlan configurations. This guide gives a sleek way to configure a static network. Afterwards you can

    apt-get –purge remove network-manager knetworkmanager network-manager-gnome dhcdbd

    to get rid of unnecessarily runnig daemons.

  6. S. Beatty Consulting » Blog Archive » An Ubuntu Server 7.04 Installation in VMware Says:

    [...] Howto: Ubuntu Linux convert DHCP network configuration to static IP configuration [...]

  7. Anonymous Says:

    auto eth0 should precede anything added to “interfaces” and be on its own line.

  8. Grayheart Says:

    one small problem… if you edit /etc/resolv.conf directly, the next time you ifdown or ifup an interface or reboot for that matter your settings are lost. In Ubuntu the resolv.conf is generated each time you up/down an interface. So unless you removed the resolvconf package which is generally installed by default, you need to add proper dns- entries to the /etc/network/interfaces file to make changes permanent. eg:
    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
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 192.168.1.254
    dns-search myisp.com

  9. G7 Says:

    About dns-nameservers: if you have >1 dns servers they must be in 1 line separated by spaces. But not in 2-3 lines (it will hangup network startup script at all) !!!

  10. Raj Says:

    I think only dhclient updates resolv.conf, look like ubuntu has some additional scripting at /etc/rc.d/*. On a side note, you can write protect resolv.conf
    sudo chattr +i /etc/resolv.conf
    man chattr

  11. pankaj Says:

    How can i encrypted the file from VI. And how can i connect the internet in my home and I want also connect the localarea network. And what is joe editor explane.

  12. pankaj Says:

    How can i configer the server connection on my one more system If i would like the 1st system is an Administrator…..

  13. polarizer Says:

    This was very useful to me cause i wasn’t able to find a way to accomplish that via dpkg-reconfigure or debconf.

    thx for sharing

    the polarizer

  14. danny0085 Says:

    a litle tutorial to how to configure DHCP

    dhcp

  15. Linux convert static IP configuration to DHCP network configuration « Vunknown’s Weblog Says:

    [...] อ้างอิง - Howto: Ubuntu Linux convert DHCP network configuration to static IP configuration [...]

  16. Tauxz Says:

    how to i restart network

    i used:
    sudo /etc/init.d/networking restart

    /etc/init.d/networking restart

    bu still nothing happening,
    pls can you help me.

  17. Tinapa Faletiute Says:

    How to save after changing something in the terminal window for ubuntu 7.04?

  18. Rim Says:

    This was incredibly helpful. I am a very experienced Windows admin but I still feel like a newbie when it comes to Linux. Not only did I complete the task I needed with your help, you also made me realize the difference between editing config files and just using ifconfig.

    Bookmarking your site.

  19. Ray Malone Says:

    I have just started with Linux with ubuntu server 8.94. Never used Linux before.
    Just wanted to say thank you. Wanted to use Fixed Ip for the server and found your site and great instructions easily.

    Just wanted to say thanks.

  20. dido Says:

    Excellent. Thanks.

  21. Randy Says:

    Oh wow. Thank you so much for this tip!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.