Setting Up SSH with RSA Key and Password Authentication on Ubuntu

Steps to Achieve This Setup:
1. Install OpenSSH Server (if not already installed):
2. Create Users (If Not Already Created):
3. Configure SSH for RSA Key Authentication (For user1
)
a. Create the .ssh
directory for user1
:
sudo mkdir -p /home/user1/.ssh
sudo chmod 700 /home/user1/.ssh
b. Generate RSA 4096-bit Key Pair:
-f ~/.ssh/sftp_rsa
is to specifies the file path where the key pair will be saved
This creates:
- Private key:
~/.ssh/sftp_rsa
- Public key:
~/.ssh/sftp_rsa.pub
c. Copy the public key (sftp_rsa.pub
) to /home/user1/.ssh/authorized_keys
:
Paste the contents of sftp_rsa.pub
into this file.
d. Set correct permissions:
4. Enable Password Authentication for user2
Open the SSH configuration file:
Add the following rules at the end of the file:
user1
will only use RSA key authentication.user2
will only use password authentication.
Restart SSH Service:
Optional: Restrict Users to SFTP Only
If you want users to only use SFTP and prevent SSH shell access, modify /etc/ssh/sshd_config
:
Then, restart SSH:
This will ensure both users can only use SFTP instead of full SSH access.
Connecting to the Server
For user1 (RSA Key Authentication)
On the client machine, rename the private key to .pem
for easier use:
Then, connect using:
For user2 (Password Authentication)
Simply connect using:
You’ll be prompted to enter the password.
Now your setup allows:
user1
to authenticate with only an RSA key.user2
to authenticate only with a password.- (Optional) Both users are restricted to SFTP-only access.
If you found this guide helpful, consider supporting me!