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 development environment 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 own 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 ngrok developer account lets you forward ports from one computer at a time.

Second, install ngrok in your Drupal Forge development environment. Open the development environment 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 an account on a public SSH server, you may not need ngrok. You can try running a reverse SSH tunnel. In addition to the previous instructions for setting up SSH, you will need to generate a public-private key pair in your Drupal Forge development environment and add the public key to the authorized keys list of your account on the public SSH server. After you set up SSH, run the SSH reverse tunneling command in the terminal of your Drupal Forge development environment.

ssh -4NR localhost:2200:localhost:22 account@example.com

This will forward port 22 from your Drupal Forge development environment to port 2200 on example.com (because port 22 on example.com is bound to its own SSH server). example.com will only accept SSH connections from itself. If you need example.com to accept SSH connections from your own computer, replace the first localhost with your computer’s domain name or IP address. If you need example.com to accept SSH connections from anywhere, omit the first localhost.

ssh -4NR 2200:localhost:22 account@example.com

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