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.
- Open Terminal on your Mac.
- 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.
- Replace
- 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.
- The
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.
-
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 andremote_host
with the IP address or hostname of the remote server.
- Use
-
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 andauthorized_keys
file have the correct permissions:chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- First, SSH into the remote machine as user A:
- 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.
- Open (or create) the SSH config file:
- Install the Remote - SSH Extension for VS Code if you haven't already.
- 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
andremote-udp
) listed. - Select the appropriate host to connect seamlessly.
- 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.
- 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.