Setup DNS Resolution With “resolv.conf” in Examples

The /etc/resolv.conf configuration file contains information that allows a computer to convert alpha-numeric domain names into the numeric IP addresses.

The process of converting domain names to IP addresses is called resolving.

When using DHCP, dhclient usually rewrites resolv.conf with information received from the DHCP server.

How do I edit the “/etc/resolv.conf” file?

Use text editor such as vi or gedit from Linux desktop:

# vi /etc/resolv.conf

There are three main configuration directives in /etc/resolv.conf:

nameserver # DNS server IP
domain # Domain Name of local host
search # Which Domain to search

The “nameserver” directive

The nameserver directive points out to the IP address of a Name Server.

nameserver <IP address>

It can be your own Name Server, some public Name Server or the Name Server of your’s ISP.

Note: Up to 3 name servers may be listed.

Example:

nameserver 192.168.0.100
nameserver 8.8.8.8
nameserver 8.8.4.4

The “domain” directive

Local domain name.

domain <local domain name>

Example:

domain domain.com
nameserver 192.168.0.100
nameserver 8.8.4.4

How the “domain” directive in the “resolv.conf” file works?

You can use domain directive for resolving short host-names – e.g. test.
So if you have the following in your /etc/resolv.conf:

domain example.com

Then your computer will try to resolve test.example.com.

The “search” directive

Search list for hostname lookup. The search list is normally determined from the local domain name but it can be set to a list of domains.

search <search list>

Example:

search example.com company.net
nameserver 192.168.0.100
nameserver 8.8.8.8

How the “search” directive in the “resolv.conf” file works?

You need to use search directive for resolving short host-names – e.g. test.
So if you have the following in your /etc/resolv.conf:

search example.com company.net

Then your computer will try to resolve test.example.com followed by test.company.net. It will return the first query that was successful.

Upgrading CentOS/RHEL from Minimal Installation with YUM

If you have installed CentOS/RHEL Minimal server installation, you may have lots of troubles with not installed packages.

There is a way to install all packages, required by a Basic Server, using yum groupinstall command.

groupinstall – is used to install all of the individual packages in a group, of the specified types.

[groupinstall] – Install a Group of Packages

Upgrade your server from Minimal installation to Basic:

# yum groupinstall "Base"

Upgrade your system to Web Server:

# yum groupinstall "Web Server"

Upgrade your system to MySQL Database Server:

# yum groupinstall "MySQL Database Server"

and so on …

[grouplist] – List Installed and Available Groups

List all installed and available groups:

# yum grouplist

[groupinfo] – List Packages in a Group

Show the description and package list of a group:

# yum groupinfo "Base"

[groupremove] – Remove a Group of Packages

Remove all of the packages in a group:

# yum groupremove "Base"

Linux: Define Locale and Language Settings

Locales are used in Linux to define which language and character set (encoding) user see in the terminal.

From this article you will learn how to check and change the current locale and language settings from the command line in Linux.

I will show how to check the current locale and language settings and how to get the list of available locals.

You will also see how to set locale and language temporary for the current session only as well as permanently for a single user or for the all users (change default system locale). (more…)

HowTo: Download and Extract (untar) TAR Archive with One Command

Use one of the following commands to download and extract (untar) [tar], [tar.gz] or [tar.bz2] files “on fly”, without saving archive themselves.

  • No temporary files;
  • No Extra output;
  • Minimal file space and memory usage.

The following methods are most fast and compact for downloading and unpacking archives.

Download and Extract Archives with WGET

$ wget http://example.com/archive.tar -O - | tar -x
$ wget http://example.com/archive.tar.gz -O - | tar -xz
$ wget http://example.com/archive.tar.bz2 -O - | tar -xj

Download and Extract Archives with CURL

$ curl http://example.com/archive.tar | tar -x
$ curl http://example.com/archive.tar.gz | tar -xz
$ curl http://example.com/archive.tar.bz2 | tar -xj
Option Description
-x extract files from an archive
-z decompress the contents of the compressed archive created by gzip program [tar.gz]
-j decompress the contents of the compressed archive created by bzip2 program [tar.bz2]

Start a GUI Application on a Remote Computer using SSH

This article describes how to log into the remote computer (server) using SSH and run a GUI (graphical) application that requires screen. The GUI program will be displayed on the physical monitor, connected to that remote machine.

Run a GUI Program on a Remote Computer’s Screen

The Basic Procedure

Log into a remote machine using SSH:

$ ssh 192.168.1.100

Tell GUI applications to be launched on the local screen (so, any graphical program that you run, will be displayed on the remote computer’s screen):

