Thursday, November 27, 2014

Hostname Command Examples in Linux



Hostname is the name of the system or server you are logged into. The hostname can also refer to the sitename or computer name. As an example, if an organization domain name is "google.com" and a specific computer name in that doman is "linux", then the hostname of the computer is "linux.google.com".
The syntax of hostname command in unix or linux system is
hostname [options] [file]

The options of hostname command are:
-a : Prints the alisa name of the host if created any.
-d : prints the domain name
-i : prints the ip address of the host
-s : prints the shortname of the host.
-v : verbose data
-V : version information
-h : help about hostname command

Hostname Command Examples:

1. Print the hostname of the system The basic functionality of the hostname command is to display the name of the system on the terminal. Just type the hostname on the unix terminal and press enter to print the hostname.
> hostname
linux.google.com

2. Ip address of the computer You can find the ip address of the computer by using the -i option with hostname command.
> hostname -i
125.20.223.69

3. Print the domain name To know the domain name where the computer resides, use the -d option with hostname command.
> hostname -d
google.com

4. Short hostname By default the hostname command prints the complete name of the computer. You can print a short name by using the -s option. This prints the name upto the first dot in the full hostname.
> hostname -s
linux

5. Getting help To get help about the hostanme command either use the man command or the -h option with hostname command.
> man hostname
> hostname -h

Chmod Command Examples in Linux

Chmod (change mode) is one of the most frequently used commands in unix or linux operating system. The chmod command is used to change the file or directory access permissions. To know about the access permissions of a file or directory, use the ls -l command as shown below:

$ ls -l sample.sh
-rwx-rw-r-- 1 matt deploy 94 Oct  4 03:12 sample.sh

Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission.
File and Directory permissions:

There are three different permissions. They are:
  • Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
  • Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
  • Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.
Here in the above, the numbers in the brackets represents the numeric values for the corresponding permissions. If you want to have a combination of permissions add the required numbers. For example, for read and execute, it is 4+1=5.

The syntax of chmod command is
chmod [options] mode filename

The important options are:
-R : recursively change the permissions of a directory.
-v : Verbose

Chmod Examples in Linux / Unix:

1. Give read, write and execute permissions to everyone.

Read, write and execute: 4+2+1=7
$ chmod 777 sample.sh

In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions.

chmod ugo+rwx sample.sh

We will see more details about the symbolic representation later.

2. Give read permission to user, write permission to group and execute permission to others.
$ chmod 421 sample.sh

3. Recursive permissions to directory

To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R.
chmod -R 666 /dir

Symbolic Representation of Permissions:

The following symbols are used to represent the users, groups and others:
  • u : User
  • g : Group
  • o : Others a : All (user, group and others)
The following symbols represents the permissions:
  • r : read
  • w : write
  • x : execute 
The following Numerical  represents the permissions:
  • 4: read
  • 2: write
  • 1:execute


The following symbols represent the permissions grant or revoke:
  • + : Additional permissions. Selected permissions are added.
  • - : Revoke the permissions. Selected permissions are revoked.
  • = : Specific permissions. Only selected permissions are assigned. 

Examples:

1. Remove write permission from group
$ chmod g-w sample.sh

This will only removes the write permission for the group.

2. Add new permission execute to others
$ chmod o+x sample.sh

In addition to the existing permissions, this will add execute permission to others.

3. Give only read permissions to the user
$ chmod u=w sample.sh

This will remove the existing permissions to the user and gives only write permission to the user.

Tail Command Examples in Linux

The tail command in unix or linux system is used to print the last N lines from the file on the terminal. Tail command is especially used with log files to read the last few lines to know about the error messages. The syntax of tail command is

tail [options] [files]

The tail command options are:
  • c : Prints the last N bytes of file; With leading +, prints the characters from the N byte in the file.
  • n : Prints last N lines; With leading + prints lines from the Nth line in the file.
  • f : Prints the appended lines on the terminal as the file grows.

Tail Command Examples

