How-To Geek
Change Ubuntu Server from DHCP to a Static IP Address
If the Ubuntu Server installer has set your server to use DHCP, you will want to change it to a static IP address so that people can actually use it.
Changing this setting without a GUI will require some text editing, but that’s classic linux, right?
Let’s open up the /etc/network/interfaces file. I’m going to use vi, but you can choose a different editor
sudo vi /etc/network/interfaces
For the primary interface, which is usually eth0, you will see these lines:
auto eth0
iface eth0 inet dhcp
As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should be added below it. Obviously you’d customize this to your network.
auto eth0
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.1
Now we’ll need to add in the DNS settings by editing the resolv.conf file:
sudo vi /etc/resolv.conf
On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)
You need to also remove the dhcp client for this to stick (thanks to Peter for noticing). You might need to remove dhcp-client3 instead.
sudo apt-get remove dhcp-client
Now we’ll just need to restart the networking components:
sudo /etc/init.d/networking restart
Ping www.google.com. If you get a response, name resolution is working(unless of course if google is in your hosts file).
Really pretty simple.
Updated Thanks to Nickname007 in the comments for noting that I forgot the DNS entries in the guide.
Got Feedback? Join the discussion at discuss.howtogeek.com
Comments (82)
Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on Google+ if you'd like.
- Published 12/13/06




