Set a Static Ip Address in Ubuntu from Command Line
By marc • May 1st, 2008 • Category: UbuntuA common task that you will need to do on a Ubuntu/Debian server is set a static IP address. By default the ip address is set through DHCP, so you will need to change that.
First edit the /etc/network/interfaces file:
-
-
sudo nano /etc/network/interfaces
Change your file so that it looks similar to the following, but replacing the specific ip addresses with your own.
-
-
# This file describes the network interfaces available on your system
-
# and how to activate them. For more information, see interfaces(5).
-
-
# The loopback network interface
-
auto lo
-
iface lo inet loopback
-
-
# The primary network interface
-
auto eth0
-
iface eth0 inet static
-
address 192.168.1.102
-
netmask 255.255.255.0
-
network 192.168.1.0
-
broadcast 192.168.1.255
-
gateway 192.168.1.1
The next thing you will need to do is set the appropriate nameservers in /etc/resolv.conf.
-
-
sudo nano /etc/resolv.conf
Your file should look something like this:
-
-
nameserver xx.xx.xx.xx1
-
nameserver xx.xx.xx.xx2
-
-
# use verizon's publicly available nameservers as a backup
-
nameserver 4.2.2.2
Now, you just need to restart your networking service to kick everything into gear.
-
-
sudo /etc/init.d/networking restart
Finally, ping a website to make sure that your name resolution works correctly.
-
-
ping yahoo.com
If you get a response, everything should be all set! That was easy, wasn’t it?
Now, if you find that your /etc/resolv.conf file is getting overwritten when you reboot, you will need to make changes to one last configuration file.
-
-
sudo nano /etc/dhcp3/dhclient.conf
You will need to add the same nameservers that you set in /etc/resolv.conf on the line that starts with “prepend domain-name-servers” and remove the comment.
The line in the /etc/dhcp3/dhclient.conf will look like this:
-
-
prepend domain-name-servers xx.xx.xx.xx1, xx.xx.xx.xx2;