Skip to main content

Access

User and Groups

whoami, id, groups, users

CommandShort Description
whoamiPrint current user's name
idPrint user and group information
groupsPrint group memberships
groups usernameShow groups for a specific user
usersShow a list of all logged-in users
cat /etc/passwdinformation about system users
cat /etc/groupinformation about groups
cat /etc/shadowencrypted password hashes for user accounts

passwd, useradd, adduser, usermod

CommandShort Description
passwd usernameChange password for the user 'username'
passwd -l usernameLock user's password, she cannot use password to login or change it
passwd -u usernameUnlock the password of the user
useraddCreate new user
useradd -m usernameCreate a new user with a home directory
useradd -m -G groupname usernameCreate user with home directory and add them to a group
useradd -m -s /bin/bash usernameCreate user with home directory and /bin/bash for login shell
adduser usernameInteractively create a new user 'username'
usermod -a -G groupname usernameAdd the user 'username' to the group 'groupname'
usermod -l newname usernameChange the login name of the user 'username' to 'newname'
usermod -m -d /new/home/dir usernameMove (create) user home directory
sudo usermod --expiredate 1 usernameSet the user to expire immediately

groupadd, userdel, chage

CommandShort Description
groupaddAdd a new group to the system.
sudo groupadd developersCreate a group named "developers".
sudo userdel johnDelete the user account "john".
sudo userdel -r janeDelete the user account "jane" along with home directory.
chage -l usernameList password aging information for a user.
chage -M 90 usernameSet maximum password age to 90 days for a user.

getent

getent retrieves entries from databases configured in /etc/nsswitch.conf

CommandShort Description
getentRetrieve entries from administrative databases
getent passwdRetrieve user account information (e.g., usernames, home directories)
getent hostsRetrieve host information (e.g., IP addresses, hostnames)
getent passwd mlibreRetrieve information for the user "mlibre"

/etc/skel

The /etc/skel/ directory in Linux is used as a template for creating a new user's home directory. When a new user is created with the adduser or useradd -m command, the system copies the files and directories contained in the /etc/skel/ directory to the new user's home directory.

Files and Folders

chmod

CommandShort Description
chmodChange file permissions
chmod +x fileAdd execute permission to a file
chmod -w fileRemove write permission from a file
chmod 644 fileSet read and write for owner, read for group and others
chmod -R 755 dirRecursively set permission
chmod u=rwx,g=rx,o=rx fileSet specific permissions for user, group, and others
chmod u+s executableSet the setuid bit on an executable
chmod g+s executableSet the setgid bit on an executable
chmod a=-r fileRemove read permission for all (owner, group, and others)
chmod -x $(find /path -type f)Remove execute permission of all files in directory and its sub
find /path -type f -exec chmod -x {} \;Alternative to above command
find /path -type f -print0 | xargs -0 chmod -xAlternative to above command

a+x will set all the x bits of the file
+x will set all the x bits of the file that are not present in the umask
-print0 tells find to print the results separated by null characters, instead of spaces or newlines. This is useful for safely handling filenames that might contain spaces or special characters
-0 tells xargs to expect input separated by null characters, and not by spaces or newlines. It ensures that xargs correctly processes the list of files provided by find

chown, chgrp, newgrp

CommandShort Description
chown user fileChange the owner of a file
chown user:group fileChange the owner and group of a file
chown -R user:group directoryRecursively change the owner and group of a directory and its contents
chown -c user:group fileChange the ownership, show a message if the ownership changes
chgrpChange group ownership of files and directories.
chgrp groupname file.txtChange the group ownership of file.txt to groupname.
chgrp -R groupname /path/to/directoryRecursively change group ownership to groupname.
chgrp --reference=file.txt test.txtChange group ownership of test.txt to match that of file.txt.
newgrpChange the user primary group (default)
newgrp staffSwitch to the 'staff' group

SUID, GUID

When the SUID permission is set on an executable file, it means that when a regular user runs that executable, it will run with the permissions of the file's owner instead of the user who is executing it.

when any user executes the /usr/bin/passwd command, it runs with the elevated permissions of the root user. This is necessary because changing a user's password requires write access to the /etc/shadow file, which is typically only accessible by the root user for security reasons.

The /etc/shadow file is owned by the root user and has restrictive permissions (e.g., readable and writable only by the root user). This means regular users do not have the necessary permissions to modify the file.

ls -l /usr/bin/passwd 
-rwsr-xr-x 1 root root 51552 Jan 25 2023 /usr/bin/passwd

Sticky bit

The sticky bit on a directory ensures that only the owner of a file within that directory (or the superuser) can modify, delete or rename that file, even if others have write permissions on the directory or the files.

Imagine you have a /tmp directory on a Linux system with the sticky bit set:

chmod +t /tmp

In this setup:

  • Alice can delete files she creates in /tmp
  • Bob can delete files he creates in /tmp
  • Other users cannot delete files created by Alice or Bob in /tmp, enhancing file security in shared directories like /tmp

umask

umask is a Linux command that controls the default permissions for newly created files and directories

umask
# 0022
  • For files: 666 - 0022 = 0644
  • For directories: 777 - 0022 = 0755
# read and write permissions to the owner and remove all permissions for others and groups on newly created files:
umask u=rw,go=

# remove write permissions for the users, group and others on newly created directories:
umask ugo-w