To restrict SSH command execution to only the root user on an Ubuntu system, you need to modify the SSH server configuration and possibly adjust user permissions. Below are the steps to achieve this:
1. Edit the SSH Server Configuration
The SSH server configuration file is located at /etc/ssh/sshd_config. You can modify it to restrict SSH access.
Steps:
Open the SSH configuration file in a text editor with root privileges:
bash
sudo nano /etc/ssh/sshd_config
Look for or add the following lines to restrict SSH access:
bash
PermitRootLogin yes
AllowUsers root
PermitRootLogin yes: Ensures the root user is allowed to log in via SSH.
AllowUsers root: Restricts SSH access to only the root user. No other users will be able to log in via SSH.
Save the file and exit the editor (Ctrl+O, Enter, Ctrl+X in nano).
2. Restart the SSH Service
After making changes to the configuration file, restart the SSH service to apply the changes:
bash
sudo systemctl restart sshd
3. Test the Configuration
Before logging out of your current session, test the configuration to ensure you can still log in as root and that other users are blocked:
Try logging in as root from another terminal or machine:
bash
ssh root@your_server_ip
Try logging in as a non-root user (e.g., testuser):
bash
ssh testuser@your_server_ip
This should fail with a "Permission denied" message.
4. Optional: Disable SSH Access for Specific Users
If you want to allow certain users to log in but prevent them from executing specific commands, you can use the ~/.ssh/authorized_keys file with the command option. However, since you want only root to use SSH, the AllowUsers root setting in sshd_config is sufficient.
5. Optional: Restrict Root Login to Key-Based Authentication
For security, it’s recommended to disable password-based login for root and allow only key-based authentication. To do this:
Ensure you have an SSH key pair generated and the public key added to /root/.ssh/authorized_keys.
Edit /etc/ssh/sshd_config and add or modify these lines:
bash
PermitRootLogin prohibit-password
PasswordAuthentication no
Restart the SSH service:
bash
sudo systemctl restart sshd
6. Additional Security Considerations
Use a Firewall: Consider using a firewall (e.g., ufw) to further restrict SSH access to specific IP addresses:
bash
sudo ufw allow from <trusted_ip> to any port 22
sudo ufw enable
Change the SSH Port: For added security, you can change the default SSH port (22) to a non-standard port by editing the Port line in /etc/ssh/sshd_config:
bash
Port 2222
Then restart the SSH service.
7. Verify Permissions
Ensure the permissions of the SSH configuration files are correct to avoid issues:
Be cautious when restricting SSH access, as misconfiguration can lock you out of your server. Always test changes in a new terminal session before closing your current session.
If you are using a cloud server, ensure you have access to the provider's console or recovery mode in case you get locked out.
By following these steps, only the root user will be able to execute SSH commands, and all other users will be denied access.