How to disable remote login for a user in linux?
- To enable remote root login, enter the following command: /etc/ssh/sshd_config: PermitRootLogin yes #enabled.
- To disable remote root login, enter the following command: /etc/ssh/sshd_config: PermitRootLogin no #disabled.
Simply use chsh (change shell) command to change the users shell in /etc/passwd file from something like /bin/bash or /bin/sh to /sbin/nologin meaning refuse a login.
- Login to the server via SSH.
- Open the file “/etc/ssh/sshd_config” in any of your favorite text editors.
- Find the section in the file containing the line with “#PermitRootLogin yes” in it.
- Uncomment and change it to “PermitRootLogin no”.
- Save the file and exit.
- Restart the sshd service.
To disable multiple users simply use a space delimiter, for example: DenyUsers user1 user2 user3 You can also block all user's SSH access and only allow specific users to login via SSH access using: AllowUsers user10 user11 This would allow only user10 and user11 to access the server via SSH.
The simplest method to disable root user login is to change its shell from /bin/bash or /bin/bash (or any other shell that permits user login) to /sbin/nologin , in the /etc/passwd file, which you can open for editing using any of your favorite command line editors as shown. Save the file and close it.
The usermod command is one of the several Linux commands system administrators have at their disposal for user management. It is used to modify existing user account details, such as username, password, home directory location, default shell, and more.
The nologin shell is located at /sbin/nologin. On some systems, this shell may also be located at /usr/sbin/nologin . Either way, it's the same file and will provide the same function. To set a user's shell to nologin , you can use the usermod command, along with the -s or --shell option, as seen in the syntax below.
Steps to enable or disable password login in SSH:
Open sshd configuration file using favourite text editor. $ sudo vi /etc/ssh/sshd_config [sudo] password for user: Search for PasswordAuthentication and set the option to no to disable PasswordAuthentication method and yes to enable.
- Step # 1: Open sshd_config file. # vi /etc/ssh/sshd_config.
- Step # 2: Add a user. Only allow user vivek to login by adding following line: AllowUsers vivek.
- Step # 3: Restart sshd. Save and close the file.
- Edit the /etc/ssh/sshd_config file with a text editor and find the following line: #PermitRootLogin yes.
- Change the yes to no and remove the '#' at the beginning of the line so that it reads : PermitRootLogin no.
- Restart the sshd service:
How do I limit SSH in Linux?
Steps to limit concurrent connection on SSH server:
Open terminal application. Open sshd_config file using your preferred text editor. $ sudo vi /etc/ssh/sshd_config [sudo] password for user: Look for MaxStartups option and set the value to the maximum simultaneous connections to allow.
- As root, edit the sshd_config file in /etc/ssh/sshd_config : nano /etc/ssh/sshd_config.
- Add a line in the Authentication section of the file that says PermitRootLogin yes . ...
- Save the updated /etc/ssh/sshd_config file.
- Restart the SSH server: service sshd restart.

- Open the SSH terminal on your machine and run the following command: ssh your_username@host_ip_address. ...
- Type in your password and hit Enter. ...
- When you are connecting to a server for the very first time, it will ask you if you want to continue connecting.
- Disable Root Login in Linux with passwd Command.
- Disable Root Login Using the usermod Command.
- Changing the Login Shell to /usr/sbin/nologin.
- Disable Root Login in Linux for SSH.
- Login as root account into the server.
- Backup the /etc/sudoers config file. # cp -p /etc/sudoers /etc/sudoers.ORIG.
- Edit the /etc/sudoers config file. # visudo -f /etc/sudoers. From: ...
- Then save the file.
- Please do the same to other user account in sudo.
The remote SSH login process for root account should be automatically denied by our SSH server, as illustrated in the below screenshot. That's all! You shouldn't be able remotely login to SSH server with root account via password or via public key authentication mechanisms.