Hello Readers,
Today I want to share with you my learnings of Advanced Linux on certain topics like creating a group, adding users to it, modifying the permissions of the file/directories, using various commands like grep, awk and find, its uses in DevOps on a daily basis, ssh/scp and systemctl.
Creating a user
#creating a user and its directory too.
useradd -m <username>
#just creating a user
useradd <username>
# changes password of a user
sudo passwd <username>
# deleting a user
sudo userdel <username>
Creating a group
Before we jump on creating a group let's understand that though there is a need to create groups but -
Let's understand with an example, suppose you run a software development company where you have different departments and each department as a whole works to run a particular software. You want that each department can only access its data so that other data is not shared or harmed by any means. Here comes a concept of grouping. If you group all the users of respective departments separately it will be a lot easier for them to work and this is the prime reason why grouping is done.
So now the question comes how we can do that? For that, you need to on your Linux system you need to run the command groupadd
#to create a group
sudo groupadd your_group_name
#to delete a group
sudo groupdel <group name>
# You can also check the configuration file that stores information about user groups on the system
cd /etc/group
#add multiple member in a group
sudo gpasswd -M <username1>..<username n> <group name>
Once the group has been created the question arises of how to add users to that group.
Once you have created a group in Ubuntu, you can add one or more users to one group using the usermod
command:
# adding a user to group
sudo usermod -aG your_group_name username
#Removing a user from a group, write the name of the groups you want to keep you user into excluding the group you dont want to keep
sudo usermod -G group1,group2 username
#Use the gpasswd command with the -d option followed by the username and the group from which you want to remove the user.
sudo gpasswd -d username groupname
Note: Here 'a' is for add or append, and 'G' is for specifying that the next argument after it will be the name of the group to which the user should be added.
When the -G
option is used without the -a
option, which means you are replacing the user's existing group memberships with the specified group(s).
Also, I want to share an important thing that I learnt while writing this blog.
Just before this, I told you that you can add usermod
to add users in a group, But there is one more command that we can use to add users to a group gpasswd
so let's understand the basic difference between them-
Feature | gpasswd | usermod |
Purpose | Basically, it is used for group password management and group membership control. | User account modification, including group membership control. |
Function | Set/change group password. Add/remove users from a group. Assign group administrators. | Modify various user account settings, including group membership. |
Required Privilege | Typically requires superuser (root) privileges for password changes and adding/removing users. | Typically requires superuser (root) privileges for modifying user accounts. |
Group Password | Can set/change the group password. | Does not handle user account passwords. |
Membership | Can add/remove users from the group. | Can add/remove users to/from a group. |
Administrator | Can assign group administrators. | Cannot assign group administrators. |
Password File | Manages group passwords in /etc/group . | N/A |
User Management | Focused on group management. | Focused on user account management. |
Few useful commands of gpasswd :
#to add user to a group
gpasswd -M <user> <group name>
#to add multiple user to group
gpasswd -M <user1> <user2> <group name>
#to remove user from a group
gpasswd -d <user> <group name>
Access Modes/Permissions
Basic meaning-
Access Modes | Binary | Decimal | Files | Directory |
d -> directory | N/A | Represent that it is a directory | ||
- | Represent that it is a file. | N/A | ||
r | 100 | 4 | To display the content | To list the content |
w | 10 | 2 | To modify | To create or remove |
x | 1 | 1 | To execute the file | To enter the directory |
Here r
stand for read, w
stands for write and x
stands for execute once you have all the permission you can do every action you want to take.
Decimal | Binary | |
rwx | 7 | 111 |
rw- | 6 | 110 |
r-x | 5 | 101 |
r-- | 4 | 100 |
-wx | 3 | 011 |
-w- | 2 | 010 |
--x | 1 | 001 |
Format:
U | G | O | ||
File | - | rwx | rwx | rwx |
Directory | d | rwx | rwx | rwx |
Command to change permissions
sudo chmod 745 <File/Directory name>
#There are other ways but this one is the most advanced method
Chown command
chown
stands for "change owner," and it is used to change the owner and/or group ownership of a file or directory.
#The basic syntax of chown is:
chown [new_owner]:[new_group] [file/directory]
# For example Change the owner of myfile.txt to "john" and the group to "users."
chown john:users myfile.txt
At the beginning of my blog I said that I would talk about various other commands too but now I realised it would make the blog too big and exhaustive so I have decided that I would write about them in future coming blogs.