There may be times when you need to forcibly log out a user from your Linux system, such as during system maintenance or when dealing with a malicious or unauthorized user.
If you have physical access to the server, you can disconnect the network cable and restart the system. However, this might not be feasible if you're working on remote servers via SSH. In such cases, you can force a user logout by terminating the user's active processes.
Steps to force user logout in Linux:
- Open the terminal.
- Display a list of users currently logged in to the system.
$ who user :0 2021-01-23 16:23 (:0) shakir pts/1 2021-01-23 16:31 (192.168.111.1)
- Identify and list all processes associated with the user you want to force logout.
$ ps -U shakir PID TTY TIME CMD 2086 ? 00:00:00 systemd 2087 ? 00:00:00 (sd-pam) 2093 ? 00:00:00 pulseaudio 2097 ? 00:00:00 tracker-miner-f 2106 ? 00:00:00 dbus-daemon 2126 ? 00:00:00 gvfsd 2135 ? 00:00:00 gvfsd-fuse 2136 ? 00:00:00 gvfs-udisks2-vo 2149 ? 00:00:00 gvfs-mtp-volume 2153 ? 00:00:00 gvfs-goa-volume 2157 ? 00:00:00 goa-daemon 2177 ? 00:00:00 goa-identity-se 2186 ? 00:00:00 gvfs-afc-volume 2194 ? 00:00:00 gvfs-gphoto2-vo 2248 ? 00:00:00 sshd 2249 pts/1 00:00:00 bash
- Terminate the user's terminal or other session processes.
$ sudo kill 2249 [sudo] password for user:
bash is normally the process if the user is connected via SSH.
- Alternatively, end all processes owned by the user.
$ sudo pkill -u shakir
- Verify if the user is still logged in.
$ who user :0 2021-01-23 16:23 (:0)
- ForceIf necessary, force the termination of any remaining processes associated with the user.
$ sudo pkill -9 -u shakir
Author: Mohd
Shakir Zakaria
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Discuss the article:
Comment anonymously. Login not required.