Hello My name is Goutham here in this blog I would like to share how to create users and assign permissions to them in Linux.
If we open a Linux terminal we observe something like
howtogeek is the user name , ubuntu is computer name ,$ means howtogeek is a normal user and for root user # is used ~ means we are currently in home directory.
Every non root user has their own space in home directory and we can display all users in machine from /etc/passwd file and all groups from /etc/group file.We can view normally or by executing cat /etc/passwd
command.
1.Adding a User
sudo adduser Goutham
Adds a user Goutham ,we can add user in other way aswell by
sudo useradd Goutham
Above command is not beginner friendly as we have to pass few arguments while running this command but adduser is beginner friendly.
2.Adding Group
sudo groupadd Devops
Creates a group Devops .
Other command to create group is addgroup but this is not beginner friendly.
3.Switching User
su - Goutham
Switching to Goutham user ,but if we want to switch to root user we have to use su -
4.Adding User to Group
sudo usermod -g Devops Goutham
Adding user Goutham to Devops group .If we want to add Goutham to multiple groups then we have to use
sudo usermod -G Devops,Development,Operations Goutham
using -G removes Goutham from previously existing groups and freshly assigns to
mentioned groups. If we want to add besides previously existing groups then we use
-aG flag.
All the mentioned groups and users must exist before we run this commands.
5. User Groups
We can know in which groups particular user was present.
sudo groups Goutham
Displays all groups in which Goutham was present.
6.Change Password for user
sudo passwd Goutham
We can change password for user Goutham.
7.Deleting User and Groups
sudo deluser Goutham
sudo userdel Goutham
Any of the above command is used to delete user Goutham, but if Goutham is present in any groups this action cannot be performed for that we need to remove user Goutham from all groups and then we can delete user.
sudo gpasswd -d Goutham Devops,Operations,Development
By executing this command Goutham will be removed from all mentioned groups.
To delete group
sudo groupdel Devops
sudo delgroup Devops
Any command can be used.
8. Change Owners
By executing ls -l
command , shell displays some of the permissions that file/folders have.
We can change owners ,groups by
sudo chown Goutham:Devops myfolder.
This above command means from now user Goutham from Devops group is new owner for my folder rather than vikash .
If we want to change only user by keeping group unchanged then
sudo chown Goutham myfolder
If we want to change only group and keep user unchanged by using
sudo chgroup Devops myfolder
9.Change Permissions
While executing ls -l
command we observe rwx and some dashes as 3 groups.
If first is character is d then it is a folder else it is file.
r->Read permission
w-> Write permission
x-> Execute permission
- -> No permission
First 3 characters are for user next 3 are for Group next 3 are for others who doesn't belong to the group .
There are 3 types to change permissions on a file/folder.
Type-1
If we want to add execute permission for user which means user can execute that folder then we use
sudo chmod u+x myfolder
To remove execute permission we use - instead of + .For group we use g+x for others we use o+x .
Same continues with other permissions as well.(r,w)
Type -2
If we want to add more permissions at a time this type is pretty much useful.
sudo chmod g=rw- myfolder
Here in above command we are trying to override read and write permissions on group .
Type-3
This type is most used here we use numbers to assign permissions.
0-> No permissions
1->Execute
2-> Write
4-> Read
With this type we can assign all permissions at a time to users ,groups and others.
sudo chmod 663 myfolder
Above executing above command we are assigning read and write permissions to user read and write permissions to group and write and execute permissions for others.
That's it these are the basic beginner commands to deal with users and permissions on Linux machine.Hope this helps. Thanks for reading my article🎉.