How to setup a host file, correctly.. in Linux.

What is a host file?

A host file or /etc/hosts in Linux, it’s a locally administrated text file which is used by the operating system to translate host names into IP addresses. Keep in mind that the hosts file has a bigger priority then DNS, consequently the hosts are first resolved from here.

Example:

cat /etc/hosts
127.0.0.1 myserver.example.com myserver
192.168.0.1 myrouter

In this example we have defined two hosts.
1) myserver with the IP address 127.0.0.1 and the FQDN is myserver.example.com.
2) myrouter with the IP address 192.168.0.1, without a domain name, this is not a FQDN.

Test:
Just use the simple ping command, as you see in the example from below, the host is successfully translated.

ping myserver -c 1
PING myserver (127.0.0.1) 56(84) bytes of data.
64 bytes from myserver (127.0.0.1): icmp_seq=1 ttl=64 time=0.045 ms

What is FQDN?

FQDN is the abbreviation for Fully Qualified Domain Name, a domain name which must include at least a second-level domain and a top-level domain.
In our example myserver.example.com:
– host name is myserver
– second level-domain is example
– top-level domain is .com.

So.. the correct entry of a FQDN would be:
ip_address hostname.domain hostname

Local system’s host name

Some apps or services like Kerberos requires that the local system’s host name to be FQDN.
/etc/hostname file stores the system’s host name, if is modified the change will be effective only after reboot.

cat /etc/hostname
myserver

Check if local system’s host name is FQDN

You need to make sure that the local host name has a valid FQDN entry in /etc/hosts.

cat /etc/hosts | grep `hostname`
127.0.0.1 myserver.example.com myserver

You can also check with the hostname command.

Display the host name

hostname
myserver

Display the domain name

hostname -d
example.com

Display the FQDN

hostname -f
myserver.example.com

Create shortcut commands with aliases and functions in Bash

Shortcut commands are very handy, they can reduce typing and increase productivity, we can create these commands with aliases and functions. The alias command is built into shells like bash, sh, ksh, zsh and dash, in this tutorial we will use bash, the most popular shell in Linux. Functions can be used when more powerful operations and logic is needed, let’s test them and see how they work!

Aliases

Show all defined aliases

Also I would like to share my favorite ones, feel free to post in the comments your favorite. Thanks!

alias
alias cp='cp -i'
alias mv='mv -i'
alias t='telnet'
alias s='ssh'
alias p='ping'
alias v='vim'
alias h='history'
alias ls='ls --color=auto'
alias ll='ls -alh'
alias grep='grep --color'
alias pinf='ping -i 0.2'
alias pinb='ping -s 9000'
alias p10='sudo ping -c 10 -i 0.02'
alias p100='sudo ping -c 100 -i 0.02'
alias update='sudo apt-get update'
alias upgrade='sudo apt-get upgrade'
alias install='sudo apt-get install'
alias cache='sudo apt-cache'

Can be found at: https://github.com/oueta/linux.bashrc.git

Create aliases

alias ls='ls --color=auto'

Remove alias

unalias ls

Note

If you add or remove an alias by command it will be available only for the current session, to make them permanent you need to add them to ~/.bashrc for the current user or /etc/bash.bashrc (Debian) , /etc/bashrc (CentOS) for all users. Also to load the bashrc file you need to start a new session or run the source command: source ~/.bashrc.

Run a command directly

If the command has the same name as the alias, you can run the command by using ‘\’ ( backslash ).

\ls

Functions

If you need to do something more complicated then an alias, you can use functions. To make them permanent you need to add them to bashrc, see the note from above.

Example

function print_param {
      if [ ! -z "$1" ]
        then
          echo "The parameter nr. 1 is $1"
          if [ ! -z "$2" ]
            then
              echo "The parameter nr. 2 is $2"
            fi
        fi
}