And what about configuring your router to force that server to recieve that IP address through dhclient?
Why does no body write a complete guide for this? Follow these steps and all sorts of things will break such as web-surfing, updates, mail – basically anything that required name resolution. This is half of what you need to do. Here is the rest.
1. With a dynamic ip address, at the console as root type: ifconfig /all. Write down the IP address for your name server.
2. Follow this guys steps.
3. As root, type: vim /etc/resolv.conf. On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. Restart networking. Ping http://www.google.com. If you get a responce, name resolution is working(unless of course if google is in your hosts file).
Good luck people, enjoy.
@NIckname007
Thanks for noticing the missing information! I’ve updated the guide with your comments.
When I follow above directions network will not restart…error ifdown: couldn’t read interfaces file “/etc/network/interfaces” /etc/network/interfaces:2: misplaced option. Same for ifup. Even if change back to original configuration, get same error.
Changes are:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
Should have mentioned further….cannot run ifconfig – same error. Did verify that /etc/resolv.conf does already point to correct nameservers….
Seems to be some other conf file that gets dynamically changed when making the above changes and needs to be corrected, but don’t know where to begin. Any help is greatly appreciated.
HI Charles,
I have just met and solved this problem myself. It had me tearing my hair out and, as usual, it was something small I had overlooked. The error message is telling you that there is a problem with the syntax of the interfaces file. In my case there was a comment mark (#) missing from the second line of the file, right at the top where it gives a bit of info about what the file does – I was concentrating so hard on the meat of the file I didn’t think to look up there for quite a while. Hope this helps.
I have been searching for this exact information for hours! I wish google had psychic powers (coming in 2009)
Worked immediately – excellent.
I found what the problem was for me.. my editor for the TOP commented line of the ‘interfaces’ file had for some reason gone onto the next line -
e.g.
# This file describes the network interfaces available on your
system and how to activate them. For more information, see interfaces(5).
IT SHOULD HAVE BEEN -
# This file describes the network interfaces available on your system and how to activate them. For more information, see interfaces(5).
I JUST DELETED THE CARRAGE RETURN AND IT WORKED A TREAT!
Just a little thing…
You have a typo… it should be “nameserver xxx.xxx.xxx.xxx.” (no space between name and server).
Otherwise spot on.
Thanks!
When i type in
sudo vi /etc/network/interfaces
all i get is a blank screen with the following character
~
~
~
~
~
Can anyone help? Im a noob when it comes to this.
Any Help would be appreciated.
when i type in
sudo vi /etc/resolv.conf
i get the same screen mentioned in the post above by Jose. should i just type in my nameserver xxx.xxx whatnot?
I experienced that it is necessary to remove the dhcp client package in order to prevent overwriting of the resolv.conf file. (dhcp-client or dhcp3-client). Use Aptitude to remove these packages if they are installed. Then the modification to resolv.conf will last.
Good luck.
Thanks, Good article
Works great…. Until my ubuntu server decides to go out and ask my router for a dhcp lease anyways. I still see that “dhclient3″ is still running and listening to eth0. how do I fix that? It’s really annoying when every morning my ip address changes from x.x.x.50 to x.x.x.102 and all of my port forwardings break and I can’t remotely access my box until I go in and do an /etc/init.d/networking restart. please help!
this article was really helpful for a newbie like me.. thanks a lot the responser
This How-To is very helpful. I did need the follow-up comments from Peter Zandbergen and Sheldon Cooper to keep the server from reverting to DHCP. To remove the dhcp client and kill the process:
.
$ sudo apt-get remove dhcp3-client
…
$ ps aux | grep dhcp
dhcp 4294 0.0 0.0 14096 680 ? S
yes, for some reason pico wrapped the top comment line and then saved it…so you get the misplaced option error really quite annoying, I’m glad someone else figured it out and saved me several hours of annoyance!
Thanks home dog.
Thanks! Reading a clear walkthrough makes this stuff a lot easier. Keep writing, i’ll keep reading.
Good stuff.
On my machine the package is called dhcp3-client, not dhcp-client3.
Cheers, worked a treat, had a problem with machine dropping ip address with dhcp, this soon sorted it out :D
Yeh about the router. Wouldn’t you still have to config it a static ip too. I have cable and its set to dynamic. If so is there any way I can find my dns server name (ie xxxx.xxxx.xxxx.xxxx) without contacting my isp provider.
HI all……….I tried the as given below but error comes every time same…
After vi/etc/network/interfaces I made changes like
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
After I restart the network
sudo /etc/init.d/networking restart
I got the following error msg
root@excel:~# sudo /etc/init.d/networking restart
* Reconfiguring network interfaces… /etc/network/interfaces:1: misplaced option
ifdown: couldn’t read interfaces file “/etc/network/interfaces”
/etc/network/interfaces:1: misplaced option
ifup: couldn’t read interfaces file “/etc/network/interfaces”
[fail]
I tried this
ls -lah /etc/network/interfaces
Iget
root@excel:~# ls -lah /etc/network/interfaces
-rwxrw-rw- 1 root root 202 2008-07-30 12:05 /etc/network/interfaces
Then I tried this one
sudo chown root:root /etc/network/interfaces
After I add “ # ”
# The loopback network interface
auto lo
iface lo inet loopback
#
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
I get same error any one can help me….
thanks,
ricky
Thanks for the article. It’s guys like you that make Linux bearable.
This worked straight away. Thanks.
I had the misplaced option file problem as well. Turns out, at the very bottom of my interfaces file (like 20 returns down) it had repeated the options for eth0, which must have been confusing the OS. so make sure there’s nothing hidden at the bottom of your file.
I have a proxy server with two interfaces. The Internet interface gets its IP via DHCP from a DSL modem on a 10.0.0.0 network, the other is static 192.168.1.0 LAN network which is setup as this article.
Therefore I cannot get rid of the DHCP client.
Thanks very much. Your tips helped me significantly.
Thanks alott !
Good explanation ! and Works perfectly !
In Ubuntu server 9 (and perhaps earlier versions) the dhcp client package is called dhcp3-client and not dhcp-client3 as it is stated in the step-by-step guide.
Thanks for the guide though.
Please correct the correction… it is dhcp3-client, not dhcp-client3.
There is no space between name & server in *name server xxx.xxx.xxx.xxx”.
Thanks for the guide!
If you still have to use dhcp, then the client can be started with an option so as not to overwrite /etc/resolv.conf (and maybe some others). Errr, I wish I could be more specific but I just uninstalled dhcp :-). Thanks for the guide.
After making changes to file, how do you save your changes?
absolutely exelent!!!!!
ur info is exactly useful
thanks a lot and more power!!!!!!!
i really love it!!!!!!!!
thanks you very much.
ifconfig -a (and not ifconfig /all)
u wrote:
(You can do ifconfig /all to find out what they are)
in fact u mixed Linux and Windows
Under Linux:
ifconfig -a
Under Windows:
ipconfig /all
but the tutorial was pretty good
thanks.
I wouldn’t recommend removing dhcp-client
What if one day u want to get an IP from ur DHCP, what if ur PC has more than one nic and one needs a DHCP IP ?
On a newly installed ubuntu ur primary eth is in dhcp, then u set it static by updating /etc/network/interface (u can edit resolv.conf also for dns) following given instructions on this page, then try a :
ps ax|grep dhc
u probably will see a running dhclient3 process running
or a dhclient if u runned it by hand
kill them respectively with :
killall dhclient3
killall dhclient
then a “ps ax|grep dhc” should show no dhcp client running.
Now ur server is set to ur static IP and wont request dhcp IPs anymore, even on reboot.
but u still can request for a dhcp IP by hand.
It is not necessary to uninstall the dhcp-client. Just ensure you use the spaces before the lines
address, … in the interfaces file as shown above, then it works.
I found that if you are having problems connecting after all of this adding:
/sbin/route add -net 0.0.0.0 gw 1.1.1.1 eth0
where you have to replace 1.1.1.1 with the internal IP address of your router (gateway).
fixes your connection issues.
1st works very good,
Tip for VirtualBox users: change the network to “Bridged adapter” in advance
On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)
you mean on the line ‘nameserver xxx.xxx.xxx.xxx’ etc…
I don’t believe “ifconfig /all” is a valid linux command. (However, “ifconfig -a” is valid)
Niether command will list your nameservers, however.
(i before e, except after c)
minor error – the dhcp 3 package is named “dhcp3-client” (not dhcp-client3) Good guide :)
Thank you very much.
I have not used Linux very much and have forgotten everything I used to know about UNIX.
(I was a wizard, but fell into bad ways when my company started using windows.)
In any case this page did solve my problems.
The only thing I would add (just for fun).
is a dictionary for us poor windows peons.
As in (we must not assume here)
Address means your assigned IP
netmask means the same as netmask in windows
gateway means the same as gateway in windows.
Newer Linux can get by without broadcast or network entries (and as I couldn’t figure them out I left them out.
(explain these.)
nameserver in /etc/resolv.conf hopefully means DNS (yeah I know, dumb) server address.
(the actual problem I had is that I put two lines out of order in interfaces, never underestimate the power of stupid.)
Thank you again but I am just asking for a little ‘dumbing down’ for us windows people.
This is one of the best post that I have ever read. You have provided a great piece of information. I will definitely share it with my other friends. Keep up the good work, I would to stay in contact with your posts.
The Linux shell command ifconfig will not show you your nameserver, as stated in this post, nor is there a valid /all switch to ifconfig. To find your currently set nameserver, execute the following:
$ cat /etc/resolv.conf | grep nameserver
this really helped! Thanks.
Hi
I am using my computer as a dedicated ubuntu 10.4 lucid server. And I am already in a big network.
I would like the server ip stable .
Could you please map the ip address with the ifconfig -a results
I am confused with address and network entries.
I have mapped like this please some one tell me this is the correct mapping.
I am confused with the address and network entries.
address –> ifconfig -> HWaddr
netmask –>ifconfig ->Mask
network –> ifconfig -> inet addr
broadcast-> ifconfig -> Bcast
gateway –>route -> Gateway
nameserver -> dig ubunto.com -> SERVER
nameserver ->/etc/resolv.conf
I have used 3 linux commands ,ifconfig, route,dig to find these entries.
I mean HWaddr entry from the result of ifconfig is the address entry (!) Is that right ,
if not please some one help me.
Hello, thank you for this article! I have followed these directions and I can ping various websites successfully. I have one problem though… I now can not ssh to my server. Is there anything I need to change in openssh?
Thx! Very useful info. Helped me alot!
I am following these instruction to Connect Net in ubuntu but I am unable to save resolv.conf , /etc/network/interfaces or any config file to make changes
After changes it only allow to save as file(wid new name) not save or append changes in original file plz help how to resolve it
Shaina, you need root privileges to write those files. Start the editor with “sudo”, like “sudo gedit /etc/network/interfaces”
Works perfectly! thnx alot :-)
Worked a treat.
Thanks heaps.
worked great! thought id change from dhcp as woul be changing IPs on all dev machines every time gets new IP lol from router restart n such! thanks any help with doing this with wireless belkin dongle?
thanks for this article and I am able to see my machine in network !
Cheers
-Brijesh
Hi,
First time on this site, please ignore illiteracy….
from ifconfig, i am getting
eth0 Link encap:Ethernet HWaddr 00:0a:e6:ef:a6:af
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
My “interfaces” file has:
auto eth0
iface eth0 inet static
address 192.168.1.3
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
I am using a single computer. What IP address I need to include in my resolv.conf as nameserver ?
*****I tried:
nameserver 192.168.1.3 (since this is the ip address of my pc)
root@desk:/etc/network# ping http://www.google.com
ping: unknown host http://www.google.com
******I tried:
nameserver 192.168.1.1 (gateway of my router)
root@desk:/etc/network# ping http://www.google.com
works fine, resolving address
******I tried:
nameserver 192.168.1.3 (since this is the ip address of my pc)
nameserver 208.67.222.222(Open DNS)
nameserver 208.67.220.220(Open DNS)
root@desk:/etc/network# ping http://www.google.com
works fine, resolving address
******I tried:
nameserver 192.168.1.3 (since this is the ip address of my pc)
nameserver “my ISP Primary DNS”
nameserver “my ISP Secondary DNS”
root@desk:/etc/network# ping http://www.google.com
works fine, resolving address
Which way would be the right way to configure it?
And which way would be the right way to configure it if I want to run a web server using the same machine?
Please please please help me
Hi,
First time on this site. Please ignore my illiteracy
I am trying to set a static ip on my pc to configure DNS. I have only 1 dedicated PC, running ubuntu 9.10.
My interfaces file includes
auto eth0
iface eth0 inet static
address 192.168.1.3
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
and on resolv.conf, I have tried:
nameserver 192.168.1.3
ping http://www.google.com -> unknown host, can’t brows internet
Tried:
nameserver 192.168.1.1
ping http://www.google.com -> works fine
Tried:
nameserver 192.168.1.3
nameserver Open DNS
ping http://www.google.com -> works fine
Tried:
nameserver 192.168.1.3
nameserver My ISP DNS
ping http://www.google.com -> works fine
Which one would be correct if I want this computer to be my nameserver in the network?
Which one would be correct if I want to run a web server from this computer using a static ip provided my ISP?
any help would be much appreciated.
PS: Please take it easy if sounds a real stupid :(
Thank you
name server should be nameserver … without this it will not resolve dns properly… Danke
hey guys, am having problems with editing my etc/network/interface. even when i use sudo vi/etc/network/interface.
am running ubuntu server on vmplayer and my pc is running on windows 7. i want to configure my virtual server with an ip so that it can communicate with my pc, but am failing,, need some help. i cant edit anything in etc/…….
thanx
Russell,
By the looks of it your router has your ISPs nameservers entered so for either scenario you can point your namerserver at your router:
nameserver 192.168.1.1
remove the nameserver 192.168.1.3 unless you are running a nameserver on your machine.
This did the trick for me. Thanks for such a clear write-up!
Thanks for this ! I tried the gui, but it seems you have to know your MAC add first. Your method worked fine, thanks for this. !
One step that I did not see mentioned here… if you want your new static IP address to resolve to your server name:
Edit /etc/hosts
NOTE: I use vim, so instructions are referenced from that perspective.
Comment the second line in the default hosts file, which should begin with 127.0.1.1. Comment by adding a # to the beginning of the line. The commented line (in vim) should turn a light blue.
Now insert a new line below it including your selected static IP address:
[example]
192.168.100.100 myhost.mydomain.com myhost
Save and close the file.
Thanx for this. It’s a simple tutorial.
Wow, I’ve set up hundreds of Linux and Unix boxes as well as Windows, MacOS, etc. I’ve never had so much trouble. The GUI networking tool won’t let you change to a static IP – well, it does, but it won’t let you apply the change – INSANE. And that’s after figuring out how to get the root account working. Next up is NIS and automounts. Simple with most Linux’s but I’m sure Ubuntu thinks it knows what I want so I’ll be spending hours configuring something that should have taken 1/2 hr. Perhaps it’s a great OS, but so far Ubuntu is awful
Thanks for the post, it was exactly what I was looking for.
Listean up, u dont need the network and broadcasst option. Just fit the config like me:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
after that just simply restart networking service:
/etc/init.d/networking restart
;)
gratz
Excuse me for speaking in another language, but I had to say que este articulo es EXCELENTE! La primera pagina que encontre sobre el tema y automaticamente arreglo la dificultad que tenia. Gracias!!!
Hi, Really thanks for your articles.
BTW, please advice,
currently i have an issue with my network always error after 2 minutes.
it’s always need to do re “sudo dhclient and network restarting”.
best regards,
hadi
Works like a charm :-) Thank you!
This is a great article, thank you. You’re missing a few small pieces of info that would be very helpful for a noob like me. Once I open the text file in the terminal, how do I save the darn thing??? I’ve been googling for the past 20 min and can’t find a direct answer. Could you please include this in your walkthrough?
Can someone help with the following please :
Set up LAMP sever on ubuntu 11.04 . Localhost gives home page with no problems
Altered the server /etc/network/interfaces file to show
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
Performed sudo /etc/init.d/networking restart
Configured my Sky router so that port forwarding HTTP port 80 sends to ip address 192.168.0.5
Then checked to see if could connect from internet using a tool on http://www.dyndns.com/support/tools/openport.html and input the ip address showing on my ADSL port ip address. Results give “An attempted connection to 94.0.47.254:80 was refused. This typically indicates that there are no services available on that port, but that it is NOT being blocked by a firewall or your ISP”
What have i done wrong ?
Another added detail: I believe dhcp-client is a *virtual* package on recent Ubuntus, and thus cannot be removed as shown. Instead, you must determine the actual package underlying the virtual package, which can be done, among other ways, by:
dpkg -l *dhcp* # This lists all packages, virtual or actual, that include dhcp in their name
Once you figure out which physical package you’ve got, then you can purge it with apt-get
For the curious, try here for more info on virtual packages: http://www.debian.org/doc/FAQ/ch-pkg_basics.en.html
Thanks a lot, this article helped me out a ton :)!
@ Rob:
- check your gateway was correctly set: route -n should return something like this
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eth0
- check your /etc/hosts.allow and /etc/hosts.deny do accept connections from outside on apache service
- check your apache config file (/etc/apache2/sites-enabled/000-default) so that your DocumentRoot has “allow from all” in the the right order (Order allow,deny)
- is your firewall really off ? double check with iptables -nL; should be empty:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
- if your firewall does not drop any input packet, you should be able to see them; you can trace this way
tcpdump -n -i eth0 port 80 and host 192.168.0.5
- you can also trace your apache log: tail -f /var/log/apache2/access.log
Hi, hey can someone help with setting up wlan on ubuntu, been reading up on the how to’s available out there but still can’t connect.
# ifup wlan0 ; says
ifup: interface wlan0 already configured
I’m sure I’m mssing a minor detail somewhere,been trying to get this going 4 days now, but lol I’m a linux noob. Thanks in advance any help would be great, peace !
Run script echo ‘–**–**–’; chmod +x /etc/fw/test.fw; sudo -S /etc/fw/test.fw && ( test -f /var/run/shutdown.pid && sudo -S shutdown -c; echo ‘Policy activated’ )
Copying /home/adura/Documents/test.fw -> 192.168.50.131:/etc/fw/test.fw
Running command ‘/usr/bin/fwbuilder -Y scp -o ConnectTimeout=30 -p 2222 -q /home/adura/Documents/test.fw test@192.168.50.131:/etc/fw/test.fw’
ssh: connect to host 192.168.50.131 port 22: Connection refused
lost connection
SSH session terminated, exit status: 1
Firewall policy installation failed
help me to settle that problem…..need help…need help
apt-get remove isc-dhcp-client
(in ubuntu 11.10)