Tuesday 28 June 2016

Linux Machine performance Monitoring with VMSTAT command


 


vmstat (virtual memory statistics) is a computer system monitoring tool that collects and displays summary information about operating system memory, processes, interrupts, paging and block I/O.
              
vmstat is a tool that collects and reports data about your system’s memory, swap, and processor resource utilization in real time. It can be used to determine the root cause of performance and issues related to memory use.

The vmstat tool is available on most Unix and Unix-like operating systems, such as FreeBSD, Linux or Solaris. The syntax and output of vmstat often differs slightly between different operating systems.

   Options:-

-a       The -a switch displays active/inactive memory, given a 2.5.41 kernel or better.

-f       The  -f  switch displays the number of forks since boot.  This includes   the fork, vfork, and clone system calls,
         and is equivalent to the total number  of  tasks  created.  Each process is represented by one or more tasks,
         depending on thread usage.  This display does not repeat.

-m       The -m displays slabinfo.

-n       The -n switch causes the header to be displayed only once  rather  than periodically.

-s       The  -s  switch  displays  a table of various event counters and memory statistics. This display does not repeat.

delay    delay is the delay between updates in seconds.  If no delay  is  specified, only one report is printed with the 
         average values since boot.

count    count  is the number of updates.  If no count is specified and delay is defined, count defaults to infinity.

-d       The -d reports disk statistics (2.5.70 or above required).

-p       The -p followed by some partition name for detailed statistics  (2.5.70 or above required).

-s       The  -S  followed  by  k  or K or m or M switches outputs between 1000, 1024, 1000000, or 1048576 bytes.

-v       The -V switch results in displaying version information.


FIELD DESCRIPTION FOR VM MODE:-

Proc:-
r:  The number of processes waiting for run time.
b:  The number of processes in uninterruptible sleep.


Memory:-
  swpd: the amount of virtual memory used.
  free: the amount of idle memory.
  buff: the amount of memory used as buffers.
  cache: the amount of memory used as cache.
  inact: the amount of inactive memory. (-a option)
  active: the amount of active memory. (-a option)


Swap:-
  si: Amount of memory swapped in from disk (/s).
  so: Amount of memory swapped to disk (/s).


IO:-
  bi: Blocks received from a block device (blocks/s).
  bo: Blocks sent to a block device (blocks/s).


System:-
  in: The number of interrupts per second, including the clock.
  cs: The number of context switches per second.


CPU:-
  These are percentages of total CPU time.
  us: Time spent running non-kernel code. (user time, including nice time)
  sy: Time spent running kernel code. (system time)
  id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
  wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.


FIELD DESCRIPTION FOR DISK MODE
:-

Reads:-
   total:   Total reads completed successfully
   merged:  Grouped reads (resulting in one I/O)
   sectors: Sectors read successfully
   ms:      Milliseconds spent reading

Writes:-
   total:   Total writes completed successfully
   merged:  Grouped writes (resulting in one I/O)
   sectors: Sectors written successfully
   ms:      Milliseconds spent writing

IO:-
   cur:     I/O in progress
   s:       seconds spent for I/O

FIELD DESCRIPTION FOR DISK PARTITION MODE:-

reads:            Total number of reads issued to this partition
read sectors:     Total read sectors for partition
writes :          Total number of writes issued to this partition
requested writes: Total number of write requests made for partition

FIELD DESCRIPTION FOR SLAB MODE:-

cache:    Cache name
num:      Number of currently active objects
total:    Total number of available objects
size:     Size of each object
pages:    Number of pages with at least one active object
totpages: Total number of allocated pages
pslab:    Number of pages per slab

FILES:-

