- Ssh Config File Keys
- Ssh Host Config
- Ssh Config File Multiple Keys
- Ssh Config File Key Code
- Ssh Config File Key
- Ssh Configuration File
You now have a public and private SSH key pair you can use to access remote servers and to handle authentication for. Open your SSH config file. Future Studio provides on-demand learning & wants you to become a better Android (Retrofit, Gson, Glide, Picasso) and Node.js/hapi developer! On the user details page, choose the Security Credentials tab, and then choose Upload SSH public key. Paste the contents of your SSH public key into the field, and then choose Upload SSH public key. Copy or save the information in SSH Key ID (for example, APKAEIBAERJR2EXAMPLE). SSH config file syntax and how-tos for configuring the OpenSSH client.
October 3, 2019 by Sana Ajani, @sana_ajani
In a previous Remote SSH blog post, we went over how to set up a Linux virtual machine and connect to the VM using the Remote - SSH extension in Visual Studio Code. In this blog post, we'll go into some tips and tricks that you can use to get the most out of your remote setup.
Connect using Remote SSH
The Visual Studio Code Remote - SSH extension allows you to connect to a remote machine or VM using SSH, all from inside VS Code. If you don't already have the extension installed, you can search for 'remote ssh' in the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
After you install the extension, you'll notice an indicator on the bottom-left corner of the Status bar. This indicator tells you in which context VS Code is running (local or remote). Click on the indicator to bring up a list of Remote extension commands.
SSH configuration file
In the earlier Remote SSH blog post, we only connected to a single machine and did so by entering the 'user@host' when prompted. If you log in to multiple remote servers or local virtual machines on a regular basis, there's a better way to connect without having to remember all the usernames, addresses, and additional configuration options.
OpenSSH supports using a configuration file to store all your different SSH connections. To use an SSH config file, click on the remote indicator to bring up the remote commands, choose Open Configuration File, and select the file that follows the path 'Users/{yourusername}/.ssh/config'.
Ssh Config File Keys
Here's an example of an SSH config file:
There are many more configuration options you can specify in the SSH config file format. You'll get completions and colorizations in this file and you can press (⌃Space (Windows, Linux Ctrl+Space)) for IntelliSense to learn more about the config options.
The options used above are:
Option | Description |
---|---|
Host | An easy-to-remember alias for your host machine. |
HostName | The hostname of server (you can use the IP address of the server). |
User | The user you've specified to log in to the machine via SSH. |
Port | The port used to connect via SSH. The default port is 22, but if you've specified a unique port, you can configure it here. |
IdentityFile | The file location where you've stored your private key. |
You can add the information for all the hosts you have. Once you've saved the config file, you'll be able to see those hosts in the Remote Explorer, as well as any folders you have opened on that host. You can select the icon next to each host or folder and it will launch a new VS Code window (instance) and connect you to that host. In the screenshot below, I'm connected to my remote machine 'python-linux-vm' and the Remote Explorer shows me the folders I have connected to in the past, as well as any forwarded ports from the remote machine.
ProxyCommand
Sometimes you may need to connect from your desktop or laptop to a remote machine over your company's Intranet or behind a firewall. In this case, you may be using an intermediate server or jump box. This kind of setup is useful if you are working within a secure system that is configured to only accept SSH connections from a fixed set of hosts.
To use a jump-box setup with the Remote - SSH extension, you can use the ProxyCommand
config option. This configuration will open a background SSH connection to the jump box, and then connect via a private IP address to the target.
You can set the ProxyCommand
config option in the SSH config file like this:
ControlMaster
If you are connecting to a remote SSH host using other authentication methods besides key-based authentication, such as two-factor, password-based, or an SSH key with a passphrase, you may have to enter the required information multiple times.
Instead of opening multiple SSH connections, you can use ControlMaster
option (only on macOS/Linux clients) to reuse an existing connection and reduce the number of times you must enter your passphrase.
To use this feature, add the following to your SSH config file:
Offline remote machine
If you are restricted by a firewall or your company locks down your VMs and they cannot connect to the Internet, the Remote - SSH extension won't be able to connect to your VM because VS Code needs to download a component called the VS Code Server to the remote machine.
However, you can now solve this issue by a new user setting in the Remote - SSH extension. If you enable the setting remote.SSH.allowLocalServerDownload
, the extension will install the VS Code Server on the client first and then copy it over to the server via SCP.
Note: This is currently an experimental feature but will be turned on by default in the next release.
Remote - SSH Nightly extension
If you're interested in testing new updates and experimental features as soon as they are available, install the Remote - SSH Nightly extension (uninstall the Remote-SSH stable extension first). This is the nightly build of the extension where we experiment with new features and settings before releasing them into the stable version.
We'd like your feedback
Thanks for trying out the Remote - SSH extension! If you run into any issues or would like to suggest new features or scenarios for us, please open an issue on our GitHub repo. If you want to see what features we're currently working on or are upcoming, take a look at our Remote Development release notes and iteration plans. You can also try out the introductory Remote development over SSH tutorial, which walk you through using the other remote extensions to work inside Docker containers and the Window Subsystem for Linux (WSL).
Happy Remote Coding,
Sana Ajani, VS Code Program Manager @sana_ajani
Managing remote servers requires either a very good memory to remember connection options like usernames, remote addresses, ports and further details or a good way to document all the details for each server.
The SSH config file should be your helping hand to control and simplify SSH connections. If you didn't have any SSH client installed yet, please go ahead and do it. OpenSSH is our tool of choice.
Complicated Connections
Connecting to a remote server via SSH requires a username, server url or IP address and the SSH server port listening for connections. Let's look at a specific example. Assuming your server url is yourserver.url
with user marcus
on port 2222
.
Your connection command looks like this:
All parameters are required to pass within the connection string.
Simplify with SSH Config File
The SSH config file isn't created automatically while installing SSH on your machine. The config file needs to be placed into your .ssh
folder. By default, the location is ~/.ssh
. Let's create the config file using nano
command line editor. Since nano
is Linux specific, you can use any other editor of your choice.
This command opens the nano editor with a blank file. Saving to disk will create the desired SSH config file.
We define the file content using the key-value system. Each key-value-pair is stated in a separate line. A key gets its value assigned by separating both by either whitespace or equal sign or a combination of equal sign with spaces. The SSH clients interpret all statements identical.
Ssh Host Config
Each configuration in your config file is initiated by the keyword Host
followed by an identifier.
Common SSH configuration options
- HostName: the hostname or IP address of your remote server. You can skip this definition if the
Host
identifier already specifies the actual hostname you want to connect with. - User: the connection username.
- Port: the port where your remote SSH server is listening for connections. Default value
22
.
The options above describe the basic configuration for an entry in the SSH config file. There are additional SSH connection items and tweaks which can be used for more complex setups.
Ssh Config File Multiple Keys
General Tweaks and SSH connection items
- Compression: a useful option for (very) slow connections.
- ServerAliveInterval: use this option to let both peers stay in contact and avoid session closes due to SSH timeouts. Configure this option to let SSH send a packet to keep the connection between client and server alive. Also, you can use this option to know if your unreliable connection is still alive.
- StrictHostKeyChecking: this option is used to configure whether SSH automatically adds hosts to the
~/.ssh/known_hosts
file. By default, you're asked to confirm the addition to the known hosts. The default value can be annoying connecting to multiple different hosts, so you may want to set this to no and add every connected remote host to known hosts automatically.
Actually, there are more options to configure SSH. You can keep those items listed above in mind and in case you run into issues with your SSH connections, change the values and check whether they improve.
Complete configuration entry example:
Of course you can define multiple entries in your config file. Just separate them by an empty line
The second example entry omits the HostName
definition because it's already set as Host
identifier.
Just save the file and leave nano
. Your created file will be recognized by your SSH client for future connections.
Connect Painlessly
Ssh Config File Key Code
From now on, you can use the defined Host
identifier for any connection to your remote server.
Your SSH client parses the config file and matches the defined Host
identifier values with your provided identifier. In case they match, the specific configuration gets automatically loaded from the config file.
Ssh Config File Key
That's all the magic! Enjoy the simplification of your SSH configuration.