miercuri, 17 august 2011

Configure a Static IP in Ubuntu

Ubuntu installs with a default setting for DHCP on the primary network adapter. Setting a static IP address is quick and easy in Ubuntu, just not intuitive.

1.Understand network configurations in Ubuntu

Ubuntu/Debian, like most Linux distributions and unlike Windows, is configured mostly through text files.

In Windows, the TCP/IP properties dialog of a network connection shows you the basic IP address configuration including DNS settings. They can be viewed or changed in that dialog, given that you have the appropriate permissions to do so.

In Ubuntu, the IP configuration and DNS settings are configured in three different files: /etc/hosts, /etc/resolv.conf, and /etc/network/interfaces.

The /etc/hosts file is what DNS replaced. In the 70's, if you had several computers networked, you had to have a configured hosts file on each computer describing the location of all the other computers on the network! DNS was simply a way for all the computers to reference a centrally maintained list. It is still wise to have a hosts file configured with at least the parameters of the machine itself, for backup and for applications that need to resolve the local hostname when DNS isn't available.

Open /etc/hosts for editing. (I use vim, see my How-To on installing vim)

:~$ sudo vim /etc/hosts

If your DHCP server hands out the domain name with the IP assignment, the default configuration will look something like this:

127.0.0.1 localhost
127.0.1.1 myhost.mydomain.com myhost

# The following lines are desirable for IPv6 capable hosts
[A lot of IPv6 stuff I'm not going to get into]

2.Edit /etc/hosts

NOTE: I use vim with syntax highlighting, 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.

3.Edit /etc/resolv.conf

The /etc/resolv.conf file in Unix/Linux performs the same function as the 'Preferred DNS server:' fields in Windows network configuration.

In an Active Directory domain the DNS suffix is automatically configured. You have to tell Linux where it lives. Edit the /etc/resolv.conf file:

:~$ sudo vim /etc/resolv.conf

Depending on how your DHCP is configured, there may be nothing to do. A default resolv.conf file looks something like:

search mydomain.com
nameserver 192.168.100.1

if you are configuring your Ubuntu machine to be on a different domain that the one it recieved the DHCP configuration from, change the search line. This should probably be the same domain that was configured in /etc/hosts:

# search mydomain.com
search myotherdomain.com

Change the nameserver IP address if you want to use a different DNS server.

Save and close the file.

4.Inspect /etc/network/interfaces

The /etc/network/interfaces file is where the nitty gritty configuration takes place. Open the file for editing:

:~$ sudo vim /etc/network/interfaces

If you only have one network card in your Ubuntu machine, the file should have the following lines:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

5.Edit /etc/network/interfaces

Now comes the really difficult part. Find and comment the line:

iface eth0 inet dhcp

Now insert these new lines below it, using your own IP, subnet mask, and default gateway, so that it now looks like:

# iface eth0 inet dhcp
iface eth0 inet static
address 192.168.100.100
netmask 255.255.255.0
gateway 192.168.100.254

That's it. Done. You basically type "Here's the address", "Here's the subnet mask", "Here's the gateway"....ok, go.

Routing in Linux can be squirelly, so I like to make sure that the routing entry for the default gateway is there every time the interface comes up. At the end of my /etc/network/interfaces file will be a line like:

up route add default gw 192.168.100.254

If you change your IP to static and then cannot apt-get or otherwise access the Internet, you might try adding that additional line (with the address of YOUR router/gateway) to the end of your interfaces file.

NOTE: We aren't touching the loopback interface lines, they stay as-is during the configuration.

6. Make Ubuntu recognize the new configuration

Ubuntu is running happily along with the configuration it read the last time it booted or the interfaces were brought up. We need to make it read the new one. You can either reboot or cycle the network interfaces at the command line.

:~$ sudo /etc/init.d/networking restart

If you get any funny output try rebooting.

Go to it!

Conclusion

Ubuntu makes a great server platform, but the default DHCP configuration for a server is generally regarded as a Bad Thing!

Niciun comentariu:

Trimiteți un comentariu