BCE-C712 Linux System Administration

0 of 74 lessons complete (0%)

Managing User Accounts

Switching User and Switching Group

You don’t have access to this lesson

Please register or sign in to access the course content.

In this lesson, we will explore the important concepts of switching users (su) and switching groups (sg) in Unix-like operating systems. These commands are essential for system administrators to perform tasks with elevated privileges or to work within different user contexts without the need to log out and back in.

Switching User (su):

The su command allows you to assume the identity of another user, including the superuser (root), and perform tasks with their permissions.

  1. Syntax: su [username]
  2. Becoming the Superuser (Root):
    • Use su without specifying a username to become the superuser (root).
    su
    • Provide the root password when prompted.
  3. Switching to a Specific User:
    • To switch to a specific user account, provide the username as an argument.
    su username
    • Provide the user’s password when prompted.
  4. Preserving Environment Variables:
    • Use su - to switch users while preserving their environment, including the home directory and shell.
    su - username

Switching Group (sg):

The sg command allows you to execute commands within a different group context. This is useful when you need to temporarily work with the permissions of a specific group.

  1. Syntax: sg groupname [-c command]
  2. Running Commands with a Different Group:
    • Use sg followed by the group name and the command you want to execute within that group’s context.
    sg developers -c "make all"
    • This command executes the make all command within the developers group context.

Restricting Access:

It’s important to manage who has access to use the su and sg commands.

  1. Limiting su Access:
    • Adjust the /etc/sudoers file to specify which users or groups are allowed to use su.
  2. sg Access Control (Optional):
    • Depending on the system, you may need to configure access to the sg command using group permissions.

Best Practices:

  1. Avoid using su for extended periods to minimize potential security risks.
  2. Always switch back to your regular user account after completing administrative tasks.
  3. Use sudo when possible for specific commands that require elevated privileges.
  4. Ensure that users have appropriate permissions to use su and sg commands.

By understanding and using the su and sg commands effectively, system administrators can perform tasks with the necessary privileges without the need to log out and back in, streamlining administrative tasks.