Linux Fundamentals
Shells, User Management, and Permissions
Shells
A shell is a command-line interpreter that provides a user interface for accessing the operating system’s services. It’s where you type commands that the kernel then executes.
Common Shells in Linux:
- Bash (Bourne-Again SHell): The most common default shell, valued for its ubiquity and powerful scripting.
- Zsh (Z Shell): A highly customizable shell with advanced features like improved tab completion and plugin support.
- Sh (Bourne Shell): An older, simpler shell that Bash is largely compatible with.
Checking and Changing Shells:
To see your current shell:
echo $SHELL
To see the default shell for user ‘john’:
grep ^john /etc/passwd
To change your own shell to Zsh:
chsh -s /bin/zsh
Restricted Shells
A restricted shell limits the commands and actions a user can perform, often used for security purposes. The most common is rbash.
Example: Setting a user’s shell to rbash
sudo usermod -s /bin/rbash newuser
User Management Commands
Linux provides several command-line utilities for managing user and group accounts. These commands require root privileges (or sudo
) to execute.
Command | Description | Example Usage |
---|---|---|
useradd | Creates a new user. | sudo useradd -m student |
usermod | Modifies a user. | sudo usermod -aG sudo student |
userdel | Deletes a user. | sudo userdel -r tempuser |
passwd | Sets a password. | sudo passwd student |
groupadd | Creates a new group. | sudo groupadd developers |
groupmod | Modifies a group. | sudo groupmod -n dev team |
groupdel | Deletes a group. | sudo groupdel oldgroup |
id | Displays user/group IDs. | id student |
groups | Displays user’s groups. | groups student |
Homes and Permissions
Every user account has a home directory (e.g., /home/username
) to store personal files. Proper permissions are crucial for privacy and security.
Understanding Permissions
Let’s break down a permission string like drwxr-xr-x
:
d
: The entry is a directory. For a file, this would be a dash (-
).rwx
: The first set of three characters defines permissions for the owner (user). `r`=read, `w`=write, `x`=execute.r-x
: The second set defines permissions for the group. This group can read and execute, but not write.r-x
: The final set defines permissions for others (all other users).
Permissions are also represented by numbers (octal notation), where `r=4`, `w=2`, and `x=1`. So, `rwx` is `4+2+1=7` and `r-x` is `4+0+1=5`. Therefore, `rwxr-xr-x` is `755`.
Managing Permissions:
Checking permissions of a home directory:
ls -ld /home/john
Changing a file’s permissions to be readable/writable only by the owner (600):
chmod 600 /home/john/private_doc.txt
Changing the owner of a file:
sudo chown admin /path/to/file.txt
Changing the group owner of a file:
sudo chgrp developers /path/to/project_file.txt