/proc/meminfo
/proc/stat
/proc/*/stat

vmstat commands output with examples:-

1.    To display active and inactive memory information status.

[root@liniuxbiginner ~]# vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 140268  48416 673184    0    0     0   103    5    2  0  0 99  1  0


2.     To display no. of forks since last boot.

[root@liniuxbiginner ~]# vmstat -f
      3357326 forks

 

3.      To Display slab information.

[root@liniuxbiginner ~]# vmstat -m
Cache                       Num  Total   Size  Pages
ip6-frags                     0      0    232     35
UDPLITEv6                     0      0   1088     30
UDPv6                        30     30   1088     30
tw_sock_TCPv6                29     29    280     29
request_sock_TCPv6            0      0    328     24
TCPv6                        15     15   2112     15
ext4_groupinfo_4k           336    336    144     28
ext4_inode_cache          76380  76516   1032     31
ext4_free_data              192    192     64     64
ext4_allocation_context      32     32    128     32
ext4_io_end                 560    560     72     56
ext4_extent_status        30197  38454     40    102
jbd2_journal_handle          85     85     48     85
jbd2_journal_head          1088   1088    120     34

Timestamp:-

1.     To display the timestamp along with every line.

[root@liniuxbiginner ~]# vmstat -t
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 139708 158452 193744    0    0     0   103    5    2  0  0 99  1  0        2016-06-28 11:15:17 UTC

 2.     To display every 3 seconds for 10 lines with timestamp.

[root@liniuxbiginner ~]# vmstat -t 3 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 139660 158472 193896    0    0     0   103    5    2  0  0 99  1  0        2016-06-28 11:22:28 UTC
 0  0      0 139024 158472 193900    0    0     0    12   65  181  0  0 99  0  0        2016-06-28 11:22:31 UTC
 0  0      0 139552 158472 193900    0    0     0   364   80  222  0  0 97  2  0        2016-06-28 11:22:34 UTC
 0  0      0 139544 158472 193900    0    0     0    20   66  196  0  0 99  0  0        2016-06-28 11:22:37 UTC
 0  0      0 139544 158472 193900    0    0     0    13   53  140  0  0 100  0  0       2016-06-28 11:22:40 UTC
 0  0      0 139516 158472 193908    0    0     0   432   96  251  0  0 97  2  0        2016-06-28 11:22:43 UTC
 0  0      0 138900 158472 193908    0    0     0    13   81  197  0  0 100  0  0       2016-06-28 11:22:46 UTC
 0  0      0 139272 158472 193908    0    0     0    11   64  169  1  0 99  0  0        2016-06-28 11:22:49 UTC
 0  0      0 139272 158472 193908    0    0     0     9   55  153  0  0 100  0  0       2016-06-28 11:22:52 UTC
 0  0      0 139552 158472 193908    0    0     0   337   74  200  0  0 98  2  0        2016-06-28 11:22:55 UTC


3.     To check vmstat version.

[root@liniuxbiginner ~]# vmstat -V
procps version 3.2.8

4.     To display the event counter statistics in table format.

[root@liniuxbiginner ~]# vmstat -s
      1019344  total memory
       879884  used memory
       673880  active memory
        48312  inactive memory
       139460  free memory
       158496  buffer memory
       194044  swap cache
            0  total swap
            0  used swap
            0  free swap
      1455666 non-nice user cpu ticks
       267970 nice user cpu ticks
       360814 system cpu ticks
    522241945 idle cpu ticks
      3575447 IO-wait cpu ticks
            0 IRQ cpu ticks
        17393 softirq cpu ticks
       276374 stolen cpu ticks
      1125512 pages paged in
    542706588 pages paged out
            0 pages swapped in
            0 pages swapped out
    329226970 interrupts
    870644185 CPU context switches
   1461822708 boot time
      3358327 forks

 To do not rediplay header:-

[root@liniuxbiginner ~]# vmstat -n
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 139484 158504 194092    0    0     0   103    5    2  0  0 99  1  0

 1.     To displays the read,I/O statistic and write of the disk statistics.

[root@liniuxbiginner ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
xvda   80091     47 2248744  665380 28417545 8633645 1085463440 150978028      0  37475
xvdb     286      0    2288     272      0      0       0       0      0      0

2.      To display report summary statistics about disk activity.

[root@liniuxbiginner ~]# vmstat -D
            2 disks
            1 partitions
        80378 total reads
           47 merged reads
      2251040 read sectors
       665664 milli reading
     28418044 writes
      8633788 merged writes
   1085482896 written sectors
    150980548 milli writing
            0 inprogress IO
        37475 milli spent IO

3.     To display reports with memory sizes reported in megabytes.you choose units  (k, K, m, M) default is K (1024 bytes) in the default mode.

[root@liniuxbiginner ~]# vmstat -S m 3 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0    142    162    198    0    0     0   103    5    2  0  0 99  1  0

 


Wednesday 22 June 2016

vipw -s & vigr -s Command use in Linux & Ubuntu for editing /etc/passwd, shadow, and group files / The best way to edit /etc/passwd, shadow, and group files.



The best way to edit /etc/passwd, or shadow or group file is to use vipw command. Traditionally (under UNIX and Linux) if you use vi to edit /etc/passwd file and same time a user try to change a password while root editing file, then the user’s change will not entered into file. To avoid this problem and to put a lock while editing file, use vipw and vigr command which will edit the files /etc/passwd and /etc/group respectively. If you pass -s option to these command, then they will edit the shadow versions of those files i.e. /etc/shadow and /etc/gshadow, respectively.

The main purpose of locks is to prevent file corruption. Do not use vi or other text editor to edit password file. Syntax:

  • vipw -s : Edit /etc/passwd file
  • vigr -s : Edit /etc/group file
Where,
  • -s : Secure file editing
An example :-

Login as a root user:


 [root@linuxbiginner ~]# vipw -s 


On other terminal login as normal user (for example anurag) and issue command passwd to change anurag’s password:

[anurag@linuxbiginner ~]# passwd
Changing password for user anurag.
Changing password for anurag.
(current) UNIX password:
New password:
Retype new password:
passwd: authentication tokens lock busy.

As you see it returned with an error “passwd: Authentication token lock busy”

This will avoid /etc/shadow file corruption.

Wednesday 15 June 2016

Linux and Unix pwd (Print Working Directory) command


 What is pwd?

pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal.

Basic syntax of pwd:

 pwd [OPTION]...

Options used with pwd
-L --logical         If the contents of the environment variable PWD provide an absolute name of the 
                     current directory with no "." or ".." components, then output those contents, 
                     even if they contain symbolic links. Otherwise, fall back to default -P handling.


-P --physical        Print a fully resolved name for the current directory, in which all components of
                     the name are actual directory names, and not symbolic links).

--help               Display a help message, and exit.

--version            Display version information, and exit.

This article aims at providing you a deep insight of Linux command ‘pwd‘.

1. Print your current working directory.

anurag@linuxgosolution:~$ /bin/pwd

/home/anurag

2. Print version of your ‘pwd’ command.

anurag@linuxgosolution:~$ /bin/pwd --version

pwd (GNU coreutils) 8.24
Packaged by Cygwin (8.24-3)
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.


3.
What is the absolute path (starting from /) of the pwd binary file.

/bin/pwd

4. What is the absolute path (starting from /) of the pwd source file.

/usr/include/pwd.h 


Tuesday 14 June 2016

Changing The Time Zone In Linux (Command Line)

There are several different ways to manage time in Linux. This quick tip will show you how to quickly change the local time to the correct time zone for the server. In this Linux tip I’ll show you how to change the localtime to your (or a) current time zone.
Location of the local time file
Linux looks at /etc/localtime to determine the current time of your machine. This can either be a symbolic link to the correct time zone or a direct copy of the time zone file.


Timezone files are located in /usr/share/zoneinfo/
For this tip we will assume your server is located in Asia and will be under the Kolkata  zone.
I change the Linux time zone by copying or making a symbolic link to from /usr/share/zoneinfo/Asia/Kolkata to /etc/localtime
For RHEL 6
1) cp /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
2) ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
For RHEL 7 & Ubuntu 14.04
1) timedatectl set-timezone Asia/Kolkata
Arun
 

Tuesday 17 May 2016

How to convert .ppk file into .pem file - UBUNTU


If you would like to convert .ppk file into .pem file, the process are given below:
1) Open terminal and make Your login.
2) sudo apt-get install putty-tools.
3) puttygen /var/www/html/abc.ppk -O private-openssh -o /var/www/html/abc.com
4) chmod 400 /var/www/html/abc.prm
 

Saturday 30 April 2016

Password Management in Linux by using passwd command




 A password(commonly knows as passwd in linux) is an unspaced sequence of characters used to determine that a computer user requesting access to a computer system is really that particular user. Typically, users of a multiuser or securely protected single-user system claim a unique name (called a user ID) that can be generally known. In order to verify that someone entering that user ID really is that person, a second identification, the password, known only to that person and to the system itself, is entered by the user. Most networks require that end users change their passwords on a periodic basis.

passwd command
The passwd command is used to create and change the password of a user account. A normal user can run passwd to change their own password, and a system administrator (the superuser ROOT) can use passwd to change another user’s password, or define how that account’s password can be used or changed.
PASSWD SYNTAX

passwd [OPTION] [USER]
Usage: passwd [OPTION...] <accountName>
-k, --keep-tokens keep non-expired authentication tokens
-d, --delete delete the password for the named account (root only)
-l, --lock lock the named account (root only)
-u, --unlock unlock the named account (root only)
-f, --force force operation
-x, --maximum=DAYS maximum password lifetime (root only)
-n, --minimum=DAYS minimum password lifetime (root only)
-w, --warning=DAYS number of days warning users receives before password expiration
(root only)
-i, --inactive=DAYS number of days after password expiration when an account becomes 
disabled (root only)
-S, --status report password status on the named account (root only)
--stdin read new tokens from stdin (root only)

Change the password for Normal user

When you logged in as non-root user like anu in my case and run passwd command then it will reset password of logged in user.
[anu@linuxgosolution ~]$ passwd
Changing password for user anu.
Changing password for anu.
(current) UNIX password:
New password:
Retype new password:
passwd: 
all authentication tokens updated successfully.
When you logged in as root user and run passwd command then it will reset the root password by default and if you specify the user-name after passwd command then it will change the password of that particular user.

Display Password Status Information

To display password status information of a user , use -S option in passwd command.
[root@linuxgosolution ~]# passwd -S anu
anu PS 2016-04-21 0 99999 7 -1 (Password set, SHA512 crypt.)
In the above output first field shows the user name and second field shows Password status (PS = Password Set , LK = Password locked , NP = No Password ), third field shows when the password was changed and last & fourth field shows minimum age, maximum age, warning period, and inactivity period for the password.
we can display password status information for all users at a time by using the option –Sa
root@linuxgosolution:~# passwd -Sa
 

Removing Password of a User

we can remove the password for particular user by using option -d
[root@linuxgosolution ~]# passwd -d anu
Removing password for user anu.
passwd: Success
[root@linuxgosolution ~]#

Lock the password of System User

Use ‘-l‘ option in passwd command to lock a user’s password, it will add “!” at starting of user’s password. A User can’t Change it’s password when his/her password is locked.
[root@linuxgosolution ~]# passwd -l anu
Locking password for user anu.
passwd: Success
 

Unlock User’s Password using -u option

use -u option to unlock the user accounts locked by passwd -l option
[root@linuxgosolution ~]# passwd -u anu
Unlocking password for user anu.
passwd: Success
 

Setting inactive days using -i option

use -i option along with  passwd command to set inactive days for a system user. This will come into the picture when password of user  expired and user didn’t change its password in ‘n‘ number of days ( i.e 7 days in my case)  then after that user will not able to login.
[root@linuxgosolution ~]# passwd -i 7 anu
Adjusting aging data for user anu.
passwd: Success
[root@linuxgosolution ~]# passwd -S anu
anu PS 2016-04-21 0 99999 7 7 (Password set, SHA512 crypt.)
[root@linuxgosolution ~]#
 

Setting Minimum No.of Days to Change Password using passwd -n option

Using the option -n along with passwd command we can set the minimum number of days to change the password. A value of zero shows that user can change it’s password in any time.
[root@linuxgosolution ~]# passwd -n 90 anu
Adjusting aging data for user anu.
passwd: Success
[root@linuxgosolution ~]# passwd -S anu
anu PS 2016-04-21 90 99999 7 7 (Password set, SHA512 crypt.)
[root@linuxgosolution ~]#

Setting the  Warning days before password expire using passwd -w option

Using the option -w along with passwd can be used to set the warning days before the password expires.
[root@linuxgosolution ~]# passwd -w 30 anu
Adjusting aging data for user anu.
passwd: Success
[root@linuxgosolution ~]# chage -l anu
Last password change                                    : Apr 21, 2016
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 90
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 30
[root@linuxgosolution ~]#

Thanks
Gurpreet Singh

Thursday 28 April 2016

The Complete Guide to “useradd” Command in Linux – 15 Practical Examples

We all are aware about the most popular command called ‘useradd‘ or ‘adduser‘ in Linux. There are times when a Linux System Administrator asked to create user accounts on Linux  with some specific properties, limitations or comments.
In Linux, a ‘useradd‘ command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. The ‘adduser‘ is much similar to useradd command, because it is just a symbolic link to it.

In some other Linux distributions, useradd command may comes with lightly difference version. I suggest you to read your documentation, before using our instructions to create new user accounts in Linux.
When we run ‘useradd‘ command in Linux terminal, it performs following major things:
  1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created User account.
  2. Creates and populate a home directory for the new user.
  3. Sets permissions and ownerships to home directory.


Basic syntax of command is:
useradd [options] username

Part I – 10 Basic Usage of useradd Commands

1. How to Add a New User in Linux

 

To add/create a new user, all you’ve to follow the command ‘useradd‘ or ‘adduser‘ with ‘username’. The ‘username’ is a user login name, that is used by user to login into the system.
Only one user can be added and that username must be unique (different from other username already exists on the system).
For example, to add a new user called ‘linuxgosolution‘, use the following command.
[root@linuxgosolution ~]# useradd linuxgosolution
When we add a new user in Linux with ‘useradd‘ command it gets created in locked state and to unlock that user account, we need to set a password for that account with ‘passwd‘ command.
[root@linuxgosolution ~]# passwd linuxgosolution
Changing password for user linuxgosolution.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store users information and the entry should be.
linuxgosolution:x:504:504:linuxgosolution:/home/linuxgosolution:/bin/bash
The above entry contains a set of seven colon-separated fields, each field has it’s own meaning. Let’s see what are these fields:
  1. Username: User login name used to login into system. It should be between 1 to 32 charcters long.
  2. Password: User password (or x character) stored in /etc/shadow file in encrypted format.
  3. User ID (UID): Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
  4. Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
  5. User Info: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by ‘finger’ command.
  6. Home Directory: The absolute location of user’s home directory.
  7. Shell: The absolute location of a user’s shell i.e. /bin/bash.

2. Create a User with Different Home Directory

 

By default ‘useradd‘ command creates a user’s home directory under /home directory with username. Thus, for example, we’ve seen above the default home directory for the user ‘tecmint‘ is ‘/home/tecmint‘.
However, this action can be changed by using ‘-d‘ option along with the location of new home directory (i.e./data/projects). For example, the following command will create a user ‘anusha‘ with a home directory ‘/data/projects‘.
[root@linuxgosolution ~]# useradd -d /data/projects gurpreet
You can see the user home directory and other user related information like user id, group id, shell and comments.
[root@linuxgosolution ~]# cat /etc/passwd | grep gurpreet

gurpreet:x:505:505::/data/projects:/bin/bash

3. Create a User with Specific User ID

In Linux, every user has its own UID (Unique Identification Number). By default, whenever we create a new user accounts in Linux, it assigns userid 500, 501, 502 and so on…
But, we can create user’s with custom userid with ‘-u‘ option. For example, the following command will create a user ‘arun‘ with custom userid ‘999‘.
[root@linuxgosolution ~]# useradd -u 999 arun
Now, let’s verify that the user created with a defined userid (999) using following command.
[root@linuxgosolution ~]# cat /etc/passwd | grep arun

arun:x:999:999::/home/navin:/bin/bash
NOTE: Make sure the value of a user ID must be unique from any other already created users on the system.

4. Create a User with Specific Group ID

Similarly, every user has its own GID (Group Identification Number). We can create users with specific group ID’s as well with -g option.
Here in this example, we will add a user ‘tarunika‘ with a specific UID and GID simultaneously with the help of ‘-u‘ and ‘-g‘ options.
[root@linuxgosolution ~]# useradd -u 1000 -g 500 tarunika
Now, see the assigned user id and group id in ‘/etc/passwd‘ file.
[root@linuxgosolution ~]# cat /etc/passwd | grep tarunika

tarunika:x:1000:500::/home/tarunika:/bin/bash

5. Add a User to Multiple Groups

The ‘-G‘ option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.
Here in this example, we are adding a user ‘linuxgosolution‘ into multiple groups like admins, webadmin and developer.
[root@linuxgosolution ~]# useradd -G admins,webadmin,developers linuxgosolution
Next, verify that the multiple groups assigned to the user with id command.
[root@linuxgosolution ~]# id linuxgosolution

uid=1001(linuxgosolution) gid=1001(linuxgosolution)
groups=1001(linuxgosolution),500(admins),501(webadmin),502(developers)
context=root:system_r:unconfined_t:SystemLow-SystemHigh

6. Add a User without Home Directory

In some situations, where we don’t want to assign a home directories for a user’s, due to some security reasons. In such situation, when a user logs into a system that has just restarted, its home directory will be root. When such user uses su command, its login directory will be the previous user home directory.
To create user’s without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘shilpi‘ without a home directory.
[root@linuxgosolution ~]# useradd -M shilpi
Now, let’s verify that the user is created without home directory, using ls command.
[root@linuxgosolution ~]# ls -l /home/shilpi

ls: cannot access /home/shilpi: No such file or directory

7. Create a User with Account Expiry Date

By default, when we add user’s with ‘useradd‘ command user account never get expires i.e their expiry date is set to 0 (means never expired).
However, we can set the expiry date using ‘-e‘ option, that sets date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time.
Here in this example, we create a user ‘gurpreet‘ with account expiry date i.e. 27th April 2014 in YYYY-MM-DDformat.
[root@linuxgosolution ~]# useradd -e 2014-03-27 gurpreet
Next, verify the age of account and password with ‘chage‘ command for user ‘gurpreet‘ after setting account expiry date.
[root@linuxgosolution ~]# chage -l gurpreet

Last password change      : Mar 28, 2014
Password expires      : never
Password inactive      : never
Account expires       : Mar 27, 2014
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires  : 7

8. Create a User with Password Expiry Date

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.
Here in this example, we will set a account password expiry date i.e. 45 days on a user ‘linuxgosolution’ using ‘-e‘ and ‘-f‘ options.
[root@linuxgosolution ~]# useradd -e 2014-04-27 -f 45 linuxgosolution

9. Add a User with Custom Comments

The ‘-c‘ option allows you to add custom comments, such as user’s full name, phone number, etc to/etc/passwd file. The comment can be added as a single line without any spaces.
For example, the following command will add a user ‘gurpreet‘ and would insert that user’s full name, Gurpreet Singh, into the comment field.
[root@linuxgosolution ~]# useradd -c "Gurpreet Singh" gurpreet
You can see your comments in ‘/etc/passwd‘ file in comments section.
[root@linuxgosolution ~]# tail -1 /etc/passwd

gurpreet:x:1006:1008:Gurpreet Singh:/home/gurpreet:/bin/sh

10. Change User Login Shell:

Sometimes, we add users which has nothing to do with login shell or sometimes we require to assign different shells to our users. We can assign different login shells to a each user with ‘-s‘ option.
Here in this example, will add a user ‘linuxgosolution‘ without login shell i.e. ‘/sbin/nologin‘ shell.
[root@linuxgosolution ~]# useradd -s /sbin/nologin linuxgosolution
You can check assigned shell to the user in ‘/etc/passwd‘ file.
[root@linuxgosolution ~]# tail -1 /etc/passwd

linuxgosolution:x:1002:1002::/home/linuxgosolution:/sbin/nologin
Part II – 5 Advance Usage of useradd Commands

11. Add a User with Specific Home Directory, Default Shell and Custom Comment

The following command will create a user ‘ravi‘ with home directory ‘/var/www/linuxgosolution‘, default shell /bin/bashand adds extra information about user.
[root@linuxgosolution ~]# useradd -m -d /var/www/ravi -s /bin/bash -c "Linuxgosolution Owner" -U ravi
In the above command ‘-m -d‘ option creates a user with specified home directory and the ‘-s‘ option set the user’s default shell i.e. /bin/bash. The ‘-c‘ option adds the extra information about user and ‘-U‘ argument create/adds a group with the same name as the user.

12. Add a User with Home Directory, Custom Shell, Custom Comment and UID/GID

The command is very similar to above, but here we defining shell as ‘/bin/zsh‘ and custom UID and GID to a user ‘tarunika‘. Where ‘-u‘ defines new user’s UID (i.e. 1000) and whereas ‘-g‘ defines GID (i.e. 1000).
[root@linuxgosolution ~]# useradd -m -d /var/www/tarunika -s /bin/zsh -c "linuxgosolution Technical Writer" -u 1000 -g 1000 tarunika

13. Add a User with Home Directory, No Shell, Custom Comment and User ID

The following command is very much similar to above two commands, the only difference is here, that we disabling login shell to a user called ‘avishek‘ with custom User ID (i.e. 1019).
Here ‘-s‘ option adds the default shell /bin/bash, but in this case we set login to ‘/usr/sbin/nologin‘. That means user ‘avishek‘ will not able to login into the system.
[root@linuxgosolution ~]# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c "linuxgosolution Sr. Technical Writer" -u 1019 avishek

14. Add a User with Home Directory, Shell, Custom Skell/Comment and User ID

The only change in this command is, we used ‘-k‘ option to set custom skeleton directory i.e. /etc/custom.skell, not the default one /etc/skel. We also used ‘-s‘ option to define different shell i.e. /bin/tcsh to user ‘navin‘.
[root@linuxgosolution ~]# useradd -m -d /var/www/navin -k /etc/custom.skell -s /bin/tcsh -c "No Active Member of linuxgosolution" -u 1027 navin

15. Add a User without Home Directory, No Shell, No Group and Custom Comment

This following command is very different than the other commands explained above. Here we used ‘-M‘ option to create user without user’s home directory and ‘-N‘ argument is used that tells the system to only create username (without group). The ‘-r‘ arguments is for creating a system user.
[root@linuxgosolution ~]# useradd -M -N -r -s /bin/false -c "Disabled linuxgosolution Member" clayton

 

 Thanks