Create the following file in your linux or unix operating system for practising the examples:
> cat example.txt
virtual storage
oracle virtual instance
mysql backup
dedicated hosting server
cloud servers

1. Display last 10 lines

By default, the tail command prints the last 10 lines from the file.
> tail example.txt

2. Display last N lines

Use the -n option to print the last n lines from the file. The following example prints the last 2 lines from the file:
> tail -n2 example.txt
dedicated hosting server
cloud servers

3. Print lines from the Nth line

You can print lines starting from the Nth line in a file. The following example prints lines from the 2nd line.
> tail -n+2 example.txt
oracle virtual instance
mysql backup
dedicated hosting server
cloud servers

4. Print the last n bytes.

use the -c option to print the last N bytes from the file. The following example prints the last 8 bytes from the file.
> tail -c8 example.txt
servers

5. Print characters from the Nth byte.

Use the leading "+" with -c option to print the characters from the Nth byte. The following example prints the characters from the 79th byte.
> tail -c+79 example.txt
cloud servers

6. Print last lines from dynamically changing file.

The -f option print the lines from file that is growing dynamically. When you run the tail -f filename command, it prints the last 10 lines and waits for new lines to be added to the file. Whenever the new lines are appended to the file, the tail command also appends the new lines on the standard output. The -f option is useful when debugging applications. In general, the applications writes error messages to log files. You can use the -f option to check for the error messages as and when they appear in the log file.

> tail -f logfile

SCP Command Examples - Linux

SCP stands for secure copy is used to copy data (files or directories) from one unix or linux system to another unix or linux server. SCP uses secured shell (ssh) to transfer the data between the remote hosts. The features of SCP are:

  • Copies files within in the same machine
  • Copies files from local machine to remote machine.
  • Copies files from remote machine to local machine.
  • Copies files between two different remote servers.

SCP Command Syntax:

The syntax of SCP command is

scp [Options] [[User@]From_Host:]Source_File [[User@]To_Host:][Destination_File]

Each element of the scp command is explained in detail below:

  • User is the one who have the permissions to access the files and directories. User should have read permissions if it is a source and write permissions if it is the destination.
  • From_Host: hostname or Ip address where the source file or directory resides. This is optional if the from host is the host where you are running the scp command.
  • Source_File: Files or directories to be copied to the destination.
  • To_Host: Destination host where you want to copy the files. You can omit this when you want to copy the files to the host where you are issuing the scp command.
  • Destination_File: Name of the file or directory in the target host.

SCP Command Options:

The important SCP command options are listed below:

  • -r : Recursively copies the contents of source files or directories.
  • -p : Preserves the access time, modification time, permissions of the source files in the destination.
  • -q : Progress bar in not displayed
  • -v : verbose mode. Displays debugging messages.
  • -P : copy files using the specified port number.

SCP Command Examples:

Let see the examples of scp command in unix or linux system.

1. Copying with in the same system

You can use the scp command just like the cp command to copy files from one directory to another directory.

scp Unix-storage.dat /var/tmp/

This command copies the file unix-storage.dat from current directory to the /var/tmp directory.

2. Copy file from local host to remote server

This is most frequently used operation to transfer files in unix system.

scp filename user@remotehost:/remote/directory/

This command connects to the remote host and copies the specified file to the /remote/directory/.

3. Copy files from remote host to local server.

This operation is used when taking backup of the files in remote server.

scp user@remotehost:/usr/backup/oracle_backup.dat .

This command copies the oracle backup file in the remote host to the current directory.

4. Copying files between two remote servers

The scp command can also be used to copy files between two remote hosts.

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh target_user@target_remote_host:/var/tmp/

The above command copies the mysql bakup shell script from the source remote host the /var/tmp directory of target remote host.

5. Copying a directory.

To copy all the files in a directory, use the -r option with the scp command. This makes the scp command to copy the directory recursively.

scp -r directory user@remotehost:/var/tmp/

The above command copies the directory from local server to the remote host.

6. Improving performance of scp command

