Forward local internet through ssh tunnel

Install http proxy on local machine, and bind it to port 8080

ssh -R 8080:<local IP>:8080 user@<remote IP>
export http_proxy=http://<local IP>:8080
export https_proxy=http://<local IP>:8080
sudo visudo
#append text
Defaults env_keep = "http_proxy https_proxy ftp_proxy"

Now wget will work and so will sudo apt-get so you can install packages.

Just adding some more and clear steps

Do the setup as follows:

Setup on Host A:

  1. Install proxy server Squid on Host A . By default Squid listens on port 3128.
    1. yum install squid
  2. Comment the http_access deny all then add http_access allow all in /etc/squid/squid.conf
  3. If Host A itself uses some proxy say to connect to internet then also add that proxy to /etc/squid/squid.conf as follows:
  refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
  cache_peer parent 8080 0 no-query default
  never_direct allow all

Setup on Host B:

  1. Add the following entries to /etc/environment
  export http_proxy=
  export https_proxy=
  1. source /etc/environment

Now our setup is complete.

Creating SSH tunnel with Remote port forwarding

  • Run the following SSH command from Host A: ssh -R 3129:localhost:3128 user@HostB
  • If you want to make persistent SSH tunnel, you can use autossh as follows: autossh -M 20000 -f -NT -R 3129:localhost:3128 user@HostB
    • For above autossh command to work, you should be having SSH Keys setup from HostA to HostB
  • This will allow Host B to access the internet through Host A.

Checking the internet:

  1. Run the following command from Host B: wget

Traffic flow diagram :

Enter your comment:
85 -4᠎ =​
  • linux/networking/tunnel_internet_through_ssh.txt
  • Last modified: 2019-10-31 09:05
  • (external edit)