Linux (Users, Groups and Permissions)

:::::::::::::Users in linux ::::::::::::::
(to add a user)
fali@ubuntu:~$sudo adduser ali

(to delete user)
fali@ubuntu:~$sudo userdel ali

(to delete user and the user home directory)
fali@ubuntu:~$sudo userdel -r ali

(to manually delete the user home directory if it is left behind)
fali@ubuntu:~$sudo rm -r /home/ali

(all user informatin is stored in a text file /etc/passwd)(users passwords information file)
(usernames and home directories)
(to see all the information for users)
(different services also have usernames)
(you can rename the username in this file)
fali@ubuntu:~$sudo vi /etc/passwd (or fali@ubuntu:~$sudo vim /etc/passwd)

(to change the user password)
fali@ubuntu:~$sudo passwd ali

(to clear the screen)

::::::::::::Groups in linux::::::::::::
(put users into groups and give permissions to groups )
fali@ubuntu:~$sudo groupadd marketing (add a group)
fali@ubuntu:~$sudo groupdel marketing (delete a group)
fali@ubuntu:~$sudo adduser ali marketing (add a user in a group)
fali@ubuntu:~$sudo deluser ali marketing (to remove a user from a group)
fali@ubuntu:~$sudo gpasswd -a ali managers
fali@ubuntu:~$sudo gpasswd -d ali managers

(to see all the information about the groups)(groups configuration file)
fali@ubuntu:~$sudo vi /etc/group (fali@ubuntu:~$sudo vim /etc/group)

(to see all the groups you are a member of)
(to see all the groups)
fali@ubuntu:~$cat /etc/group

three digit number e.g. 214
first digit= permissions of the owner of the file (e.g. read, write and execute)
second digit= permissions of the group owner
third digit= everybody else (not even an owner) (mostly given read and execute for example to execute the scripts e.g 5)

(to change the permissions)
fali@ubuntu:~$sudo chmod 755 folder1 -R
fali@ubuntu:~$sudo chmod 775 file1
(chmod stands for change mode)
(-R switch is for recursive, for a folder)
(three digit number depends on the security policy)

to see the permissions for files and folders in the current directory->
fali@ubuntu:~$ls -l
drwxrwxrwx 2 root root test1
(rwxrwxrwx means 777 permission)

fali@ubuntu:~$sudo chmod 755 folder1 -R
7=rwx (4+2+1)
5=rx- (4+1)

fali@ubuntu:~$ls -l
drwxr-xr-x 2 ali group1 test1

(execute for e.g. .txt file means that there are scripts in it)
(execute for folder means you can change a working directory to be the working directory)
(in windows the file or folder can be accessed by several groups, but in linux only by one user, one group and everyone)
(but can use access control list to allow multiple groups to access file or folder)

fali@ubuntu:~$sudo chmod u+w file1
fali@ubuntu:~$sudo chmod g+r file1
fali@ubuntu:~$sudo chmod o+x file1

fali@ubuntu:~$sudo chmod -R u+w folder1

fali@ubuntu:~$sudo chmod u-w file1
fali@ubuntu:~$sudo chmod g-r file1
fali@ubuntu:~$sudo chmod o-x file1

fali@ubuntu:~$sudo chmod u+rwx,g+rw,o+x file1

fali@ubuntu:~$sudo chmod u-rwx,g-rw,o-x file1

::::::::::::::Ownerships of files and folders:::::::::::::
fali@ubuntu:~$sudo chown -R user1 folder1 (to change the user ownership of a folder and sub-folders)
fali@ubuntu:~$sudo chown user1 file1 (to change the user ownership of a file)

fali@ubuntu:~$sudo chgrp -R group1 folder1 (to change the group ownership of the folder and sub-folders)
fali@ubuntu:~$sudo chgrp group1 file1 (to change the group ownership of the file)

fali@ubuntu:~$ls -l
drwxrwxrwx 2 root root 4096 test1
(first root is the user-owner and the second root is the group-owner)

fali@ubuntu:~$sudo chgrp -R ali folder1
fali@ubuntu:~$ls -l
drwxrwxrwx 2 ali root 4096 test1

fali@ubuntu:~$sudo chgrp -R testgroup1 folder1
fali@ubuntu:~$ls -l
drwxrwxrwx 2 ali testgroup 4096 test1

(changing ownership in one line)(-R is for recursive in case of a folder)
fali@ubuntu:~$sudo chown ali:marketing file1
fali@ubuntu:~$sudo chown -R ali:marketing folder1


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s