Define a static IP on Linux, and assign a Default Gateway

Written by
Date: 2009-01-03 10:36:30 00:00

There are some networks where DHCP is not yet implemented, and when you enter into those networks asking for connection, the Admin of the network will give you these data.

IP, Netmask, Gateway and sometimes DNS, I see myself in this situations lots of times as I travel with my Laptop, and in the offices where I have to work I found this types of networks with no DHCP server installed.

Well, what will you do with those info?, I like the command line, so I will show you how to assign this IP through the Linux command line console.

We will not make it fixed I mean it will be erased when the PC is restarted, so it will be there only for the current session, the command is.

sudo ifconfig ethX ADDRESS netmask NETMASK

Lets say you have been given this info.

IP: Netmask: Gateway: DNS:

And your NIC is named eth0 on you Linux, to find the NICs available, issue the command.

sudo ifconfig

With no options, and all the available interfaces will be on the screen.

Well first assign the IP and the netmask

sudo ifconfig eth0 netmask up

Now add the default Gateway

sudo route add default gw

The last step is to assign a DNS server, if you travel a lot like me, I hardly recommend to have your own DNS on your Laptop, read here how to do it: Configure pdnsd as local DNS server

Otherwise, you should edit /etc/resolv.conf, and there enter this line, following our example.


Updated: 2010-04-06

You may want to read Add second IP to Linux for more info, and also how to make changes permanent.