By default the scp command uses the Triple-DES cipher/AES-128 to encrypt the data. Using the blowfish or arcfour encryption will improve the performance of the scp command.

scp -c blowfish filename  user@remoteserver:/var/
scp -c arcfour  localfile user@remoteserver:/var/

7. Limit bandwidth

You can limit the bandwidth used by the scp command using the -l option.

scp -l bandwidth_limit filename user@hostname:/usr/backup/
Here bandwidth_limit is numeric to be specified in kilobits per second.

8. Specifying the port number

We can make the scp command to copy the files over a specified port number using the -P option.

scp -P 6001 storage_backup.bat username@hostname:/tmp/

Wednesday, November 26, 2014

fstab file format

The /etc/fstab file can be used to define how disk partitions, various other block devices, or remote filesystems should be mounted into the filesystem.
Each filesystem is described in a separate line. These definitions will be converted into systemd mount units dynamically at boot, and when the configuration of the system manager is reloaded. The default setup will automatically fsck and mount filesystems before starting services that need them to be mounted. For example, systemd automatically makes sure that remote filesystem mounts like NFS or Samba are only started after the network has been set up. Therefore, local and remote filesystem mounts specified in /etc/fstab should work out of the box. See man 5 systemd.mount for details.
The mount command will use fstab, if just one of either directory or device is given, to fill in the value for the other parameter. When doing so, mount options which are listed in fstab will also be used.
File example
A simple /etc/fstab, using kernel name descriptors:
/etc/fstab
# <file system>        <dir>         <type>    <options>             <dump> <pass>
/dev/sda1              /             ext4      defaults,noatime      0      1
/dev/sda2              none          swap      defaults              0      0
/dev/sda3              /home         ext4      defaults,noatime      0      2

Field definitions
Each line in the /etc/fstab file contains the following fields separated by spaces or tabs:
file_system    dir    type    options    dump    pass
file system
The partition or storage device to be mounted.
dir
The mountpoint where <file system> is mounted to.
type
The file system type of the partition or storage device to be mounted. Many different file systems are supported: ext2, ext3, ext4, btrfs, reiserfs, xfs, jfs, smbfs, iso9660, vfat, ntfs, swap and auto. The auto type lets the mount command guess what type of file system is used. This is useful for optical media (CD/DVD).
options
Mount options of the filesystem to be used. See the mount man page. Please note that some options are specific to filesystems; to discover them see below in the aforementioned mount man page.
dump
Used by the dump utility to decide when to make a backup. Dump checks the entry and uses the number to decide if a file system should be backed up. Possible entries are 0 and 1. If 0, dump will ignore the file system; if 1, dump will make a backup. Most users will not have dump installed, so they should put 0 for the dump entry.
pass
Used by fsck to decide which order filesystems are to be checked. Possible entries are 0, 1 and 2. The root file system should have the highest priority 1 (unless its type is btrfs, in which case this field should be 0) - all other file systems you want to have checked should have a 2. File systems with a value 0 will not be checked by the fsck utility.


Identifying filesystems

There are different ways to identify filesystems that will be mounted. /etc/fstab does support several methods: kernel name descriptor, label or UUID, and GPT labels and UUID for GPT disks. UUID must be privileged over kernel name descriptors and labels. See Persistent block device naming for more explanations. It is recommended to read that article first before continuing with this article.
In this section, we will describe how to mount filesystems using all the mount methods available via examples. The output of the commands lsblk -f and blkid used in the following examples are available in the article Persistent block device naming. If you have not read that article yet, please read it now.

Kernel name descriptors

Run lsblk -f to list the partitions and prefix the values in the NAME column with /dev/.
/etc/fstab
# <file system> <dir> <type> <options>                                                                                            <dump> <pass>
/dev/sda1       /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
/dev/sda2       /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
/dev/sda3       /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
/dev/sda4       none  swap   defaults                                                                                             0      0

Labels

