Coder Social home page Coder Social logo

aws-secure-tunnel's Introduction

Secure SSH Tunneling to AWS EC2 Instance

Prerequisite

Client: Raspberry Pi, Bearbone or etc

  • Linux OS
  • OpenSSH
  • autossh (optional)
  • Generated RHA Key (id_rsa, id_rsa.pub in ~/.ssh): If you don't have these, simply run ssh-keygen without passphrase

Server: AWS EC2 or any Linux server

Create user account named tunnel

For security reasons, this account should not be a sudo account.

  • Amazon Linux 2 or Amazon Linux AMI EC2 Instance
[ec2-user ~]$ sudo adduser tunnel
  • AWS Ubuntu EC2 Instance
[ubuntu ~]$ sudo adduser newuser --disabled-password

Login tunnel account

sudo su - tunnel

Add SSH Public key of clients

  • make authorized_keys
mkdir .ssh
chmod 700 .ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
  • add public key of client to authorized_keys
nano ~/.ssh/authorized_keys

Installation

This script should be provided with [REMOTE-IPADDRESS] and [REMOTE_PORT].

Full installation script: case of autossh

curl --insecure -o- \
https://raw.githubusercontent.com/eunchurn/aws-secure-tunnel/scripts/[email protected] \
| bash -s [REMOTE_IPADDRESS] [REMOTE_PORT]

Full installation script: case of ssh

curl --insecure -o- \
https://raw.githubusercontent.com/eunchurn/aws-secure-tunnel/scripts/[email protected] \
| bash -s [REMOTE_IPADDRESS] [REMOTE_PORT]

Contents of these script files

secure-tunnel@aws file in /dev/default/

  • TARGET is your EC2 IP address.
  • USERNAME is tunnel user account.
  • REMOTE_PORT is the port number of EC2 that you need to connect to the client from the EC2 instance.
TARGET=$1
LOCAL_ADDR=0.0.0.0
LOCAL_PORT=22
REMOTE_PORT=$2
USERNAME=tunnel
SSH_TARGET_PORT=22

[email protected] file in /dev/systemd/system/

In case of autossh

[Unit]
Description=Setup a secure tunnel to %I
After=network.target

[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=/etc/default/secure-tunnel-autossh@%i
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -o "ExitOnForwardFailure=yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NR ${REMOTE_PORT}:${LOCAL_ADDR}:${LOCAL_PORT} -p ${SSH_TARGET_PORT} ${USERNAME}@${TARGET}

# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always

[Install]
WantedBy=multi-user.target

In case of ssh

[Unit]
Description=Setup a secure tunnel to %I
After=network.target

[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=/etc/default/secure-tunnel@%i
ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L ${LOCAL_ADDR}:${LOCAL_PORT}:localhost:${REMOTE_PORT} ${TARGET}

# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always

[Install]
WantedBy=multi-user.target

Example

AWS Server

  • DNS: ec2-13-124-180-92.ap-northeast-2.compute.amazonaws.com
  • IP Address: 13.124.180.92
  • Port: 10022
  • Client Username: client-user

Installation autossh tunnel system

  • In your client
curl --insecure -o- \
https://raw.githubusercontent.com/eunchurn/aws-secure-tunnel/scripts/[email protected] \
| bash -s 13.124.180.92 10022
  • Make sure your tunnel daemon alive
systemctl status secure-tunnel@aws
  • Or check log: -r is reverse, -f is follow
journalctl -u secure-tunnel@aws -r

Check tunnel and Log in client from your EC2 instance

  • Make sure PORT is open.
sudo lsof -i:10022 | grep IPv4
  • Connect to SSH and log in through the tunnel.
ssh client-user@localhost -p 10022
  • Enjoy

aws-secure-tunnel's People

Contributors

eunchurn avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

eunchurnpark

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.