Creating Remote Connections: Home Computer to Campus Intranet via Cloud

2 minute read

Published:

This guide explains how to set up remote connections from a computer on public Wi-Fi to a campus intranet server using a cloud computer, including SSH key generation, autossh installation, and automated service configuration.

Tutorial: Reverse SSH - Home Computer Connecting to Campus Intranet Server

This comprehensive guide provides step-by-step instructions on establishing remote connections for a computer connected to a public Wi-Fi network. The connections will be orchestrated through the use of a cloud-based computer, such as Amazon Cloud.

Terminology

  • A: Refers to the computer connected to public Wi-Fi.
  • B: Designates the cloud computer.
  • C: Represents the laptop at home.

Setting Up the Connection from A to B:

  1. Generate an SSH Key Pair: Begin by creating an SSH key pair on computer A to facilitate passwordless login to computer B. Execute the following command:
    ssh-keygen -t rsa # Press Enter for all prompts. The generated id_rsa.pub file will be located in the ~/.ssh/ directory.
    
  2. Copy Public Key: Copy the public key from computer A to computer B using this command:
    ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@666.666.666.666 -p 22
    
  3. Install Autossh: Install autossh to guarantee automatic reconnection in the event of a disruption in the reverse SSH connection between A and B:
    apt-get install autossh
    
  4. Test Connection: Verify the success of the autossh + passwordless login setup by running the following command:
    autossh -M 9991 -NfR 8990:localhost:22 ubuntu@666.666.666.666
    

    If the command on computer A runs successfully, and on computer B, you observe the port numbers in the red box when executing the command watch -n 1 netstat -tnlp, then the test is successful.

Configuring the Autossh Service:

  1. Automate Autossh Startup: Automate the startup of autossh when computer A is powered on. Begin by creating an autossh.service file:
    vim /lib/systemd/system/autossh.service
    

    Insert the following content into the autossh.service file:

    [Unit]
    Description=Auto SSH Tunnel
    After=network-online.target
    
    [Service]
    User=YOUR_USER_NAME
    Type=simple
    ExecStart=/usr/bin/autossh -NR 9888:localhost:22 -i /home/YOUR_USER_NAME/.ssh/id_rsa ubuntu@666.666.666.666 -p 22 >> /dev/null 2>&1
    ExecReload=/bin/kill -HUP $MAINPID
    ExecStop=/bin/kill -TERM $MAINPID
    KillMode=process
    Restart=no
    
    [Install]
    WantedBy=multi-user.target
    WantedBy=graphical.target
    
  2. Verify Configuration: Check if the autossh service is successfully configured:
    systemctl enable autossh
    systemctl start autossh
    

    Confirm the service’s status:

    systemctl status autossh
    

    If the service starts successfully, when running the command watch -n 1 netstat -tnlp on computer B, you should see the port numbers in the red box.

Connection from C to A:

To establish a connection from laptop C to computer A through computer B:

  1. Connect to Computer B: Initiate an SSH connection to computer B using the following command:
    ssh ubuntu@666.666.666.666
    
  2. Access Computer A: Open another terminal tab and connect to computer A:
    ssh -p 9888 YOUR_USER_NAME@127.0.0.1
    

    You should now have a remote connection from laptop C to computer A via computer B, enabling seamless access to your campus intranet server.