# How to Change User in Linux


Changing users in Linux is a common task you might need to do whether you're managing a server or just switching accounts on your personal machine. If you’re new to Linux or want to get more comfortable with user management, this guide will walk you through the easiest and most effective ways to change users. You’ll learn how to switch users using the command line and graphical interfaces, making your Linux experience smoother.

We’ll cover everything from basic commands like `su` and `sudo` to graphical options for desktop environments. By the end, you’ll know exactly how to switch users safely and efficiently, no matter your Linux setup. Let’s dive in and make user switching simple for you.

## Understanding User Accounts in Linux

Linux is a multi-user operating system, which means several users can have accounts on the same machine. Each user has their own files, settings, and permissions. Changing users means switching from one account to another, which is essential for security and managing different tasks.

Here’s what you need to know about Linux users:

- **Root User:** The superuser with full system control.
- **Regular Users:** Accounts with limited permissions.
- **Guest Users:** Temporary accounts with restricted access.

Knowing these helps you understand why and when you might want to change users.

## Using the `su` Command to Change User

The `su` command stands for "substitute user" or "switch user." It’s one of the oldest and most straightforward ways to change users in Linux.

### How to Use `su`

- Open your terminal.
- Type `su - username` and press Enter.
- Enter the password for the target user when prompted.

For example, to switch to user `john`, you’d type:

```bash
su - john
```

The `-` ensures you get the target user's environment, including their PATH and variables.

### Important Points About `su`

- You need to know the target user’s password.
- If you just type `su` without a username, it switches to the root user.
- Use `exit` to return to your original user.

The `su` command is useful when you have the password of the user you want to switch to.

## Using `sudo` to Run Commands as Another User

`sudo` is another powerful command that lets you run commands as another user, usually root, without switching users completely.

### How to Use `sudo` for User Switching

- To run a command as another user, use:

```bash
sudo -u username command
```

For example, to list files as user `john`:

```bash
sudo -u john ls /home/john
```

- To open a shell as another user:

```bash
sudo -u username -i
```

This opens an interactive shell as that user.

### Why Use `sudo`?

- You don’t need the target user’s password, only your own.
- It’s safer because it limits full user switching.
- It’s commonly used for administrative tasks.

Make sure your user is in the `sudoers` file to use `sudo`.

## Switching Users Graphically in Linux Desktop Environments

If you’re using a Linux desktop like GNOME, KDE, or XFCE, you can switch users without the terminal.

### How to Switch Users Graphically

- Click on the system menu or user icon.
- Select “Switch User” or “Log Out.”
- Choose the other user account from the login screen.
- Enter the password to log in.

### Benefits of Graphical Switching

- Easy and intuitive for beginners.
- Keeps your session active while switching.
- Useful for shared computers.

This method is perfect if you prefer point-and-click over commands.

## Using `login` and `logout` Commands

The `login` command lets you log in as a different user from a terminal session, while `logout` ends your current session.

### How to Use `login`

- Open a terminal.
- Type `login` and press Enter.
- Enter the username and password for the new user.

This method is less common but useful in certain terminal-only environments.

### How to Use `logout`

- Simply type `logout` or press `Ctrl+D` to end your session.

These commands are handy when working on virtual terminals or remote sessions.

## Switching Users in SSH Sessions

When you connect to a Linux server via SSH, you often start as one user but might need to switch.

### How to Switch Users Over SSH

- Connect to the server:

```bash
ssh username@server_ip
```

- Use `su` or `sudo` to switch users once logged in.

Alternatively, you can directly SSH as another user:

```bash
ssh otheruser@server_ip
```

This avoids switching users after login.

### Tips for SSH User Switching

- Ensure the target user has SSH access.
- Use key-based authentication for security.
- Avoid using root login over SSH for safety.

## Managing User Permissions for Switching

Sometimes, you might not be able to switch users due to permission issues.

### How to Fix Permission Problems

- Add your user to the `sudoers` file using `visudo`.
- Ensure the target user account is active and not locked.
- Check `/etc/passwd` and `/etc/shadow` for user status.

### Commands to Manage Users

- To unlock a user:

```bash
sudo passwd -u username
```

- To add a user to sudoers:

```bash
sudo usermod -aG sudo username
```

Proper permissions make user switching smooth and secure.

## Tips for Safe User Switching

Switching users can affect system security. Here are some tips to keep things safe:

- Avoid using root unless necessary.
- Always log out or exit after switching users.
- Use `sudo` instead of `su` when possible.
- Keep passwords secure and updated.
- Monitor user activity for unusual behavior.

Following these helps protect your Linux system.

## Summary Table: Commands to Change User in Linux

| Command                 | Description                          | Requires Password?       |
|-------------------------|------------------------------------|-------------------------|
| `su - username`         | Switch to another user shell       | Yes, target user’s      |
| `sudo -u username -i`   | Open interactive shell as user     | Your own (sudo access)  |
| `login`                 | Log in as another user in terminal | Yes, target user’s      |
| Graphical User Switch   | Switch users via desktop interface | Target user’s           |
| SSH as another user     | Connect directly as different user | Target user’s           |

This table helps you pick the right method for your needs.

## Conclusion

Changing users in Linux is easier than it seems once you know the right commands and methods. Whether you prefer the command line or graphical interface, Linux offers flexible options to switch users safely. Using `su` and `sudo` commands gives you control over user sessions, while graphical tools make it simple for desktop users.

Remember to manage permissions carefully and always log out when done to keep your system secure. With these tips and commands, you’ll be confident switching users on any Linux system, from personal desktops to remote servers.

### FAQs

### How do I switch to the root user in Linux?

Use the command `su -` and enter the root password. This switches you to the root user with full system privileges.

### Can I switch users without knowing their password?

Yes, if you have `sudo` privileges, you can run commands or open a shell as another user without their password using `sudo -u username -i`.

### How do I switch users in a graphical Linux environment?

Click your user icon or system menu, select “Switch User,” then choose the other account and enter its password at the login screen.

### What is the difference between `su` and `sudo`?

`su` switches the entire user session and requires the target user’s password. `sudo` runs commands as another user, usually root, using your own password and is more secure.

### How do I fix permission denied errors when switching users?

Make sure your user is in the `sudoers` file, the target user account is active, and you have the correct passwords. Use `visudo` to edit sudo permissions safely.
