Linux Tools and Utilities: User and Group Management, File System Management, and Job Automation

Linux Tools and Utilities: User and Group Management, File System Management, and Job Automation

Linux is a powerful operating system that provides many useful tools and utilities that can help you manage your system efficiently.

In this article, we will cover some important Linux topics like user management, group management, Linux file system permissions, archiving files in Linux, and job automation.

User Management

User management is an essential part of Linux system administration. Linux allows you to create and manage users and assign them various permissions.

Note: user1, user2, and user3 are sample names of users in the below table

Here are some common commands for managing users in Linux:

Commands

Description

useradd user1

Create user account

passwd user1

Assign user a password

useradd -m user2

Create a user account along with the user's home directory

grep user1 /etc/passwd

Check user account properties

grep user1 /etc/shadow

Check user password properties

su user2

Switch to user2 [ Press ctrl+d to exit from switched user]

usermod -l user2 user3

Change the username of the existing user

cat /etc/passwd

Check the list of users on your system

userdel user1

Delete user account

Group Management

In Linux, groups are used to manage access to system resources and files. Here are some common commands for managing groups in Linux:

Note: user1, user2, and user3 are sample usernames and group1 is the sample name of a group in the below table

Commands

Description

groupadd group1

Create a group account

gpasswd -a user1 group1

Add a single user to a group

gpasswd -A user1 group1

Make a particular user the group admin

gpasswd -M user1,user2,user3 group1

Add multiple users into a group

gpasswd -d user2 group1

Remove the user from the group

cat /etc/group

Display a list of groups on the system

grep group1  /etc/group

Check particular group account property

grep group1  /etc/gshadow     

Check particular group admin property

groupdel group1

Delete the group

Linux File System Permissions

File system permissions are used to control access to files and directories. There are three types of permissions:

  1. Basic permissions: Basic permissions are the traditional read, write, and execute permissions assigned to the owner, group, and others.

    $ ls -l filename

    [permissions] [link] [owner] [group] [filesize] [date&time_of_file] [Name_of_File]

    Permissions: 10 characters

    -|---|---|--- == Type|OwnerAttributes|GroupAttributes|PublicAttributes

    Type : - | d | c | l : RegularFile|Directory|CharacterFile|symbolicLink Owner_attributes : Read|Write|Execute

    Group_attributes : Read|Write|Execute

    Public_attributes : Read|Write|Execute

    • Let's learn to change the permissions of the file:

      chmod [option] [filename]

Options Type:

  • Character value:

    [Option]---> [ who | what | which ] ---> [ u/g/o/a | -/+/= | r&/w&/x ]

    For example,

    chmod u+rwx filename --------->Grants user with r+w+x access to the file.

    chmod g+rx filename ---------->Grants group with r+x access to the file.

    chmod a+x filename ----------->Grants user+group+others with x access.

  • Numeric value

    [Option]---> [ owner | group | other ] ---> [ 1...7 | 1...7 | 1...7 ]

    For example,

    chmod 700 filename --------->Grants user with r+w+x access to the file.

    chmod 050 filename ---------->Grants group with r+x access to the file.

    chmod 777 filename ----------->Grants user+group+others with r+w+x access.

  1. Special permissions: Special permissions are used to set advanced permissions for files and directories.

    • Let's learn to change the ownership of the file:

      chown [option] [filename] -----------------> Change user ownership

      chgrp [option] [filename] -----------------> Change group ownership

      For example,

  2. Access control list (ACL) permissions: ACL permissions are used to grant specific permissions to individual users or groups.

    If you want to give some read/write/execute permission to an outside user which is not a member of the group created by you. Then this can be done with ACL permissions

    Commands

    Description

    getfacl [file/folder]

    Check ACL permissions of file/folder

    setfacl -m u:user1:rw [file/folder]

    Set permissions for user

    setfacl -m g:group1:rwx [file/folder]

    Set permissions for group

    setfacl -x u:user1: [file/folder]

    Remove permissions for user

    setfacl -x g:group1: [file/folder]

    Remove permissions for group

    setfacl -b [file/folder]

    Remove all ACL permissions

    In this case, 'chaitu' is an outside user and not a member of the 'ubuntu' group. Using ACL permissions 'chaitu' has given 'read+write+execute' access to a file 'backup.tar.gz'.

Archive files in Linux

Archiving is a process of combining multiple files/ in one file. Archiving can be useful for backup purposes, or for transferring multiple files as a single unit.

tar [options] [archive_name.tar] [files/folders]

Options

Description

-c

Create

-x 

Extract

-v 

Verbose

-f 

Forcefully

-t 

For test

-z 

For gzip

-j 

For bz2

-J 

For xz

-C 

For specific location

Here are some common commands for archiving files in Linux:

Commands

Description

tar -cvf [ archive_name.tar ] [files/folders]

Create Archive file

tar -xvf [ archive_name.tar ]

Extract tar archive file on default location

tar -xvf [archive_name.tar] -C [specific_path]

Extract tar archive file on specific location

tar -cvzf [ archive_name.tar.gz ] [ files/folders ]

Create Archive file with compress in size (gzip, bz2, xz)

tar -cvjf [ archive_name.tar.bz2 ] [ files/folders ]

tar -cvJf [ archive_name.tar.xz ] [ files/folders ]

tar -xvzf [ archive_name.tar.gz ] -C [ specific_path ]

Extract tar archive file (gzip, bz2, xz) on specific location

tar -xvjf [ archive_name.tar.bz2 ] -C [ specific_path ]

tar -xvJf [ archive_name.tar.xz ] -C [ specific_path ]

Check file size after you create archives: du -sh [ filename ]

Job Automation in Linux

Job automation in Linux is the process of automating repetitive tasks, jobs, or processes in Linux operating systems. Common tools used for job automation in Linux are

  • 'at' command

  • 'cron' scheduler

  1. Using 'at' command------->

    at [ hh:mm ] [ month ] [ date ] ------------------------> Creates at job

    _____________________________________________________________________________________

    For example:

    at 4:40 Mar 27

    warning: commands will be executed using /bin/sh at Mon Mar 27 04:40:00 2023

    tar -czf backup.tar.gz /home/ubuntu/*

    press ctrl+d

    _____________________________________________________________________________________

    atq ------------------------>Shows pending jobs

    atrm [ job_id ] ------------------------>Removes job

    To Restrict a particular user from accessing at utility edit at.deny file>>>>Just add username and save the file

    _____________________________________________________________________________________

    vim /etc/at.deny

    krishiva

    :wq

    _____________________________________________________________________________________

    krishiva:~$ at 10:37 Mar 20

    You do not have permission to use at.

    _____________________________________________________________________________________

  2. Using 'cron' scheduler------->

    You can create cron jobs using the cron scheduler Cron is a time-based job scheduler in Linux that allows you to schedule jobs to run at specific times or intervals.

    Check cron package availability in the ubuntu system

    $ dpkg -s cron | grep

    Status Status: install ok installed

    $ service cron start ------------------------>start your cron service

    $ systemctl enable cron.service ------------- >add to startup program

    $ service cron status ----------------------->shows cron service status

    Following are commands for crontab utility:

    $ crontab -e ----------------------->set a cron job

    For example:

    crontab -e

    ______________________________________________

    0 0 * * * tar -czf backup.tar.gz /home/ubuntu/*

    :wq

    ______________________________________________

    This crontab will create a compressed backup archive of all files and directories in the /home/ubuntu directory every day at midnight

    $ crontab -l ----------------------->list cronjobs of current usr

    $ crontab -r ----------------------->remove cron jobs

    Or go to crontab -e file and remove cronjob lines

    $ crontab -u chaitu -e ----------------------->set a cron job for other usr

    $ crontab -u chaitu -l ----------------------->list cronjobs of other usr

    $ service rsyslog restart --------------------->Start rsyslog service to check logs

    $ tail -f /var/log/syslog ----------------------->check crontab log file

    $ tail -f /var/log/syslog | grep CRON------------->check cron log entries only

    To Restrict particular users from accessing at cron service edit cron.deny file>>>>Just add the username and save the file. _____________________________________________________________________________________

    vim /etc/cron.deny

    krishiva

    :wq

    _____________________________________________________________________________________

    krishiva:~$ crontab -e

    You do not have permission to use crontab.

    _____________________________________________________________________________________

    Summary:

    In this blog, we covered important topics like user management, group management, Linux file system permissions, archiving files in Linux, and job automation. I hope this blog has been informative and helpful to you.

    Stay tuned for my next blog on "Linux Advanced Features". I will keep sharing my learnings and knowledge here with you.

    Let's learn together! I appreciate any comments or suggestions you may have to improve my Linux blog.

    Thank you,

    Chaitannyaa Gaikwad

Did you find this article valuable?

Support Chaitannyaa Gaikwad by becoming a sponsor. Any amount is appreciated!