In this tutorial, we will show you how you can logout a user on a Linux operating system.
Knowing how to logout a user in Linux is vital to maintaining a healthy operating system. You might need an ex-employee you want removed from the system, or you have identified a rogue user logged in. Luckily the process of logging out the user is very straightforward.
We will also go into detail about logging out your user from the system if you are using SSH. This process is easy and will help close an SSH connection to your server correctly.
You must be careful when you log out a user as any unsaved work will be forced closed. Double check the spelling is correct, and you are logging out the correct user.
You will need to look at other tutorials for logging out of a Linux GUI desktop environment, as the logout process can vary heavily between different operating systems.
Using the who and pkill Command to Logout a User
This section will cover how you can use the who command to find a user that you can logout using the pkill command. You can use this method to log out any user you want, as long as you have the correct permissions.
It is important to never use the pkill command or its variants on the root user as it is likely to cause system instability.
1. We can use the who command to view all users currently logged into the system. Using this data, we can select a user to log out of the system.
To use the who command, enter the following line into the terminal.
You should see a list of all the users currently logged into the system.
2. To log out a user, use the pkill command with the
-u option. The
-u option specifies we are using the UID or username. This process will kill all processes that are owned by the specified user. We also use
-SIGKILL as this will ensure that the system kills the process.
In our example, we will log out the user named
newUser by entering the following line into the terminal.
3. You can verify the user is logged out of the system by using the who command.
Logout of an SSH Session
In this section, we will discuss how you can log out of an SSH session using two different methods. These methods will only work with the current user and cannot be used to log out other users.
Using logout or exit
If you are logged into a Linux system via an SSH session, you can use the logout command to log out of your current session.
If you are running as the root user or another user via the sudo su command, you may receive an error when you use the logout command, such as in the example below.
In this case, you will need to use the
exit command. You can also use the exit command instead of the logout command.
The command below will exit the current user session or log you out entirely if you haven’t used sudo su to switch users.
Using Shortcut keys
An alternative to both of the methods described above is to use shortcut keys. For example, pressing CTRL + D will work in both scenarios mentioned above (exit and logout).
Below is an example of the output after pressing CTRL + D.
You should have a good understanding of how you can log out of your user and other users on your Linux system using the commands mentioned above. Knowing the process of logging out a user is very beneficial if you have to maintain multi-user systems.
There is plenty more to learn about Linux systems and the commands you can use in the terminal. If you are interested in user management, look at our tutorials on usermod, useradd, and userdel. They are extremely useful for handling multiple users.
Please let us know if you notice a mistake or if an important topic is missing from this guide.