Step 1: Understanding User Switching (su)
Definition: User switching, achieved through the
su
command, allows you to switch from your current user account to another. This is helpful for performing tasks that require superuser privileges.Usage: Open a terminal and type
su
, followed by the username of the account you want to switch to.Example: To switch to the root user, type
su root
and enter the root user's password when prompted.Security Note: Be cautious when using
su
as it grants full access to the target account, which can be risky if used improperly.
Step 2: Secure User Switching (su -
)
Introduction:
su -
is a more secure way to switch users as it starts a new shell session with the target user's environment. It's the recommended method for switching to the root user.Usage: Type
su -
followed by the username of the account you want to switch to.Example: To securely switch to the root user, type
su - root
and enter the root user's password when prompted.
Step 3: Managing Privileges with sudoers
Overview:
sudo
is a powerful command that allows authorized users to execute specific commands with superuser privileges. The configuration forsudo
is stored in thesudoers
file.Accessing
sudoers
: Use thevisudo
command to safely edit thesudoers
file. It opens the file in a text editor and performs syntax validation.Adding User to
sudo
Group: Ensure that the user is a member of thesudo
group. You can use theusermod
command:sudo usermod -aG sudo username
.
Step 4: Configuring sudoers
Edit
sudoers
: Runsudo visudo
to open thesudoers
file for editing.Granting All Permissions: To grant a user all permissions, add the following line to the file:
username ALL=(ALL:ALL) ALL
Replace
username
with the actual username.Granting Specific Permissions: To grant specific permissions, use this syntax:
username ALL=(ALL:ALL) command_to_execute
Replace
username
with the username, andcommand_to_execute
with the specific command.Example: To allow the user "john" to restart the Apache web server, add:
john ALL=(ALL:ALL) /usr/sbin/service apache2 restart
Security Note: Always be cautious when editing the
sudoers
file to prevent unintentional access or privilege escalation.
Step 5: Save and Exit sudoers
- When using
visudo
, the changes are automatically saved if there are no syntax errors. Simply exit the text editor.
Step 6: Testing sudo
Access
To test
sudo
access, open a new terminal and run a command withsudo
. For example, to restart Apache as "john," use:bashCopy codesudo /usr/sbin/service apache2 restart
You'll be prompted to enter your own password or, if configured, the user's password.
Step 7: Review and Audit
- Periodically review and audit your
sudoers
file to ensure that users have appropriate permissions. Remove unnecessary or overly permissive entries.
Step 8: Home Activity - Creating and Managing a User
Home Activity: Now that you've learned about user switching, securing user switching, and managing user privileges with
sudo
, it's time to put your knowledge into practice. Create a new user account on your Linux system and configure their permissions using thesudoers
file.Steps:
Open a terminal window.
Use the
sudo useradd
command to create a new user, replacingnewuser
with your desired username:sudo useradd newuser
Set a password for the new user:
sudo passwd newuser
Follow the prompts to set and confirm the password.
Edit the
sudoers
file usingsudo visudo
to grant specific permissions to the new user, just like you've learned in the previous steps.Save your changes and exit the
sudoers
file.Open a new terminal and switch to the new user using the
su - newuser
command.Test the new user's access by running a command that you granted them permission for using
sudo
.
Objective: By completing this home activity, you'll gain hands-on experience in creating a new user account, configuring their permissions, and testing their access. This practical exercise will reinforce your understanding of user management in Linux and enhance your system administration skills.