View User Information

CommandDescription
whoamidisplays the current logged-in user
idshows user ID (UID), group ID (GID), and group memberships
wholists all users currently logged into the system
userslists logged-in users in a single line
lastShows the login history of users.

Create New Users and Understand Primary Groups

CommandDescription
id <username>displays the UID, GID, and groups of a specific user
sudo adduser <username>creates a new user with a home directory and default settings
sudo adduser --home <homepath> --gid <groupname> <username>creates a new user with arguments
sudo passwd <username>Sets or changes a user’s password.

Note: A user’s primary group is automatically created with the same name as the user by default.


Explore and Modify User Groups

CommandDescription
groups usernamelists all groups a user belongs to
getent group <groupname>get gid from a group name
sudo groupadd <groupname>creates a new group
sudo usermod -aG <groupname> <username>adds a user to an additional group without removing existing groups
sudo usermod -G <groupname> <username>assigns a user to a group, replacing all existing group memberships
sudo deluser <username> <groupname>removes a user from a specific group

Grant Sudo Privileges to Users

CommandDescription
sudo usermod -aG sudo <username>adds a user to the sudo group (Debian/Ubuntu systems)
sudo visudoopens the sudoers file for editing
%groupname ALL=(ALL:ALL) ALLgrants sudo privileges to all users in a group

View and Understand File Permissions

CommandDescription
ls -ldisplays detailed file information, including permissions
ls -ld directory/shows permissions for a directory

File permission format: -rw-r--r--

  • First character: Type (- for file, d for directory)
  • Next three characters: Owner permissions (read, write, execute)
  • Middle three characters: Group permissions
  • Last three characters: Other users’ permissions

Change File Ownership

chown: Changes the ownership of files or directories.

CommandDescription
sudo chown user file.txtchanges the owner of file.txt to user
sudo chown user:group file.txtchanges both the owner and group of file.txt
sudo chown -R user directory/recursively changes ownership for a directory

Modify File Permissions

chmod: Changes file permissions.

CommandDescription
chmod 644 file.txtsets permissions to read/write for owner, read-only for group and others
chmod u+x file.shadds execute permission for the owner
chmod -R 755 folder/recursively sets permissions for a directory

Common Numeric Permissions:

  • 777: Full permissions for everyone
  • 755: Full permissions for owner, read/execute for group and others
  • 644: Read/write for owner, read-only for group and others