LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2024, 08:06 PM   #1
nedlud
LQ Newbie
 
Registered: Oct 2004
Posts: 26

Rep: Reputation: 0
How to establish ssh from remote firewalled PC to local machine, enabling local browsing on remote LAN


Apologies if the terminology is incorrect, suggestions for better thread title welcome.

I administer (a grand word for my amateur efforts) a server (oakdrum) on a friend's (christine) LAN, which is used to backup her laptop (x1-laptop), and for syncthing, DLNA server, samba etc.

She lives 500 miles from me. After initial setup at her house with physical access, I setup port forwarding on her router so that I could ssh into (oakdrum), and (x1-laptop) for remote assistance via vnc. Having done so, I could also access the web GUI (no telnet or ssh available) of her router from my machine (lutyens) with:

Code:
nedlud@lutyens:~ssh -v -D 24080 -f -C -q -N oakdrum
... and configuring a manual proxy in my browser:

Code:
Manual proxy configuration:
  SOCKS Proxy  127.0.0.1  Port 24080
      check the box for "SOCKS v5"
... then access her router GUI at 192.168.1.1 on my local machine (lutyens)

This week after a fault her ISP sent her a new router. She's tech-phobic, but swapped it out plug for plug with the broken one. She has WAN access with the default config of course, but (oakdrum) has a different IP so her backup doesn't work, and now I can't get to her LAN.

The limit of her capability is copy pasting a string in the terminal.

assume:
I will temporarily forward port 33022 on my firewall to port 22 on my machine, and enable password only ssh login.
My username on my machine (lutyens) is nedlud.
My dynamic dns is nedlud.dyndns.net


I'm after the cli string that she can use on her laptop (x1-laptop) to ssh to my machine (lutyens), and anything I need to run on (lutyens), such that I can browse to her router config page, and re-establish port forwarding.

MTIA.
 
Old 04-27-2024, 08:28 PM   #2
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 4,463
Blog Entries: 7

Rep: Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561
You probably want to use a reverse SSH session.

You'll get many hits if you search for it, but here's a random one which explains the basics of it: https://ryan.himmelwright.net/post/s...se-ssh-tunnel/
 
Old 04-27-2024, 11:33 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by rkelsen View Post
You probably want to use a reverse SSH session.
Yes, that'd be the way to go. Have her system connect to yours using -R option.

Code:
ssh -f -N \
        -i ~/.ssh/some.key.ed25519 \
        -R 2222:localhost:22 \
        -l christine \
        lutyens.example.com
Much of that can actually be put in her ~/.ssh/config file so that she only has to type a 'ssh lutyens' or some other shortcut, and that can in turn be put in a script for a .desktop file to click on. For example:

Code:
Host lutyens lutyens.example.com
	HostName lutyens.example.com
	User christine
	RemoteForward 2222 localhost:22
	ForkAfterAuthentication yes
	SessionType none
	IdentitiesOnly yes
	IdentityFile /home/%u/.ssh/some.key.ed25519

Host *
	ServerAliveCountMax 3
	ServerAliveInterval 30
	ConnectTimeout 2
Then once that connection is established, connect almost the same as before, but to localhost instead of oakdrum and specify the port of the tunnel:

Code:
ssh -D 24080 -f -C -q -p -N 127.0.0.1
Setting up reverse tunnels is rather simple but not deceptively so.

Last edited by Turbocapitalist; 04-27-2024 at 11:46 PM. Reason: SessionType and ForkAfterAuthentication
 
Old 04-28-2024, 06:23 PM   #4
nedlud
LQ Newbie
 
Registered: Oct 2004
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Yes, that'd be the way to go. Have her system connect to yours using -R option.
Thank you very much. Hoping it will go well and quickly when I do it for real, I have practised this from my laptop (on public wifi) to simulate, and a VM on my main machine, which I'll use for the real exercise, to avoid exposing my main machine, enabling password ssh, having to provide/change my password etc.

The only things I had to tweak were adding her username when setting up the tunnel for the browser, and specifying the port.

Substituting what worked in my practice run with the dummy hosts and users in the OP, this worked:

christine copy pastes:

Code:
christine@x1-laptop:~ssh -f -N -p 33022 -R 2222:localhost:22 nedlud@nedlud.dyndns.net
nedlud@nedlud.dyndns.net's password:
christine@x1-laptop:~
Then I:

