DEV Community

Indra Wahyu
Indra Wahyu

Posted on

Turn off SSH access for a specific user in Ubuntu by modifying the SSH daemon configuration file

Edit the SSH Configuration File

Open the main configuration file using a text editor like nano with administrative privileges

sudo nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Add the Deny Directive

Scroll to the bottom of the file and add the DenyUsers directive followed by the username you want to block

DenyUsers username_to_block
Enter fullscreen mode Exit fullscreen mode

Restart the SSH Service

Save the file (press Ctrl+O then Enter in nano) and exit (Ctrl+X). Test your configuration for errors, then restart the service to apply the changes

sudo sshd -t
sudo systemctl restart ssh
Enter fullscreen mode Exit fullscreen mode

Alternative: The Whitelist Approach

If you prefer to block everyone by default and only allow a few specific users, use AllowUsers instead

AllowUsers allowed_user1 allowed_user2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)