Thursday, December 2, 1999

L I N U X - Simple But Important Command

Check OS & OS Version:

You can use any one of the following method to find out your Linux distribution and name:
a] /etc/*-release file.
b] lsb_release command [$ lsb_release -a].
c] /proc/version file.

How Do I Find Out My Kernel Version?
$ uname -a
$ uname -mrs
Sample outputs:
Linux - Kernel name
2.6.32-5-amd64 - Kernel version number
x86_64 - Machine hardware name (64 bit)

bash$ uname -srv
Linux 3.3.0-gentoo #2 SMP PREEMPT Wed Mar 21 02:07:10 CDT 2012
The first part prints out the kernel name, which is Linux in the above example. The second part is the kernel release version, which is 3.3.0-gentoo. The rest of it is a more detailed kernel information like the compilation date and config.
bash$ uname -mnipo
Output: machinename i686 Intel(R) Core(TM)2 Duo CPU E6850 @ 3.00GHz GenuineIntel GNU/Linux
machinename is the name of the machine, while the rest is the processor architecture, processor type, version, speed and operating system information.
You can also use the -a option which prints out all the available information about the kernel and the machine.
Type the following command to see kernel version and gcc version used to build the same:
$ cat /proc/version

List all Users:

$ cat /etc/passwd
$ more /etc/passwd
$ less /etc/passwd
$ awk -F':' '{ print $1}' /etc/passwd

A Note About System and General Users: Each user has numerical user ID called UID. It is defined in /etc/passwd file. The UID for each user is automatically selected using /etc/login.defs file when you use useradd command. To see current value, enter:
$ grep "^UID_MIN" /etc/login.defs
$ grep UID_MIN /etc/login.defs

1000 is minimum values for automatic uid selection in useradd command. In other words all normal system users must have UID >= 1000 and only those users are allowed to login into system if shell is bash/csh/tcsh/ksh etc as defined /etc/shells file. Type the following command to list all login users:
## get UID limit ##
l=$(grep "^UID_MIN" /etc/login.defs)
## use awk to print if UID >= $UID_LIMIT ##
awk -F':' -v "limit=${l##UID_MIN}" '{ if ( $3 >= limit ) print $1}' /etc/passwd

To see maximum values for automatic uid selection in useradd command, enter:
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd
$ grep "^UID_MAX" /etc/login.defs

In other words all normal system users must have UID >= 1000 (MIN) and UID <= 60000 (MAX) and only those users are allowed to login into system if shell is bash/csh/tcsh/ksh etc as defined /etc/shells file. Here is an updated code:
## get mini UID limit ##
l=$(grep "^UID_MIN" /etc/login.defs)
## get max UID limit ##
l1=$(grep "^UID_MAX" /etc/login.defs)
## use awk to print if UID >= $MIN and UID <= $MAX   ##
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd

/sbin/nologin is used to politely refuse a login i.e. /sbin/nologin displays a message that an account is not available and exits non-zero. It is intended as a replacement shell field for accounts that have been disabled or you do not want user to login into system using ssh. To filter /sbin/nologin, enter:
#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user accounts in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite <www.cyberciti.biz>, under GPL v2.0+
# -----------------------------------------------------------------------------------
_l="/etc/login.defs"
_p="/etc/passwd"
## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)
## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin   ##
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) "$_p"

Finally, this script lists both system and users accounts:
#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user and system accounts in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite <www.cyberciti.biz>, under GPL v2.0+
# -----------------------------------------------------------------------------------
_l="/etc/login.defs"
_p="/etc/passwd"
## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)
## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin   ##
echo "----------[ Normal User Accounts ]---------------"
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p"
echo ""
echo "----------[ System User Accounts ]---------------"
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 <= max  && $7 != "/sbin/nologin")) print $0 }' "$_p"

Get Current User:

a] $USER - Current user name. 
$ echo "$USER"
u="$USER"
echo "User name $u"
b] $USERNAME - Current user name.
c] id command - Current user name.[$ id -u -n]
#!/bin/bash
_user="$(id -u -n)"
_uid="$(id -u)"
echo "User name : $_user"
echo "User name ID (UID) : $_uid"
- - - - - - - - - - - - - - - - - - - - - - - 
# Make sure only root user can run the following script:
#!/bin/bash 
## get UID 
uid=$(id -u)
## Check for it
[ $uid -ne 0 ] && { echo "Only root may enable the nginx-chroot environment to the system."; exit 1; }
## Continue main logic with root user
- - - - - - - - - - - - - - - - - - - - - -
A note about $EUID:This variable EUID is readonly. It expands to the effective user ID of the current user, initialized at shell startup. You can use $EUID to find out if user is root or not with the following syntax:
# Find out if you are root or not for admin tasks.
(( EUID )) && { echo 'Run this script with root priviliges.'; exit 1; } || echo 'Running as root, starting service...'

Add Group & User:

You can use the useradd or usermod commands to add a user to a group. The useradd command creates a new user or update default new user information. The usermod command modifies a user account and it is useful to add user to existing groups. There are two types of groups under Linux operating systems:
  1. Primary user group.
  2. Secondary or supplementary user group.
All user account related information are stored in the following files:
  • /etc/passwd - Contains one line for each user account.
  • /etc/shadow - Contains the password information in encrypted formatfor the system's accounts and optional account aging information.
  • /etc/group - Defines the groups on the system.
  • /etc/default/useradd - This file contains a value for the default group, if none is specified by the useradd command.
  • /etc/login.defs - This file defines the site-specific configuration for the shadow password suite stored in /etc/shadow file.
You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:
# useradd -G {group-name} username
$ grep developers /etc/group # make sure developers group exists, or add developers Group
# If you do not see any output then you need to add group developers using the groupadd command:
$ groupadd developers
# Next, add a user called dixit to group developers:
$ useradd -G developers dixit
# Setup password for user dixit:
$ passwd dixit
# Ensure that user added properly to group developers:
$ id dixit
# Output: uid=1122(dixit) gid=1125(dixit) groups=1125(dixit),1124(developers)
# Please note that capital G (-G) option add user to a list of supplementary groups.
$ useradd -G admins,ftp,www,developers dixit
# Add existing user dixit to ftp supplementary/secondary group with the usermod command using the -a option ~ i.e. add the user to the supplemental group(s). Use only with -G option:
$ usermod -a -G ftp dixit
# In this example, change tony user's primary group to www, enter:
$ usermod -g www dixit


Option                          Purpose
-a
--append                 Add the user to the supplementary group(s). Use only with the -G option.
-g GROUP
--gid GROUP                 Use this GROUP as the default group.
-G GRP1,GRP2
--groups GRP1,GRP2 Add the user to GRP1,GRP2 secondary group.

Give Sudo Access to User

Run visudo as root and add following line:
dixit ALL = (root) ALL # this line give sudo access to root for user 'dixit'.
dixit ALL = (root) /bin/kill, /bin/ps # this line give root access to user 'dixit'.
#### Root user spec - following line give all access to user 'root'.
root ALL = (ALL) ALL

Edit /etc/sudoers file either manually or using visudo application. Remember: System reads /etc/sudoers file from top to the bottom, so you could overwrite particular setting by putting next one below. So to be on the safe side - define your access setting at the bottom.
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
To add a user to the group you should run (as root):
# usermod -a -G groupname username
where groupname is your group (developers, admin) and username is the username (eepuser, dixit).

# Find occurrence of word in a file:
$ grep -c word file # [grep -c 'java.util.ConcurrentModificationException' ddp-mediation-ms-ddp-po-3p.log*]
$ grep -ic word file # case insensitive

# Count multiple occurrences of the word in a single line:
$ cat filename | grep -o 'word' | wc -l
$ cat ddp-mediation-rte_ddp_1_1.log | grep -o '201402090032218553' | wc -l
$ cat ddp-mediation-rte_ddp_1_1.log | grep -o 'INFO' | wc -l

# Find line number in a text file - without opening the file:
$ grep -C 2 yourSearch yourFile
$ grep -C 2 yourSearch yourFile > result.txt

# $ grep -n -2 your_searched_for_string  your_large_text_file
Will give you almost what you expect
-n : tells grep to print the line number
-2 : print 2 additional lines (and the wanted string, of course)