Code:
nedlud@lutyens:~ ssh -D 24080 -f -C -q -N -p 2222 christine@127.0.0.1
christine@127.0.0.1's password:
nedlud@lutyens:~
I could then, with browser settings:

Code:
Manual proxy configuration:
  SOCKS Proxy  127.0.0.1  Port 24080
      check the box for "SOCKS v5"
...browse "from" (x1-laptop), proved by visiting whatismyip.com.

So I'm confident that when I do this for real, I'll be able to access her router admin page, which is what I need to achieve. Don't think I'm missing anything, and I'll mark as solved when it's done.

Thanks again!
 
Old 04-29-2024, 06:35 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Just a FYI that you have posted your real username and URL which essentially points a neon target at your server. Changing ssh ports is not a real deterrent.
 
Old 04-29-2024, 01:43 PM   #6
nedlud
LQ Newbie
 
Registered: Oct 2004
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Just a FYI that you have posted your real username and URL which essentially points a neon target at your server. Changing ssh ports is not a real deterrent.

Thanks Michael, Good of you to take the trouble. However all host names, usernames and ports in my OP are dummies, though accurately describing the commands entered.

I hope to do this thing this evening, and assuming it's successful, I'll add a post describing everything, with "generic specifics", if that makes sense.
 
Old 04-29-2024, 07:29 PM   #7
memilanuk
Member
 
Registered: Sep 2010
Location: Washington state, USA
Distribution: Ubuntu among others
Posts: 69

Rep: Reputation: 3
Interesting scenario!

I've not a lot of direct experience with ssh tunneling myself... but would having something like tailscale installed on the respective machines help at all? That way they can all 'see' each other on a flat VPN network, with no port forwarding at the router level. Seems like it'd be near ideal for a use case like this...
 
Old 04-29-2024, 11:13 PM   #8
friendlysalmon8827
Member
 
Registered: Dec 2023
Distribution: Anfroid,Debian
Posts: 103

Rep: Reputation: 6
I'd strongly recommend that the OP go back through his original post and redact the host names and other potentially exposing information. It seems to me that the OP would be best served by investing in some high-quality uninterruptible power supplies a couple of good brands are CYBERPower and APC the latter of which is a synder electric product line.
 
Old 04-30-2024, 05:46 AM   #9
nedlud
LQ Newbie
 
Registered: Oct 2004
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by memilanuk View Post
Interesting scenario!

I've not a lot of direct experience with ssh tunneling myself... but would having something like tailscale installed on the respective machines help at all? That way they can all 'see' each other on a flat VPN network, with no port forwarding at the router level. Seems like it'd be near ideal for a use case like this...
Thanks for the suggestion, but I don't want to use anything needing third party server, or proprietary (especially with it's roots in Google) if I can help it. I usually use vnc over ssh to provide remote assistance to (christine) so she has to do literally nothing to set up the connection. I'm experimenting with self hosted RustDesk, but haven't figured out how the ssh keys work yet.

Quote:
Originally Posted by friendlysalmon8827 View Post
I'd strongly recommend that the OP go back through his original post and redact the host names and other potentially exposing information.
As in https://www.linuxquestions.org/quest...6/#post6498905, all host and user names are dummies. Am I missing something?

Quote:
Originally Posted by friendlysalmon8827 View Post
It seems to me that the OP would be best served by investing in some high-quality uninterruptible power supplies a couple of good brands are CYBERPower and APC the latter of which is a synder electric product line.
I'm not seeing how that's directly relevant?

Christine won't be ready to do this for a few days, and when it's done I'll post full details.

Last edited by nedlud; 04-30-2024 at 05:47 AM.
 
Old 05-14-2024, 11:43 AM   #10
nedlud
LQ Newbie
 
Registered: Oct 2004
Posts: 26

Original Poster
Rep: Reputation: 0
This worked for me. I've explained this as best I can, but clearly I only have a tenuous grasp of how it's working, so suggestions for corrections or improvements are welcome.

To establish an ssh connection to (first, from) a remote machine behind third party firewall, with no pre-existing setup on the remote machine (other than in assumptions below). This could be due to circumstances described in the OP, or perhaps remote machine is on hotel or coffee shop etc. wifi. I (nedlud) wish to provide remote assistance to (christine).

Assumptions:

- The remote machine has a public IP, which may be unknown to you.
- An ssh server is running on the remote machine, which may be on a non-standard port, allowed by the remote machine's internal firewall.
- Remote machine ssh server is configured for password authentication and you know the username and password of the remote user, or for public key access and you have the private key.
- The remote user can open a terminal and copy/paste, or type a string, but nothing more technical.


LOCAL machine:
hostname: lutyens
username: nedlud
ssh port: 33022
dynamicdns: nedlud.ddns.net

In my case host (lutyens) is a VM with password authenticated ssh, to avoid having to change anything on main machine, and to make things simpler for (christine)

REMOTE machine:
hostname: x1-laptop
username: christine
ssh port: 55022


From remote host (x1-laptop), user (christine) runs in terminal the string that (nedlud) has sent her by email or other means:

Code:
christine@x1-laptop:~$ ssh -f -N -p 33022 -R 2222:localhost:55022 nedlud@nedlud.ddns.net
nedlud@nedlud.ddns.net's password:
christine@x1-laptop:~
christine@x1-laptop:~$ ls .ssh | grep key
christines-key-on-x1-carbon.id_ed25519
christine@x1-laptop:~$

ssh options with extracts from man ssh:

-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background...
-N Do not execute a remote command. This is useful for just forwarding ports...
-p 33022 Port to connect to on the remote host. (i.e. remote from host (x1-laptop) in this example, i.e. your local machine (lutyens). ssh is on port 33022. Omit for standard port 22).
-R 2222:localhost:55022 x1-laptop [bind_address:]port:host:hostport - 2222 is arbitrary, any non privileged port (above 1024). (x1-laptop) ssh is on 55022. Use 22 for standard ssh port 22.


(nedlud) checks it worked:

Code:
nedlud@lutyens:~$ netstat -tulpn | grep 2222
tcp        0      0 127.0.0.1:2222          0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:2222                :::*                    LISTEN      -                   
nedlud@lutyens:~$

(nedlud) can now ssh to (x1-carbon) with:

Code:
nedlud@lutyens:~$ ssh -p 2222 christine@localhost
christine@localhost's password:
christine@x1-carbon:~$
OR
Code:
nedlud@lutyens:~$ ssh -p 2222 -i .ssh/christines-key-on-x1-carbon.id_ed25519 christine@localhost
Enter passphrase for key '/home/nedlud/.ssh/christines-key-on-x1-carbon.id_ed25519':
christine@x1-carbon:~$
Or as in OP, (nedlud) can now browse via (x1-carbon) with proxied browser on whichever LAN (x1-carbon) is on, e.g. to access router firewall config page and establish port forwarding to enable direct ssh to (x1-carbon), initiated from (lutyens) as usual and provide remote assistance.

Code:
Manual proxy configuration:
  SOCKS Proxy  127.0.0.1  Port 24080
      check the box for "SOCKS v5"

Code:
nedlud@lutyens:~$ ssh -D 24080 -f -C -q -N -p 2222 christine@localhost
christine@localhosts's password:
nedlud@lutyens:~$

ssh options with extracts from man ssh:


-D [bind_address:]port
Specifies a local “dynamic” application-level port forwarding. This works by allocating a socket to listen to port
on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the
connection is forwarded over the secure channel, and the application protocol is then used to determine where to
connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a
SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be specified in the con‐
figuration file.
-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background...
-C Requests compression of all data...
-q Quiet mode. Causes most warning and diagnostic messages to be suppressed.
-N Do not execute a remote command. This is useful for just forwarding ports...
-p 2222 christine@localhost specifies the arbitrary port used for the reverse tunnel established from (x1-carbon)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to establish ssh tunnel from remote firewalled PC for VNC remote assistance? nedlud Linux - Networking 6 05-25-2023 11:30 AM
our LAN's proxy server is firewalled disabling a movie download..plz help rs_vijay Linux - Networking 2 11-01-2007 01:35 AM
Ssh connection to a firewalled machine. assasukasse Linux - Networking 10 06-20-2007 11:58 AM
Cannot SSH to remote firewalled terminal? ajeetraina Linux - Networking 1 06-14-2007 08:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration