SSH Basics
SSH is a protocol that allows you to connect to a remote computer. It is widely used in remote computing, such as connecting to a remote server or running Jupyter Lab in a remote host.
Create an SSH key pair
The following steps are adapted from the Generating a new SSH key and adding it to the ssh-agent. The procedure has been tested on macOS Sequoia.
Check for existing SSH keys
First check for existing SSH keys on your computer by running:
|
|
Check the directory listing to see if you have files named either id_ed25519.pub
or id_ed25519.pub
. If you don’t have either of those files then read on, otherwise skip the next section.
Generate a new SSH key
Open Terminal. Paste the text below, substituting in your GitHub email address. This creates a new ssh key, using the provided email as a label.
|
|
Add your SSH key to the ssh-agent
Start the ssh-agent in the background.
|
|
If you’re using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config
file to automatically load keys into the ssh-agent and store passphrases in your keychain.
First, check to see if your ~/.ssh/config file exists in the default location.
|
|
If the file doesn’t exist, create the file.
|
|
Next, open your ~/.ssh/config file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.
|
|
Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.
|
|
SSH Access to Windows
Creating an SSH Key in Local SSH Client
For a detailed explanation, refer to my previous post.
In summary:
- Generate a key pair in the local SSH client.
- Activate the
ssh-agent
and link the private key to it.
In Windows, this process can be executed in PowerShell:
|
|
Starting SSHD on the Remote Host
|
|
Deploying the Public Key to the Remote Host
Your public key, \.ssh\id_ed25519.pub
, should be placed on the server in a text file named administrators_authorized_keys
located in C:\ProgramData\ssh\
.
Configuring the default shell for OpenSSH in Windows
For example, to use PowerShell as the default shell:
|
|
Windows Configurations
In Windows, sshd reads configuration data from `%programdata%\ssh\sshd_config`` by default.
You can modify the configuration file to change the default port, for example:
|
|
You can also disable password authentication to prevent brute-force attacks:
|
|
You can enable public key authentication:
|
|
Connecting to Windows in terminal
|
|
Connecting to Windows in VS Code
To connect, you need to know the username
and hostname
of the remote host.
Determining the Username
The username
typically matches the account name of the remote host, retrievable by:
|
|
However, if you’re using a Microsoft account to log in, the username
might be the associated email address.
Determining the Hostname
The hostname
, which is the IP address of the remote host, can be retrieved with:
|
|
If it doesn’t work
Try enable Port 22
|
|
Try enabling Remote server listen on socket
in VS Code.
SSH Access to WSL2
For a comprehensive guide, refer here.
Since Windows uses port 22 by default for SSH, consider changing the SSH port in WSL2 to 2222 to prevent conflicts.
On the Remote Host (WSL2)
|
|
On the Remote Host (Windows)
Unblock Port 2222
|
|
Automatically Start SSHD Service
Create a CMD script to initiate the SSHD service in WSL2.
|
|
Connecting to WSL2
|
|
Note that the username
here is distinct from the Windows host username; it pertains to the Linux system. Determine it using:
|
|
The hostname
, however, remains consistent with the Windows host.
VS Code Remote - SSH
Edit the ~/.ssh/config
on the local client:
|
|
Running Jupyter Lab on a Remote Host
Use ssh
to run jupyter lab
on a remote host and access it from a local browser.
Redirect Traffic from Remote Port to Local Port
If you designate <remote_port>
for the remote and <local_port>
for the local, redirect the traffic as:
|
|
Running Jupyter Lab on the Remote Host
|
|
Using croc
to Transfer Files
For more details, consult this link.
On the Local Machine
Navigate to the directory you wish to sync:
|
|
On the Remote Machine
To receive the files:
|
|