Understanding how users, groups, and permissions work in Linux can seem intimidating at first. In this tutorial, we'll break down the essentials of system administration for Linux environments. We'll cover different types of users, how to manage them, and how to modify permissions using key commands like chmod and chown.
Key Takeaways
- Linux supports system, regular, and superusers for different operational roles.
- Commands like
useraddanduserdelmanage user accounts directly. sudoandsuhave distinct roles for executing commands with elevated permissions.chmodmodifies file permissions, andchownchanges ownership.- Groups help streamline permission management and user organization.
Linux Users
Linux is designed to allow multiple users to access the same system simultaneously. These users fall into three categories:
System users
System users handle non-interactive or background processes. These users typically lack individual logins or home directories.
Regular users
Regular users operate interactive processes and usually have personal logins and home directories.
Super user
The root user, or superuser, holds ultimate control over files and permissions. This user is responsible for creating and managing other users.
Linux - Viewing all users
To view all the users on a Linux system, execute:
$ cat /etc/passwd
This file lists every user, showing their username, user ID, group ID, and home directory.
Linux - Switching to the root user
Switching to the root user varies by Linux distribution. On Ubuntu, the command is:
$ sudo su
Other distributions might allow switching with just su, though Ubuntu requires sudo since the root password isn’t set by default. Setting a root password (sudo passwd root) is possible but generally not advised.
Linux - Adding a user
To add users as root, use:
# useradd <username>
This command creates a new user. Set a password with:
# passwd <username>
This process prompts password creation for the user.
Linux - Logging in as a new user
Log in as your new user using:
# su <username>
You'll be prompted for the user's password and granted access once entered correctly.
Linux - Removing a user
Remove users with:
# userdel <username>
Linux - Difference between su and sudo
The su command lets you switch users. Without a username, it defaults to the root user. The sudo command temporarily grants users access to commands typically restricted to the root user, bypassing the need to switch users.
To use sudo, a user must be listed in the sudoers file located in the /etc/ directory. Instead of editing this file directly, use the visudo command for safety.
Linux Permissions
Permissions safeguard a user's files from others. Use ls -l to display file permissions in a directory, which outputs something like:
-rwxr--r-- 1 username groupname 1234 Jun 1 10:00 file.txt
The representation explains access levels for the owner, group, and others. For example, -rw-r--r-- means the owner has read and write access, group members have read access, and others also have read access.
Linux Groups
Groups streamline user management. List all groups and members with:
$ cat /etc/group
Users automatically belong to a group, and files created are generally assigned to the user's primary group. To manage group association:
Add a new group
$ groupadd <groupname>
Change groups
chgrp <newgroup> <filename>
Linux changing permissions with chmod
Use chmod as the root or a sudoer to adjust file permissions. For instance:
chmod +rwx file.txt
This grants read, write, and execute permissions. To revoke them:
chmod -rwx file.txt
Customize permissions further with user classes u, g, and o (user, group, others):
chmod ug+rw file.txt
chmod go-rw file.txt
chmod ugo+rwx file.txt
Linux changing ownership with chown
To change file ownership as root or a sudoer:
chown username file.txt
Change only the group with:
chown :newgroup file.txt
Conclusion
Understanding and utilizing chown and chmod helps maintain secure Linux environments by managing file permissions and ownership effectively. Properly organizing users and groups is critical for maintaining system integrity and efficient user management.
FAQ
What is the difference between root and sudo?
The root user has full control over the system, while sudo allows specific commands to run with root privileges, enforcing a level of security and preventing full root login when unnecessary.
How can I safely edit the sudoers file?
Use the visudo command to edit the sudoers file safely. This utility checks syntax for errors, avoiding potential security risks from incorrect configurations.
Why isn't it recommended to set a password for the root user in Ubuntu?
Not setting a password for root discourages direct root logins, enhancing security by making specific administrative tasks require increased verification through sudo.
