SSH to your Drupal Forge development environment

Submitted by Darren Oh on

The SSH protocol gives you a secure way to run commands and use a terminal on a remote computer. With SSH remote access, you can control a remote VS Code server from VS Code running on you own computer. You can create shortcut commands on your computer that run commands on a remote computer in one step instead of requiring multiple steps to open a terminal on the remote computer. And an SSH session on the command line is more stable and reliable than a terminal emulator running in a browser window.

Currently DevPanel does not offer a way to open an SSH port to your Drupal Forge development environment. But with the magic of reverse tunneling, you can open an SSH port to your Drupal Forge development environment yourself.

Set up SSH

Start by getting SSH working.

First, you need to add an SSH port to your Drupal Forge development environment. In the VS Code terminal, enter the following command:

sudo nano /etc/ssh/sshd_config

Uncomment the line that says #Port 22 so it becomes just Port 22. Then restart SSH:

sudo service ssh restart

Second, add an authorized public key to the www user. If you don’t have a public key on your local computer, use a tool such as ssh-keygen to create one. Then copy it to ~/.ssh/authorized_keys in your Drupal Forge development environment. You may have to create the ~/.ssh directory first:

mkdir -m 700 ~/.ssh
nano ~/.ssh/authorized_keys

Set up ngrok

Now set up ngrok. ngrok is a command line tool that forwards ports to a random ngrok domain.

First, get an ngrok account. A free developer account lets you forward ports from one computer at a time.

Second, install ngrok in your Drupal Forge development environment. Open the VS Code terminal and enter the following commands:

curl -sSL https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null
echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list >/dev/null
sudo apt-get update
sudo apt-get install ngrok

Third, go to your ngrok dashboard to get an authentication token, and run the command to add it to your Drupal Forge development environment.

Token screen of ngrok dashboard.
ngrok config add-authtoken 25JHZiSCb0ftF2rfVa7RqBGxUNi_au4JJ9F4zc5WnS3wPQZc

Fourth, start ngrok. Run the following command to forward just the SSH port:

ngrok tcp 22

You now have an SSH port on an ngrok domain:

ngrok screen shot showing forwarding domain and port.
In this screen shot, the ngrok domain is 2.tcp.ngrok.io and the SSH port is 18933.

Test your SSH port

To confirm that SSH is set up correctly, try connecting to the ngrok domain and port from your own computer. Note that you must connect as the www user.

ssh -p 18933 www@2.tcp.ngrok.io

The port and the domain come from the ngrok connection screen in the terminal of your Drupal Forge development environment.

Tunnel to your own server

If you have a server that is reachable from your Drupal Forge development environment, you don’t need ngrok. You can run a reverse SSH tunnel. After you set up SSH in your Drupal Forge development environment, run

ssh -4NR localhost:2200:localhost:22 user@yourdomain.com

This will forward port 22 from your Drupal Forge development environment to port 2200 on your server (because port 22 on your server is bound to its own SSH server). The server will only accept SSH connections from localhost. If you need your server to accept SSH connections from anywhere, change the command to

ssh -4NR 2200:localhost:22 user@yourdomain.com

SSH servers can be configured not to accept forwarded ports, so this will not work with every server.