How to SSH using Node.js

How to SSH using Node.js

I build IoT applications on a daily basis at RAZRLAB, and every once in a while, I do need the internal tools to perform some functions on servers, be it executing commands or forwarding ports. For this, I found a neat little tool node-ssh which helps establish ssh tunnels to servers with key authentication.

I build IoT applications on a daily basis at RAZRLAB, and every once in a while, I do need the internal tools to perform some functions on servers, be it executing commands or forwarding ports. For this, I found a neat little tool node-ssh which helps establish ssh tunnels to servers with key authentication.

Let’s Get Started

Let’s create a new folder and set up a fresh npm repository

npm init --y

Install Dependencies

We are going to use the following dependencies:

  • node-ssh — an extremely lightweight Promise wrapper for ssh2
  • dotenv — a zero-dependency module that loads environment variables from a .env file into process.env

Environment Variables

We will create our SSH tunnel using private key authentication for that we will create an SSH key pair with a passphrase.

I would recommend using your cloud providers SSH key access management to securely manage access to your servers. Click here to know how to manage SSH key access through Digital Ocean.

Now, let’s specify paths and information in the .env file

## .env

SSH_host=
SSH_user=
SSH_privatekey=
SSH_passphrase=

Environment variable definitions:

  • SSH_host : Host IP Address or DNS name of the server
  • SSH_user : Host username for logging in
  • SSH_privatekey : path to the private key
  • SSH_passphrase : passphrase for the key for an added layer of security

Test Setup

We will access the server and install a Redis server. We will use an Ubuntu machine and leverage the apt package manager to install the Redis server.

  • SSH as we always do 😁
ssh <username>@<host>
  • Install Redis Server
sudo apt-get install redis-server

This will download and install Redis and its dependencies. It will also export a CLI application called redis-cli to interact with the Redis server.

nodejs remote-access javascript ssh web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Hire Nodejs Developer

Looking to **hire NodeJS Developer**[https://www.dataeximit.com/node-js-development/](https://www.dataeximit.com/node-js-development/ "https://www.dataeximit.com/node-js-development/") to build a perfect back-end to your website or...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.