User
List all users
cat /etc/passwd
List specific user
cat /etc/passwd | grep username
OR
grep username /etc/passwd
Create new user
useradd username
Delete existing user
userdel username
Set user password or update password
passwd username
You can use shortcut to add new user. Using following command you can create user set password and create home directory for newly added user
adduser username
ls -l /home/username
Rename existing user
usermod --login new-name old-name
Group
List all groups
cat /etc/group
List specific group
cat /etc/group | grep groupname
cat /etc/group | grep ^groupname
grep groupname /etc/group
grep ^groupname /etc/group
Add new group
groupadd group-name
Delete existing group
groupdel group-name
List all members of specific group
getent group group-name
Add new member in specific groups
usermod -a -G group-name1,group-name2,... user-name
Check in which groups user exist
groups user-name
Remove user from specific group
gpasswd -d user-name group-name
Permission
Permission List
- 7 – 111 => read write execute
- 6 – 110 => read write –
- 5 – 101 => read – execute
- 4 – 100 => read – –
- 3 – 011 => – write execute
- 2 – 010 => – write –
- 1 – 001 => – – execute
- 0 – 000 => no permission
How to change permission
chmod -Rf 777 path-to-file-or-directory
How to give only specific permission
#only executable
chmod -Rf +x path-to-file-or-directory
#only writable
chmod -Rf +w path-to-file-or-directory
#only readable
chmod -Rf +r path-to-file-or-directory
How to give permission to only specific role
#only owner
chmod -Rf u+x path-to-file-or-directory
#only group
chmod -Rf g+x path-to-file-or-directory
#only others
chmod -Rf o+x path-to-file-or-directory
Ownership
How to change ownership of any file or directory
chown -Rf user:group path-to-file-or-directory
Change attribute read only
chattr +a file.txt
chattr
: This is a command in Linux used to change file attributes on a file system.+a
: This option sets the “append-only” attribute on the specified file. When this attribute is set, the file can only be opened in append mode for writing. Existing data in the file cannot be modified or removed. This attribute is often used to prevent accidental deletion or modification of important files.- to revert use -a option for chattr