Run lsblk -f to list the partitions, and prefix the values in the LABEL column with LABEL=:
/etc/fstab
# <file system> <dir> <type> <options>                                                                                            <dump> <pass>
LABEL=EFI       /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
LABEL=SYSTEM    /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
LABEL=DATA      /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
LABEL=SWAP      none  swap   defaults                                                                                             0      0
Note: If any of your fields contains spaces, see #Filepath spaces.

UUIDs

Run lsblk -f to list the partitions, and prefix the values in the UUID column with UUID=:
/etc/fstab
# <file system>                           <dir> <type> <options>                                                                                            <dump> <pass>
UUID=CBB6-24F2                            /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
UUID=0a3407de-014b-458b-b5c1-848e92a327a3 /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
UUID=f9fe0b69-a280-415d-a03a-a32752370dee none  swap   defaults                                                                                             0      0
Tip: If you would like to return just the UUID of a specific partition: $ lsblk -no UUID /dev/sda2

GPT labels

Run blkid to list the partitions, and use the PARTLABEL values without the quotes:
/etc/fstab
# <file system>                      <dir> <type> <options>                                                                                            <dump> <pass>
PARTLABEL=EFI\040SYSTEM\040PARTITION /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
PARTLABEL=GNU/LINUX                  /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
PARTLABEL=HOME                       /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
PARTLABEL=SWAP                       none  swap   defaults                                                                                             0      0
Note: If any of your fields contains spaces, see #Filepath spaces.

GPT UUIDs

Run blkid to list the partitions, and use the PARTUUID values without the quotes:
/etc/fstab
# <file system>                               <dir> <type> <options>                                                                                            <dump> <pass>
PARTUUID=d0d0d110-0a71-4ed6-936a-304969ea36af /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
PARTUUID=98a81274-10f7-40db-872a-03df048df366 /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
PARTUUID=7280201c-fc5d-40f2-a9b2-466611d3d49e /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
PARTUUID=039b6c1c-7553-4455-9537-1befbc9fbc5b none  swap   defaults                                                                                             0      0

Tips and tricks

Automount with systemd

If you have a large /home partition, it might be better to allow services that do not depend on /home to start while /home is checked by fsck. This can be achieved by adding the following options to the /etc/fstab entry of your /home partition:
noauto,x-systemd.automount
This will fsck and mount /home when it is first accessed, and the kernel will buffer all file access to /home until it is ready.
Note: This will make your /home filesystem type autofs, which is ignored by mlocate by default. The speedup of automounting /home may not be more than a second or two, depending on your system, so this trick may not be worth it.
The same applies to remote filesystem mounts. If you want them to be mounted only upon access, you will need to use the noauto,x-systemd.automount parameters. In addition, you can use the x-systemd.device-timeout=# option to specify a timeout in case the network resource is not available.
Note: If you intend to use the exec flag with automount, you should remove the user flag for it to work properly as found in the course of a Fedora Bug Report
If you have encrypted filesystems with keyfiles, you can also add the noauto parameter to the corresponding entries in /etc/crypttab. systemd will then not open the encrypted device on boot, but instead wait until it is actually accessed and then automatically open it with the specified keyfile before mounting it. This might save a few seconds on boot if you are using an encrypted RAID device for example, because systemd does not have to wait for the device to become available. For example:
/etc/crypttab
data /dev/md0 /root/key noauto

Filepath spaces

Since spaces are used in fstab to delimit fields, if any field (PARTLABEL, LABEL or the mount point) contains spaces, these spaces must be replaced by escape characters \ followed by the 3 digit octal code 040:
/etc/fstab
UUID=47FA-4071     /home/username/Camera\040Pictures   vfat  defaults,noatime       0  0
/dev/sda7          /media/100\040GB\040(Storage)       ext4  defaults,noatime,user  0  2

External devices

External devices that are to be mounted when present but ignored if absent may require the nofail option. This prevents errors being reported at boot.
/etc/fstab
/dev/sdg1        /media/backup    jfs    defaults,nofail    0  2

Reference: https://wiki.archlinux.org/index.php/Fstab