$ export DISPLAY=:0

Execute GUI Program. For, example lets start Firefox browser that will be launched and displayed on the remote machine’s screen in which we logged in:

$ firefox "www.shellhacks.com"

Use nohup to prevent a process from being stopped after closing SSH session:

$ nohup firefox "www.shellhacks.com"

More Examples

Send a pop-up notification (notify-send) that will be shown on the remote computer’s screen:

$ ssh 192.168.0.100 'DISPLAY=:0 nohup notify-send "Hello" "World"'

Start a music player (rhythmbox) on the remote computer:

$ ssh 192.168.0.100 'DISPLAY=:0 nohup rhythmbox ./Smoke-on-the-Water.mp3'

Untar Tar.gz – Linux Tar Command – HowTo: Extract Tar File

Most of the Linux files that can be downloaded from the Internet are compressed with a tar, tar.gz and tar.bz2 compression formats and it is important to know how to extract such files.

The following article will help you to extract (unpack) and uncompress (untar) – tar, tar.gz and tar.bz2 files from the Linux command line.

You will learn how to list the contents of a tar archive without unpacking it and how to extract only a single file or a single directory. (more…)

Finding Active Computers in Local Network from Linux

Searching for Linux command that can list all IP addresses of devices connected to the network?

Use nmap or ping commands to determine alive hosts in your local network.

[nmap] Scan Network for Alive Computers

Scan for active hosts on a network using nmap command:

# Standard ICMP ping
$ nmap -sn 192.168.1.0/24

Sample output:

