Get Linux User Account Info Using Bash Script

Thanks to http://www.thegeekscope.com

This article provides a bash script to get the information for a Linux user account. Script accepts the username as input and displays the following information about the user.

  • User Info
  • User Groups
  • User’s home directory and default shell
  • Total Size occupied by user’s home directory
  • User password and account expiry information

This script has been tested on RHEL5.3.

Script to Get Linux User Account Info

# cat get_user_info.sh
#!/bin/bash
#
# Script Name: get_user_info.sh
# http://www.thegeekscope.com/script-to-get-linux-user-info/ 
#        
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
# Copyright www.thegeekscope.com
# Author: Kam Aggarwal, (kam@thegeekscope.com)
# Read the terms of use at http://www.thegeekscope.com/terms-of-use/
#

if [ "$1" = "" ]
then
        echo
        echo "Usage: $0 USERNAME"
        echo
        echo "Example:  $0 kam"
        echo

        exit 1
fi

Username=`cat /etc/passwd | grep -Ew ^$1 | cut -d":" -f1`

if [ "$Username" = "" ]
then
        echo "Username $1 doesn't exist"
        exit 2
fi

Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`
UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4`
UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1`
UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5`
UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6`
UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7`
UserGroups=`groups $Username | awk -F": " '{print $2}'`
PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '{print $2}'`
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk -F": " '{print $2}'`
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{print $2}'`
HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`

echo
printf "%-25s : %5s  [User Id - %s]\n" "Username" "$Username" "$Userid"
printf "%-25s : %5s\n" "User Info" "$UserInfo"
echo
printf "%-25s : %5s  [Group Id - %s]\n" "User's Primary Group" "$UserPrimaryGroup" "$UserPrimaryGroupId"
printf "%-25s : %5s\n" "User is Member of Groups" "$UserGroups"
echo
printf "%-25s : %5s  [Size Occupied - %s]\n" "Home Directory" "$UserHomeDir" "$HomeDirSize"
printf "%-25s : %5s\n" "Default Shell" "$UserShell"
echo
printf "%-25s : %5s\n" "Last Password Changed On" "$LastPasswordChangeDate"
printf "%-25s : %5s\n" "Password Expiry Date" "$PasswordExpiryDate"
printf "%-25s : %5s\n" "Account Expiry Date" "$AccountExpiryDate"
echo

Script Usage

Download the script get_user_info.sh and transfer it to your Linux system. On my Linux system, this script resides in /root folder.

Provide executable permission to the script.

# chmod 755 /root/get_user_info.sh

You are ready to use the script. Following examples demonstrate the script usage.

Get Information of Linux User Kam

# sh get_user_info.sh kam

Username                  : kam  [User Id - 502]
User Info                 : Kam Aggarwal

User's Primary Group      : kam  [Group Id - 502]
User is Member of Groups  : kam root wheel bob

Home Directory            : /home/kam  [Size Occupied - 80K]
Default Shell             : /bin/bash

Last Password Changed On  : Sep 12, 2012
Password Expiry Date      : Oct 13, 2012
Account Expiry Date       : Dec 16, 2012

Get Information of Linux User root

# sh get_user_info.sh root

Username                  :  root  [User Id - 0]
User Info                 :  root

User's Primary Group      :  root  [Group Id - 0]
User is Member of Groups  : root bin daemon sys adm disk wheel

Home Directory            : /root  [Size Occupied - 1.3M]
Default Shell             : /bin/bash

Last Password Changed On  : Aug 30, 2012
Password Expiry Date      : never
Account Expiry Date       : never
Advertisements
Posted in Linux
One comment on “Get Linux User Account Info Using Bash Script
  1. Lynnette says:

    What’s up to all, the contents present at this site are actually awesome for people experience, well, keep up the good work fellows.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: