Comprehensive SSH Guide

O

Ohidur Rahman Bappy

MAR 22, 2025

SSH Cheat Sheet

Login via SSH with Password (Local Server)

$ ssh brad@192.168.1.29

Basic Server Setup

  • Create a folder and file
    $ mkdir test
    $ cd test
    $ touch hello.txt
    
  • Install Apache
    $ sudo apt-get install apache2
    

Generate SSH Keys (Local Machine)

$ ssh-keygen

Add Key to Server in One Command

$ cat ~/.ssh/id_rsa.pub | ssh brad@192.168.1.29 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Copy a File to the Server using SCP

$ touch test.txt
$ scp ~/test.txt brad@192.168.1.29:~

Digital Ocean

Setting Up a Droplet

  • Create Account -> Create Droplet

Create Keys for Droplet (id_rsa_do)

$ ssh-keygen -t rsa

Add Key When Creating Droplet

Logging In

  • Initial login
    $ ssh root@doserver
    
  • If it fails
    $ ssh-add ~/.ssh/id_rsa_do
    
  • Successful login
    $ ssh root@doserver
    

Update Packages

$ sudo apt update
$ sudo apt upgrade

Create a New User with Sudo Privileges

$ adduser brad
$ id brad
$ usermod -aG sudo brad
$ id brad

Login as New User

$ ssh brad@doserver

Configure SSH for New User

  • Log back in as root
    $ ssh root@doserver
    $ cd /home/brad
    $ mkdir .ssh
    $ cd .ssh
    $ touch authorized_keys
    $ sudo nano authorized_keys  # Paste the id_rsa_do.pub key
    

Secure SSH Configuration

  • Edit SSH config
    $ sudo nano /etc/ssh/sshd_config
    
  • Adjust settings
    PermitRootLogin no
    PasswordAuthentication no
    
  • Reload sshd service
    $ sudo systemctl reload sshd
    

Adjust File Permissions

  • Change ownership and permissions
    $ sudo chown -R brad:brad /home/brad
    $ chmod 700 /home/brad/.ssh
    

Install Apache and Test

$ sudo apt install apache2 -y

GitHub Integration

Generate a GitHub Key (On Server)

$ ssh-keygen -t rsa  # Name it id_rsa_github

Add New SSH Key

$ ssh-add /home/brad/.ssh/id_rsa_github

If You're Prompted About Auth Agent

$ eval `ssh-agent -s`

Clone a Repository

$ git clone git@github.com:bradtraversy/react_otka_auth.git

Node.js Setup

Install Node.js

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs

Install Node Dependencies

$ npm install

Start Development Server

  • Visit: ip:3000
$ npm start

Build React App

$ npm run build

Deploy to Web Server

$ sudo mv -v /home/brad/react_otka_auth/build/* /var/www/html