Basic Linux TutorialIntroduction – What is it? Why to learn? Linux installation directory structures Boot process Run levels in Linux Desktop Environments Different shells BASH Internal and External Commands Basic Linux Commands Important files and directories in Linux Environmental and Shell Variables Command history in Linux Character classes in Linux Text editors vim nano Searching files Creating new files Viewing File Contents File commands File permissions and ownership WildCards (Globbing) in files File compression Directory commands xargs command in Linux Comparing files Searching patterns using grep command Translating the characters using tr command Extracting data using cut command Stream editing using sed command Data extraction and reporting using awk command Sorting the file or string input uniq command in Linux Difference between grep, tr, cut, sed and awk commands Hardware commands Hard disk and memory space commands Working with Processes Managing Jobs Working with cron jobs Service command in Linux Network commands Managing Users and Groups Other Popular commands Standard streams and Redirection Pipes Package Managers in Linux
users and groups in linux shellLinux is a multi-user operating system. It means that many user can work on the system simultaneously. You can find out user information from /etc/passwd file. Each Linux user should belong to a specific group. You can find out groups information from /etc/group file. Here is the snapshot of typical passwd and group file in Linux. passwd-file-contents passwd-file-contents Each line in passwd file contains below information.
group-file-in-linux group-file-in-linux Each line in group file contains below information.
Below is the list of commands to manage users and groups in linux system.
- useradd – add new user into the system
- userdel – delete the specific user from the system
- usermod – modify the user details
- groupadd – add new group in the system
- groupdel – delete specific group from the system
- groupmod – modify the group details
- passwd – change or set the password for given user.
- finger user – Prints details about the user. Please note that you will to install finger package for this.
- whoami – gives name of the user currently logged into the system
- id – shows the user id, group id and group details for given user.
- Setting up new user in Linux
sudo useradd shaun sudo passwd shaun
Modifying user in LinuxYou can modify user using usermod command. This command has many options that allow you to update the user.
You can also use multiple options in single line.
usermod -d /home/newdir/ shaun -> New home directory for the user shaun usermod -e 2017-12-30 shaun -> Set the expiry date for the user shaun usermod -g newg shaun -> change the primary group of user shaun usermod -aG secondaryg shaun -> append new group to user shaun usermod -l mark shaun -> change login name of shaun to mark usermod -L shaun -> lock user shaun usermod -U shaun -> unlock user shaun usermod -d /var/shaun/ -m shaun ->move home directory of shaun to new location usermod -s /bin/sh shaun -> Change the default shell of user shaun usermod -u 777 shaun -> Change the user id of shaun
If you want to view the expiry date of user account, you can use below command.
usermod -d /home/kk/ -s /bin/bash -e 2018-02-22 -c “This is abc” -u 4 -aG g1 shaun usermod -u 111 -g 444 shaun
Note that you can change the account expiry details using chage command as well. For example, if you want to change the account expiry of user shaun, you can use below command.
chage -l shaun
Deleting the user using userdel command in LinuxYou can delete a user using below command.
sudo userdel <username>
passwd command in LinuxWhen you execute passwd command, you will have to enter current password. If you want to change the password for other user, you can use below command. Note that you should be a root user to be able to change password of other users.
A root user can also disable the password of other users using below command.
If you want to change the passwords of multiple users at the same time, you can use below command followed by list of user name and their passwords.
passwd -d <username>
id command in Linuxid command will display your user id, group id and the groups you belong to. If you want to see other user details you can use below syntax.
group commands in LinuxYou can use below commands to manage groups in Linux. To add new group say “g1”, you can use below command.
To rename the group from say “g1” to “newg1”, you can use below command.
sudo groupadd g1
To change the id of group, you can use below command. Here we are changing the id of group g1 to 1010
groupmod -n newg1 g1
To delete the group say “newg1”, you can use below command.
groupmod -g 1010 g1
sudo groupdel newg1
Switching user accounts“su” command is used to switch user account. Switch to a different user account using su command. Super user can switch to any other user without entering their password. sudo is similar command but only allows execution of one command at a time with special privileges.
Complex problems, Simple Solutions