Starting Nmap 6.00 ( http://nmap.org ) at 2013-06-14 00:52 EEST
Nmap scan report for 192.168.1.1
Host is up (0.0031s latency).
Nmap scan report for 192.168.1.101
Host is up (0.00097s latency).
Nmap scan report for 192.168.1.102
Host is up (0.065s latency).
Nmap done: 256 IP addresses (3 hosts up) scanned in 2.98 seconds

[ping] Find Active Hosts in LAN

Use the following script to find out what computers in your local network respond to ping:

$ echo 192.168.1.{1..254}|xargs -n1 -P0 ping -c1|grep "bytes from"

Sample output:

64 bytes from 192.168.1.101: icmp_req=1 ttl=64 time=0.042 ms
64 bytes from 192.168.1.1: icmp_req=1 ttl=64 time=37.4 ms
64 bytes from 192.168.1.102: icmp_req=1 ttl=64 time=208 ms

Discover Computers Behind a Firewall

Some hosts may have a firewall, and will not respond to standard ICMP pings.

If a firewall is blocking standard ICMP pings, try the following host discovery methods:

# TCP SYN Ping
$ nmap -sn -PS 192.168.1.0/24

# TCP ACK Ping
$ nmap -sn -PA 192.168.1.0/24

# UDP Ping
$ nmap -sn -PU 192.168.1.0/24

# IP Protocol Ping
$ nmap -sn -PO 192.168.1.0/24

# ARP Ping
$ nmap -sn -PR 192.168.1.0/24

Last three commands should be executed with root credentials.

Print Usage and Exit if Arguments are Not Provided

Use the following test in your shell scripts to:

1. verify the number of input values

2. display an error message if the number of input argument is not correct

3. exit a shell script with the error status

[ $# -eq 0 ] && { echo "Usage: $0 argument"; exit 1; }
Parameter Description
$# variable tells the number of input arguments the script was passed
-eq 0 check if the number of input arguments is equal zero
$0 returns the path to your shell script

Bash Script Example

The following script is using dig command to find the name server of a domain name. The domain name should be provided as an argument.

#!/bin/bash
domain=$1
[ $# -eq 0 ] && { echo "Usage: $0 domain_name"; exit 1; }
dig NS $domain @8.8.8.8 +short

Sample output if no arguments are specified :

$ ./find_ns.sh
Usage: ./find_ns.sh domain_name

Sample output if an argument is passed:

$ ./find_ns.sh shellhacks.com
ns2.ukraine.com.ua.
ns3.ukraine.com.ua.
ns1.ukraine.com.ua.

7 Tips – Tuning Command Line History in Bash

7 Tips that can help you to improve you Bash history file.

You will learn how to add date and time to bash history file, increase history size, ignore specific commands and much more …

Every command that you enter, is stored in the file ~/.bash_history. Run history to see your last commands.

You can improve your Bash history, appending different environment variables to your ~/.bashrc file.

After modifying ~/.bashrc file, execute the following command to apply changes:

source ~/.bashrc

1. Add Date and Time to Bash History

Sometimes it would be very nice to know when some command got executed.

Set HISTTIMEFORMAT to print the time stamps associated with each history entry.

Append the following line to ~/.bashrc file:

export HISTTIMEFORMAT="%h %d %H:%M:%S "

Now, when you type history, it will show something like:

113  Jun 08 16:31:06 sudo ifconfig
114  Jun 08 16:31:10 top
115  Jun 08 16:31:19 ping 8.8.8.8
116  Jun 08 16:31:22 history

2. Increase Bash History Size

Increase HISTSIZE – the number of commands to remember in the command history (the default value is 500).

export HISTSIZE=10000

Increase HISTFILESIZE – the maximum number of lines contained in the history file (the default value is 500).

export HISTFILESIZE=10000

3. Append Bash Commands to History File

Bash overwrites .bash_history file?

To append commands to the history file, rather than overwrite it, add the following line to ~/.bashrc:

shopt -s histappend

4. Store Bash History Immediately

By default, Bash only records a session to the .bash_history file when the session terminates.

This means that if you crash or your session terminates improperly, you lose the history up to that point.

Use $PROMPT_COMMAND variable to save each command right after it has been executed.

Append the following line to ~/.bashrc file, if the variable $PROMPT_COMMAND hasn’t been set yet:

PROMPT_COMMAND='history -a'

Append the following line, if the variable $PROMPT_COMMAND has already been set:

PROMPT_COMMAND='$PROMPT_COMMAND; history -a'

5. Control Bash History

HISTCONTROL is a colon-separated list of values controlling how commands are saved in the history file.

Value Description
ignorespace don’t save lines which begin with a <space> character
ignoredups don’t save lines matching the previous history entry
ignoreboth use both ‘ignorespace’ and ‘ignoredups’
erasedups eliminate duplicates across the whole history

Example:

export HISTCONTROL=ignorespace:erasedups

6. Ignore Specific Commands

HISTIGNORE is a colon-separated list of patterns used to decide which command lines should be saved in the history file.

Don’t save ls, ps and history commands:

export HISTIGNORE="ls:ps:history"

Don’t save commands with s in the beginig:

export HISTIGNORE="s*"

7. Use one command per line

Store multi-line commands in one history entry:

shopt -s cmdhist

Change the History File Name

Use HISTFILE to change the name of the file in which Bash history is saved. The default value is ~/.bash_history.

export HISTFILE=~/.custom_file

Change Network Interface Name: eth0,eth1,eth2+

The best way to rename a network interface is through udev.

Edit the file /etc/udev/rules.d/70-persistent-net.rules to change the interface name of a network device.

The names of the network devices are listed in this file as follows:

# PCI device 0x11ab:0x4363 (sky2)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
ATTR{address}=="00:00:00:00:00:00",ATTR{dev_id}=="0x0", ATTR{type}=="1",
KERNEL=="eth*", NAME="eth0"

Rename network interface from eth0 to wan0

To rename interface eth0 to wan0, edit /etc/udev/rules.d/70-persistent-net.rules file and change NAME="eth0" to NAME="wan0".

# PCI device 0x11ab:0x4363 (sky2)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
ATTR{address}=="00:00:00:00:00:00",ATTR{dev_id}=="0x0", ATTR{type}=="1",
KERNEL=="eth*", NAME="wan0"

For Centos/RHEL etc.

Rename the network interface configuration file:

# cd etc/sysconfig/network-scripts/
# mv ifcfg-eth0 ifcfg-wan0

Edit the network interface configuration file and replace all occurrences of the old name eth0 with the new one wan0:

# vi /etc/sysconfig/network-scripts/ifcfg-wan0

For Ubuntu etc.

Edit the /etc/network/interfaces file and replace all occurrences of the old name eth0 with the new one wan0:

# sudo nano /etc/network/interfaces

Test changes

Reboot the system to test changes:

# reboot

Verify new settings:

# ifconfig -a

Rename network interface from eth1 back to eth0

Q: Why does my network interface name change?
A: The interface name of a network device increases if the MAC address of a network card changes.

Edit the file /etc/udev/rules.d/70-persistent-net.rules.

Copy the new MAC address from eth1 to the line of your eth0 rule.

Delete the rule for eth1. Save and close the file.

For Centos/RHEL etc.

Check the network interface configuration located under:

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

Don’t forget to replace the old MAC address with the new one.

For Ubuntu etc.

Make sure /etc/network/interfaces file has correct configuration:

# sudo nano /etc/network/interfaces

Test changes

Reboot the system to test changes:

# reboot

Verify new settings:

# ifconfig -a