Skip to content

Instantly share code, notes, and snippets.

@kamal-hossain
Forked from bradtraversy/ssh.md
Last active July 20, 2021 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamal-hossain/540334dce7a263e291c33af473e65002 to your computer and use it in GitHub Desktop.
Save kamal-hossain/540334dce7a263e291c33af473e65002 to your computer and use it in GitHub Desktop.
SSH Snippets

SSH Cheat Sheet

Login via SSH with password (LOCAL SERVER)

$ ssh brad@192.168.1.29

Login via SSH with key (remote server)

ssh -i "react-counter-app.pem" ubuntu@test.compute.amazonaws.com

Logout from SSH

$ logout

Create folder, file, install Apache (Just messing around)

$ mkdir test

$ cd test

$ touch hello.txt

$ sudo apt-get install apache2

Generate 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

SCP command

Create & copy a file to the server using

$ touch test.txt

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

Copy a file to the Server with credential

scp -i id_rsa_socket2 app.js root@socket2.tapcom.com:/root/DashSocket

Copy a file from server using scp to local machine

scp -i id_rsa_socket2 root@socket2.tapcom.com:/root/DashSocket/app.js ./

copy an entire folder from server using SCP

scp -r root@test.example.com:/root/dist ./localPCFolder

Becareful with nested node_modules folder

Start nodejs server and exit from ssh

nohup node app.js & exit

You have to kill the process on next SSH login by pkill -f node, (you can use other ways)

DIGITAL OCEAN

Create account->create droplet

Create Keys For Droplet (id_rsa_do)

$ ssh-keygen -t rsa

Add Key When Creating Droplet

Try logging in

$ ssh root@doserver

If it doesn't work

$ ssh-add ~/.ssh/id_rsa_do (or whatever name you used)

Login should now work

$ ssh root@doserver

Update packages

$ sudo apt update

$ sudo apt upgrade

Create new user with sudo

$ adduser brad

$ id brad

$ usermod -aG sudo brad

$ id brad

Login as brad

> ssh brad@doserver

We need to add the key to brads .ssh on the server, log back in as root

$ ssh root@doserver

$ cd /home/brad

$ mkdir .ssh

$ cd .ssh

$ touch authorized_keys

> sudo nano authorized_keys (paste in the id_rsa_do.pub key, exit and log in as brad)

Disable root password login

$ sudo nano /etc/ssh/sshd_config

  • Set the following

    PermitRootLogin no

    PasswordAuthentication no

  • Reload sshd service

    $ sudo systemctl reload sshd

Enable root password login

  • Create password for root

    sudo su
    passwd root
    nano /etc/ssh/sshd_config
    
  • Edit the ssh_config file with

    PermitRootLogin yes
    PasswordAuthentication yes
    #PasswordAuthentication no
    
  • Restart sshd configuration file

    service sshd restart

Change owner of /home/brad/* to brad

$ sudo chown -R brad:brad /home/brad

May need to set permission

$ chmod 700 /home/brad/.ssh

Install Apache and visit ip

$ sudo apt install apache2 -y

Github

Generate Github Key(On Server)

$ ssh-keygen -t rsa (id_rsa_github or whatever you want)

Add new key

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

If you get a message about auth agent, run this and try again

```$ eval `ssh-agent -s````

Clone repo

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

Install Node

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

$ sudo apt-get install -y nodejs

Install Dependencies

$ npm install

Start Dev Server and visit ip:3000

$ npm start

Build Out React App

$ npm run build

Move static build to web server root

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment