Configure a static IP on Ubuntu Server 14.04

20151229113855At home I use Ubuntu Server 14.04 and have never had to set  static IP as I would configure a static IP/mac address mapping in my router’s DHCP settings so it would always get the same address . That was until last week when I had to look up the exact steps on Google. Needless to say,  the process is straight-forward, here are the steps to configure a static IP on Ubuntu Server 14.04:

The Steps

1. The first thing to mention is that the interface config file in Ubuntu looks a little weird if you are used to working with only RHEL/CentOS. By default Ubuntu Server enables DHCP on a network interface. To configure the interface you have to edit the /etc/network/interfaces file. Below I am opening it with vi editor:

admin@web01:~$ sudo vi /etc/network/interfaces file

You will see something similar to the below:

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 dhcp

2. As you can see from the above eth0 is set to use DHCP and not a static IP. I want to set a static IP with the following info:

IP address10.10.10.50
Subnet Mask255.255.255.0
Default Gateway10.10.10.1
DNS Server 110.10.10.1
DNS Server 28.8.8.8

To achieve this, change iface eth0 inet dhcp to iface eth0 inet static. Then add the network parameters per the below:

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 10.10.10.50
netmask 255.255.255.0
gateway 10.10.10.1
dns-nameservers 10.10.10.1 8.8.8.8

Save the changes and exit.

3. The next step is to update the /etc/hosts file by opening it with vi or any other editor of your choice:

admin@web01:~$ sudo vi /etc/hosts

You should see the lines below:

127.0.0.1   localhost
127.0.1.1   web01

Add a new line under the existing lines containing the static IP and relevant hostname(s):

127.0.0.1   localhost
127.0.1.1   web01
10.10.10.50     web01

Save the changes and exit the file.

4. To commit the changes made in the steps above you can either reboot the server or restart the network services. Restart the network services by running:

admin@web01:~$ sudo ifdown eth0 & sudo ifup eth0

Please note that in previous version of Ubuntu server you could run service networking restart to restart services. This no longer works in Ubuntu Server 14.04. You need to shut down the interface and then bring it back up per step 4 above.

After restarting the network services the operating system and all applications will recognise and use the new IP.

Please also note that restarting the network services will force an update to the /etc/resolve.conf file. It will automatically add the DNS servers we configured in step 2 above to the file. We can confirm this by reading the file:

admin@web01:~$ cat /etc/resolv.conf

The output is:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.10.10.1
nameserver 8.8.8.8

That’s it! Your Ubuntu Server is now running using a static IP.

References:
Linux Basics – Set A Static IP On Ubuntu
Official Ubuntu Documentation > Network Configuration > Interfaces

Set environment variable for a specific user in RHEL 6/CentOS 6 using csh shell

I was asked by one of the SAP Basis engineers in my project team to create an environment variable for a specific user (psoadm). He wanted me to create a variable called SECUDIR and set it to /home/psoadm/sec, per the below:

SECUDIR = /home/psoadm/sec

So the first thing to do was find out the type of shell was being used. To do this I logged into the server as root and then ran:

su - psoadm

This allows me to switch user from root to the psoadm user and assume psoadm’s own home directory and environment variables – it’s the equivalent of psoadm logging into a new session.

To confirm the shell being used run the following:

ps -p $$

As you can see from the output below the shell was csh (C Shell):

 PID  TTY          TIME CMD
 7872 pts/0    00:00:00 csh

Now that we know that our shell is csh the next step is to add the SECUDIR variable to the following files: $HOME/.cshrc and $HOME/.login. The $HOME variable relates to psoadm’s home directory and is the equivalent of ~. So to edit the $HOME/.cshrc file run the following:

nano ~/.cshrc

then scroll to the bottom of the file and add the following:

# Set SECUDIR for psoadm user
setenv SECUDIR /home/psoadm/sec

Hit CTRL + x to exit and you will see the following:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?
Y Yes
N No  ^C Cancel

enter Y to save and then it will ask to confirm the file to write these changes to, per the below:

File Name to Write: /home/psoadm/.cshrc

Hit Enter to confirm.

Now follow the exact same steps to update the $HOME/.login file. In order for the SECUDIR variable to be available you, either logout of the current shell and log back in or run the source command against the files, per the below:

source ~/.cshrc
source ~/.login

Now enter echo $SECUDIR at the shell prompt like so:

echo $SECUDIR

And you will now see that the response correctly returns the value of SECUDIR as:

/home/psoadm/sec

If your shell is bash, check the links directly below for advise on how to sent environment variables for the bash shell:

How to permanently export a variable in Linux
How do I set a user environment variable permanently not session?

That’s all folks! I hope this was useful.

References:
C shell
What shell am I using?
Set environment variable in Unix
How to permanently export a variable in Linux
How do I set a user environment variable permanently not session?