Live as if you were to die tomorrow. Learn as if you were to live forever.

Sunday 22 February 2015

Information about crontab in Linux

Crontab stands for corn table. Crontab is used to perform the specific task at given time. Using crontab we can schedule the job to run on particular time. Crontab runs the jobs in the background.

The three mains things in crontab server are

    1)      The crontab contains crond daemon
    2)      The /etc/init.d/crond contains the initscript to start or stop the crond server
    3)      The system crontab file is /etc/crontab

The crontab contains 6 fields

        *             *             *             *             *             script_name/command               
1              2              3            4              5                              6

column
Field
Allowed Values
1
Minute
0-59
2
Hour
0-23
3
Day of Month
1-31
4
Month
1-12 or Names
5
Day of Week
0-7(0 or 7 is Sunday) or Names
6
Script
Path of script or command

Time related fields contains

(i)                  *     which matches any value
(ii)                A single integer which matches exactly
(iii)               Two integers separated by dash (-) matches the range of value
Suppose, the value is 4-6 in hour field which match 4am, 5am and 6am
(iv)              A comma (,) separated by series of integers or ranges matches any listed value i.e. 3,5,7-9 matches 3am, 5am, 7am, 8am and 9am
(v)                */2 in hour field which means cron job execute at midnight, 2am, 4am and so forth. i.e. cron executes at after every 2 hours
(vi)              0-10/2 in hour field which means cron job executes at midnight, 2am, 4am, 6am, 8am and 10am

Note:    Any line that begins with # is the comment

Cron access Permissions

There are two important files for cron access

(i)                  cron.allow
(ii)                cron.deny  

(i)                  cron.allow: If this file is present then only users listed in it can use crontab and all other users are denid.

(ii)                cron.deny: If this file is present then only users listed in it cannot use the crontab and all other users are allowed and can use the crontab

Note:    If cron.deny or cron.allow is touched (created a blank file), then no users are allowed to create a crontab except root user.

Some other files in crontab are

/etc/cron.hourly             
/etc/cron.daily                                  run-parts script executes the scripts in 
/etc/cron.weekly                              these directory on hourly, daily, weekly and monthly basis.
/etc/cron.monthly

See the Examples of cron job.

Guys… Please comment if you have any query or feedback…. :)

Thursday 19 February 2015

Some important questions on users and groups




How to disable/lock the user with the help of command and using shadow file ?

Lock account using command

We can lock the user using usermod command with the help of L to lock option and U to unlock the account.

#usermod -L test
It will lock the user test

#usermod -U test
It will unlock the user test

Lock and Unlock account using file

#vi /etc/shadow

test:$6$xb7FL2gXeJ9VbVP1$dRbWMmySxrDR8Kb8VQCfIg9IYlf1h72I84/cjqAiy2gR.VcWZeia4J/RzZXEHYNCAsPq4xDBmlgLo31Qsi2aP/:16461:0:99999:7:::

change file as shown below
#vi /etc/shadow

test:!$6$xb7FL2gXeJ9VbVP1$dRbWMmySxrDR8Kb8VQCfIg9IYlf1h72I84/cjqAiy2gR.VcWZeia4J/RzZXEHYNCAsPq4xDBmlgLo31Qsi2aP/:16461:0:99999:7:::
:wq

In this way you can lock the user account with the help of file. In the same way you can unlock the account for that you have to remove the ! in second column.

How to remove the password of the user using command and using shadow file?

We can use passwd command with -d option to remove the password of the user.
#passwd -d test

Remove password using file

Open file /etc/shadow in vi editor and remove the second column
#vi /etc/shadow

test:$6$xb7FL2gXeJ9VbVP1$dRbWMmySxrDR8Kb8VQCfIg9IYlf1h72I84/cjqAiy2gR.VcWZeia4J/RzZXEHYNCAsPq4xDBmlgLo31Qsi2aP/:16461:0:99999:7:::

Change as shown below
#vi /etc/shadow

test::16461:0:99999:7:::
:wq

Here, we have just removed the second column. So the system will not prompt for password for that user.

How to change the login shell of the user?

You can use the usermod command to change the login shell of the user with the help of -s option

#usermod -s /sbin/nologin

We can also edit the /etc/passwd file and add /sbin/nologin instead of /bin/bash

How to check total number users in the system?

[root@server Desktop]# cat /etc/passwd  | wc -l
43

It will show the count of all the users in the system.

Use below command to see the users having login shell bash

[root@server Desktop]# cat /etc/passwd  | grep bash$
root:x:0:0:root:/root:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
test:x:500:500::/home/test:/bin/bash
dnyaneshwar:x:501:500::/home/dnyaneshwar:/bin/bash
pramod:x:502:500::/home/pramod:/bin/bash
vinod:x:503:500::/home/vinod:/bin/bash

Here, we have used grep command to check the bash shell. $ is used to check the bash word at the end of the line. So it will print all the lines which contains bash at end of each line.

Guys, Please comment if you have any query or feedback…. :)

Wednesday 18 February 2015

Information about /etc/group file in Linux/Unix


The /etc/group file contains the list of group along with the users associated with the group.


The /etc/group file contains group, group id, x which means password is stored in /etc/gshadow file and list of the users associated with the group.


The /etc/group file contains the information as shown below


[root@red /]# cat /etc/group

root:x:0:root

bin:x:1:root,bin,daemon

daemon:x:2:root,bin,daemon

sys:x:3:root,bin,adm

adm:x:4:root,adm,daemon

disk:x:6:root

wheel:x:10:root

mail:x:12:mail,postfix

test:x:500:

dnyaneshwar:x:501:

pramod:x:502:dnyaneshwar,test


This file contains four fields separated by colon (:)


1st field:    It contains the group name

2nd field:  It shows x which means password is stored in encrypted format in /etc/gshadow file

3rd field:  It contains the group id

4th field:  It contains the list of the users in the group.


If you see the below example:


pramod:x:502:dnyaneshwar,test


Here, pramod is the group name, x means password of group pramod is stored in /etc/gshadow file, 502 is the group id and dnyaneshwar, test are the users in the group.


Whenever you create the user it will automatically crate the group for that user.


How to add new user to the group?


To add new user to the group you can use usermod command.

#usermod -G group_name users


For e.g.

#usermod -G pramod vinod


Output

pramod:x:502:dnyaneshwar,test,vinod


Here, we have added the user vinod in group pramod


How to remove the user from group ?


We can use gpasswd command to remove the user from the group

#gpasswd –d username groupname


For e.g.

#gpasswd –d test pramod

Removing user test from group pramod



How the change check the primay group of the user and change it?


The primary group for any user is the username itself.

If you have created the user pramod then primary group for user pramod is the pramod itself.


Use the below command to check the primary group of the user


# groups pramod

pramod : pramod



To change primary group use the below command

# usermod -g test pramod

# groups pramod

pramod : test



Note: G is used to change the secondary group and g is used to change the primary group


Guys, Please add comment if you have any query or feedback.... :)