How to set up static IP address and why
Notice! I do not talk about other ways of seting up network like with NetworkManager. Any device mentioned in
/etc/network/interfaces are not touched by NM
To be able to communicate through an IP network (IPv4 and IPv6) the computer must know what IP address it has. So therefore
address directive is needed to tell the computer that.
When the computer then wants to talk to another computer, it uses that other computers IP address to check if that computer is in the same network, LAN. If so, the computer can communicate direct with the other computer.
So how do the computer knows when it is communicating with a computer on the same LAN? By using the networks
netmask, where the net part of the address is set to ones and host part is set to zeros. So by doing a bitwise AND operation between each bit in a IP address and the netmask we will get the network address of the IP address, where host part is zero. So if we do this on the computers IP address and the other computers IP address we get each network address.. If they are equal, it means that the computers are in the same network and can talk directly to each other.
If the network addresses are not equal they are on different LAN and cannot communicate directly to each other. Then the computer needs to use a special computer that is connected to other LANs. That computer is a router (that could also have a firewall and NAT). So when the computer want to talk to other computers outside the LAN, it need to know the address of that computer which is set with the
gateway directive. If another interface has already set a
gateway value, you do not and should not set another gateway directive for this interface. The
gateway directive sets the default route for the computer, so you need only one for IPv4 and only one for IPv6 on each machine.
This network address can be manually set with the
Sometime the computer want to communicate with all computers in the LAN, and the it uses broadcast address. This address is listen to by all computers in the same LAN. This is basicly the same as network address, except the host part is not all zeros and instead all ones. This broadcast address is set by the directive
broadcast in the interface.
network address is only needed to be calculated once and is usually calculated correctly from that
netmask directives. Same with the
broadcast address. So you do not need to set them. In fact if you set one or both to the wrong values, you could lose connection to internet and other computers in your LAN. So unless you have some strange values on them, let the computer calculate them for you.
So a minimal static settings, or stanza, in
/etc/network/interfaces could look like this for the device
eth1 in a private network:
iface eth1 static inet
How name resolving works and how it is tied to DNS.
Domain names are used to convert between easy for humans to read and remember domain names and the computers not so easy to remember IP address, mentioned above. This is called Name Resolving.
This is usually controlled by the file
/etc/nsswitc.conf and the line which start with
hosts:. If you ask the computer to connect to the computer
my.example.com, it will look into this file and try to solve the IP address from the name
my.example.com. This file does not actually answers the question "which IP do my.example.com have", it just tell the computer where it could find the answer.
Usually it tries the
/etc/hosts file first for static local names, then avahi
mDNS for dynamic local names and then a DNS domain name resovler to get a name from internet.
If some of these are slow in answering your questions, it might look like the computer get stuck for a while. So if you get that, check name resolving first.
So adding static addresses you can just add it to your
/etc/hosts file. By the way, if you have a static address, you probably should change the IP address there for your machine to your IP address and not the default
127.0.1.1 (which is in the localnet network where localhost are,
127.0.0.1). That only works ok for clients and not for servers.
The dynamic addresses you get from Linux machines with the
avahi package and from Apple machines (and MS Windows with iTunes?). That is handled by "magic" and you will not need to fix that.
The DNS is used to reach internet and you need to tell the computer where those DNS servers you want to use are, what IP address to use and what your default DNS domain are.
This is done in the file
/etc/resolv.conf and can be statically set up. This does not work that well in our not so static world, so usually you have a package called
resolvconf installed. This let you set up these settings in the
So if we assumes we want to add one of Googles DNS servers,
22.214.171.124, and your ISP's DNS server,
192.0.2.1, and your domain
my.example.org domain as default, you just edit the
/etc/network/interfaces file and add these two lines in the stanza for the static device.
dns-nameservers 126.96.36.199 192.0.2.10
You may also notice that the DNS resolver will only use a maximum of three DNS servers. Please look this up in the
resolv.conf. As usually, you can use the command
man resolv.conf and
man resolvconf for more information.
Also notice that I use domain example.com and example.org and IP network 192.0.2.0/24 for the ISP example DNS server. These are explicit defined to be used in examples. See http://example.com/ or http://tools.ietf.org/html/rfc2606 and rfc5735
How do you then check that it works?
You can do that in many ways, but I usually use
getent hosts my.test.com
to test the whole setup for name resolution. If I just want to check if DNS works, I use one of these commands:
But remember that those only test DNS through the settings in the
/etc/resolv.conf file, and not the
/etc/nsswitch.conf part. It might be what you want, or not.