Remote SSH: tips and hacks

In a previous post on Remote SSH, we looked at how to configure a Linux virtual machine and connect to a virtual machine using the Remote - SSH extension in Visual Studio Code. In this post we will talk about some tips and tricks that you can use to make the most of your remote configuration.







Connect using Remote SSH



The Visual Studio Code Remote - SSH extension allows you to connect to a remote machine or virtual machine using SSH, and all from within VS Code. If the extension is not already installed, you can search for “remote ssh” in the “Extensions” (⇧⌘X) tab.







After installing the extension, you will see an indicator in the lower left corner of the status bar. This indicator tells you in what context VS Code (local or remote) works. Click on the indicator to display a list of remote extension commands.







SSH configuration file



In the previous post about remote SSH, we connected to only one machine and, when prompted, entered “user @ host”. If you regularly connect to multiple remote servers or local virtual machines, there is a better way to connect without having to remember all user names, addresses and additional configuration parameters.



OpenSSH supports the use of a configuration file to store all of your various SSH connections. To use the SSH configuration file, click on the remote indicator to invoke the remote commands, select “Open Configuration File”, and select the file that is located in “Users / {yourusername} /. Ssh / config”.







Here is an example SSH configuration file:



#      SSH: https://linux.die.net/man/5/ssh_config Host python-linux-vm HostName <vm address> User sana IdentityFile ~/.ssh/id_python_vm Host node-vm HostName <vm address> User sana Port 5522 IdentityFile ~/.ssh/id_node_vm
      
      





There are many other configuration options that you can specify in the SSH configuration file format. You will get the add-ons in this file, and you can click (⌃Space) for IntelliSense to learn more about configuration options.



Options used above:

Host An easy-to-remember alias for your host.
Hostname Server host name (you can use the server IP address).
User The user you specified to log in via SSH.
Port The port used to connect through SSH. The default port is 22, but if you specified a unique port, you can configure it here.
IdentityFile The location of the file where you saved your private key.
You can add information for all the hosts that you have. After saving the configuration file, you can see these nodes in Remote Explorer, as well as any folders that you open on this node. You can select an 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 am connected to my “python-linux-vm” remote machine, and Remote Explorer shows me the folders that I connected to in the past, as well as any redirected ports from the remote machine.







Proxycommand



Sometimes you may need to connect from your desktop or laptop computer to a remote computer through your company’s intranet or behind a firewall. In this case, you can use an intermediate server or jump-box . This type of configuration is useful if you are running a secure system that is configured to accept SSH connections only from a fixed set of hosts.



To use the jump-box configuration with the Remote - SSH extension, you can use the ProxyCommand



configuration parameter. This configuration will open the background SSH connection with the jump-box, and then connect through the private IP address to the target.



You can set the ProxyCommand



configuration parameter in the SSH configuration file as follows:



 # Jump box   IP- Host jump-box HostName 52.179.157.97 User sana IdentityFile ~/.ssh/jumpbox #     IP- Host target-box HostName <IP address of target> User sana IdentityFile ~/.ssh/target ProxyCommand ssh -q -W %h:%p jump-box
      
      





Controlmaster



If you connect to a remote SSH host using authentication methods other than key-based authentication, such as two-factor, password-based, or an SSH key with a passphrase, you may need to enter the required information several times.



Instead of opening multiple SSH connections, you can use the ControlMaster



option (only on macOS / Linux clients) to reuse an existing connection and reduce the number of times you need to enter your passphrase.



To use this feature, add the following to your SSH configuration file:



 Host * ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h-%p ControlPersist 600
      
      





Standalone Remote Computer



If you are limited by a firewall or your company is blocking your virtual machines and they cannot connect to the Internet, the Remote - SSH extension will not be able to connect to your virtual machine because VS Code must download a component called VS Code Server to the remote machine.



However, now you can solve this problem with a new user parameter in the Remote extension - SSH. If you enable the remote.SSH.allowLocalServerDownload



parameter, the extension will first install VS Code Server on the client, and then copy it to the server through SCP.



Note. This is currently an experimental feature, but it will be enabled by default in a future release.



Remote - SSH Nightly extension



If you want to test new updates and experimental features as soon as they become available, install the Remote extension - SSH Nightly (first remove the stable Remote-SSH extension). This is an overnight build of the extension, where we experiment with new features and settings before adding them to the stable version.



All Articles