How to Set Up Secure Remote Access and SSH Key Authentication on Cloud Servers

How to Set Up Secure Remote Access and SSH Key Authentication on Cloud Servers

Setting up secure remote access and SSH key authentication on cloud servers is essential for protecting your server from unauthorized access. Here are the steps you can follow to achieve this:

  1. Launch a Cloud Server:
    • Start by creating a new cloud server instance on your chosen cloud provider's platform (e.g., AWS, Google Cloud, Azure, DigitalOcean, etc.).
    • Ensure that you choose a secure operating system, such as a recent version of Ubuntu, CentOS, or Debian.
  2. Connect to the Server:
    • Use the provided credentials (usually a username and password) to connect to the server using SSH. For example, if you're using Linux/macOS, open a terminal and run:cssCopy codessh username@your_server_ip
  3. Update the System:
    • Once logged in, update the system's package lists and upgrade any existing packages to ensure you have the latest security patches:sqlCopy codesudo apt update && sudo apt upgrade
  4. Create a New User:
    • It's recommended to create a new user for remote access and disable root login for added security.Copy codesudo adduser newusername
    • Follow the prompts to set a password and other details.
  5. Grant Sudo Privileges (Optional):
    • If you want the new user to have administrative privileges, add them to the sudo group:Copy codesudo usermod -aG sudo newusername
  6. Generate SSH Key Pair:
    • On your local machine, generate an SSH key pair (if you haven't already):cssCopy codessh-keygen -t rsa -b 4096 -C "your_email@example.com"
    • This command creates a new SSH key using the provided email as a label.
  7. Copy Public Key to Server:
    • After generating the key pair, copy the public key to the server using the ssh-copy-id command. Replace newusername and your_server_ip with your specific values:sqlCopy codessh-copy-id newusername@your_server_ip
  8. Disable Password Authentication:
    • Once the key is copied, you should disable password authentication to force key-based authentication. Open the SSH configuration file:bashCopy codesudo nano /etc/ssh/sshd_config
      Find the line PasswordAuthentication yes and change it to PasswordAuthentication no.
      Save the file and exit.
  9. Restart SSH Service:
    • Restart the SSH service for the changes to take effect:Copy codesudo systemctl restart ssh
  10. Test SSH Key Authentication:
    • Attempt to SSH into your server. You should be able to log in without being prompted for a password:cssCopy codessh newusername@your_server_ip
  11. Optional: Disable Root Login:
    • If you haven't done so already, disable direct root login to further enhance security:bashCopy codesudo nano /etc/ssh/sshd_config
      Find the line PermitRootLogin yes and change it to PermitRootLogin no.
      Save the file and restart the SSH service.
  12. Firewall Configuration (Optional):
    • Configure the firewall to only allow specific ports and services. For example, you can use ufw on Ubuntu:bashCopy codesudo ufw allow ssh
      sudo ufw enable

Now, you should have secure remote access and SSH key authentication set up on your cloud server. Make sure to keep your private key secure and don't share it with unauthorized parties. This setup provides a strong layer of security for accessing your server remotely.