Quick question - I run two linux boxes, one my own desktop and the other my VPS. For security reasons on the VPS end I opted for socket connections to MySQL (/var/run/mysqld/mysql.sock
). I know I can tunnel like this: ssh -L 3307:127.0.0.1:3306 [email protected]
if I set up the remote sql server to listen on some port, but what I want to know is can I do something like: ssh -L /path/to/myremotesqlserver.sock:/var/run/mysqld/mysql.sock
thereby tunnelling two sockets, as opposed to two ports?
A perfectly acceptable solution would also be to forward a local port to the remote socket file, but where possible I'm trying not to have tcp servers running on the remote box.
(and yes, I know tcp would be easier).
Altough in the time, when the question was asked, it was really impossible, but it is possible nowadays.
You can to both: UNIX=>TCP and UNIX=>UNIX forwarding.
For example:
It is possible since OpenSSH 6.7.
Forward a local socket on demand
socat
at both endsthen
stolen from forwarding unix domain sockets with ssh and socat
i haven't done this, but i would try with socat. maybe something like:
again, i have never done anything like this.
No more socat is needed since ssh 6.7. You can forward unix domain sockets directly like:
More info: https://medium.com/@dperny/forwarding-the-docker-socket-over-ssh-e6567cfab160
Yes, you can using socat.
First do the TCP tunnel with SSH. Then use socat like this:
socat unix-listen:/var/run/mysqld/mysqld.sock,fork,unlink-early tcp:127.0.0.1:3306
Then give permissions to new created socket (chmod 777 may be)
Another modification of @mpontes'/@javier's answer that
Cleaner
PROS
Feature
-f
option is not used, you can either use a public key and run in the background via&
or you could log in interactively and use Ctrl+Z and use the same$!
to store the pid.CONS
-f
ssh option, as you'll lose the pid of ssh that way. This method relies on running in the foreground and Ctrl+C to kill.Explanation
socat ...&
- run socat in background on remote serverpid=$!
- store the pidtrap kill\ $pid 0
- runkill $pid
on bash terminationwhile :; sleep...
- sit in an infinite loopecho -ne \ \b
- Echo space followed by backspace. This fails as soon as the ssh is disconnected. With asleep 5
, this means thatsocat
can run up to 5 seconds after sshNote: Actually tested using docker, port
2375
,/var/run/docker.sock
, and environment variableDOCKER_HOST='tcp://localhost:2375'
, but should work for mysql all the sameUpdate
Using SSH Controls, you can use the
-f
flag using my way, just add the following flagsAnd you'll get
Now you can terminate all the controlled sessions using
The
-o
options can be saved in your.ssh/config
file, or you can use -S instead (but you'll still need-o ControlMaster
)Elaborating on Javier's answer, this works for me:
ssh -f [email protected] -L 9999:localhost:9999 "socat TCP-LISTEN:9999,fork,bind=localhost UNIX-CONNECT:/var/run/mysqld/mysql.sock"
You need
fork
in order to be able to connect multiple times without socat dying once you close the first connection. Also, the way socat lets you specify an address to bind to is through thebind
option, not as an address before the port.After this, just connect to
localhost:9999
normally as you would. Then to teardown the tunnel:ssh -f [email protected] "killall socat"
(or something similar, you can do more elaborate things that involve keeping socat's PID)