Skip to content

Instantly share code, notes, and snippets.

@aculich
Created November 20, 2024 20:34
Show Gist options
  • Save aculich/8bd8d20a98a8de46ab3aff42f85d1caa to your computer and use it in GitHub Desktop.
Save aculich/8bd8d20a98a8de46ab3aff42f85d1caa to your computer and use it in GitHub Desktop.

Quick Start Tutorial: Setting Up SSH for a Mac Laptop to Access Remote Linux Environments

This tutorial will guide you through setting up SSH on a Mac laptop to securely connect to a remote Linux machine with two different user accounts. We'll cover SSH key generation, integrating keys with the Mac keychain, updating authorized keys on the remote machine, and configuring VS Code for seamless access.

Step 1: Generate SSH Keys on the Mac

  1. Open Terminal on your Mac.
  2. Generate a new SSH key pair:
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    • Replace [email protected] with your actual email address.
    • When prompted for a file to save the key, you can press Enter to accept the default (~/.ssh/id_rsa).
    • Set a passphrase to enhance security.

Step 2: Add SSH Keys to the macOS Keychain

  1. Add the SSH private key to the SSH agent and store it in the macOS keychain:
    eval "$(ssh-agent -s)"
    ssh-add -K ~/.ssh/id_rsa
    • The -K flag saves the passphrase in the macOS keychain, so you won't need to enter it every time you use the key.

Step 3: Copy SSH Public Key to Remote User Accounts

We need to add the SSH public key to both the single-user account (user A) and the shared group account (UDP) on the remote Linux machine.

  1. Copy SSH Key to Remote User A

    • Use ssh-copy-id to copy your key to the remote machine for user A:
      ssh-copy-id user_a@remote_host
    • Replace user_a with the correct username on the remote machine and remote_host with the IP address or hostname of the remote server.
  2. Manually Add SSH Key to Shared Group Account (UDP)

    • First, SSH into the remote machine as user A:
      ssh user_a@remote_host
    • Switch to the shared group account (UDP):
      sudo su - udp
    • Append your public key to the authorized_keys file in the .ssh directory of the shared account:
      cat >> ~/.ssh/authorized_keys << EOF
      [paste your public key here]
      EOF
    • Make sure the .ssh directory and authorized_keys file have the correct permissions:
      chmod 700 ~/.ssh
      chmod 600 ~/.ssh/authorized_keys

Step 4: Configure SSH Config File on the Mac

  1. Edit the SSH Config File to simplify connections and manage different users:
    • Open (or create) the SSH config file:
      nano ~/.ssh/config
    • Add the following configuration:
      Host remote-user-a
        HostName remote_host
        User user_a
        IdentityFile ~/.ssh/id_rsa
        UseKeychain yes
      
      Host remote-udp
        HostName remote_host
        User udp
        IdentityFile ~/.ssh/id_rsa
        UseKeychain yes
      
    • Replace remote_host with the actual IP or hostname of your server.
    • UseKeychain yes ensures that the key is automatically retrieved from the macOS keychain.

Step 5: Use VS Code with SSH

  1. Install the Remote - SSH Extension for VS Code if you haven't already.
  2. Connect to Remote Hosts:
    • Open VS Code.
    • Press Cmd+Shift+P and type Remote-SSH: Connect to Host.
    • You should see the configured hosts (remote-user-a and remote-udp) listed.
    • Select the appropriate host to connect seamlessly.

Step 6: Test Your SSH Setup

  • From your Mac terminal, you should be able to SSH into the remote machine without needing to enter the passphrase again:
    ssh remote-user-a
    ssh remote-udp
  • VS Code should also be able to connect to both accounts without further authentication prompts.

Summary

  • SSH Keys are generated on the Mac and integrated with the macOS keychain for easy access.
  • Authorized keys are updated for both individual and group accounts on the remote machine.
  • VS Code configuration is updated to easily connect to the remote environment.

With this setup, you'll be able to seamlessly connect to your remote Linux machine from your Mac laptop, either through the terminal or VS Code, without having to enter your SSH key passphrase every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment