SSH tunnel in Ubuntu

2

How do I create a ssh tunnel in Ubuntu that is persistent? I have tried a lot of ways but I haven't gotten it to work.

Stanciu Alexandru-Marian

Posted 2011-12-16T17:52:52.947

Reputation: 21

Answers

2

apt-get install autossh, then use autossh instead of ssh in the tunnel creation command line. If you set up public key auth it will be very persistent.

Eduardo I.

Posted 2011-12-16T17:52:52.947

Reputation: 495

How do i set up public key authentication?:D – Stanciu Alexandru-Marian – 2011-12-16T18:33:20.370

1>

  • Generate a public key: ssh-keygen -t dsa; 2. Use ssh-copy-id -i $HOME/.ssh/id_dsa.pub user@host
  • < – Vi. – 2011-12-16T18:38:24.023

    0

    You need to be more specific in your question. Anyway, you can look at the this link for more information about SSH tunnels in ubuntu.

    Khaled

    Posted 2011-12-16T17:52:52.947

    Reputation: 649

    I have two routers and i have to create a ssh tunnel between them. And the settings must remain after a reboot. I work with lxc containers, so my routers are actually lxc containers. – None – 2011-12-16T18:01:56.780

    0

    Something like ssh -N user@host -L 0.0.0.0:5555:127.0.0.1:5555 -R 0.0.0.0:5556:127.0.0.1:5556 - will forward port 5555 from us to them and port 5556 from them to us.

    Vi.

    Posted 2011-12-16T17:52:52.947

    Reputation: 13 705

    The command ssh -N user@host -L 0.0.0.0:5555:127.0.0.1:5555 -R i give in every container? And user@host is the name and ip of the end of the tunnel in wich i give this command? – Stanciu Alexandru-Marian – 2011-12-16T18:31:23.447

    Don't understand the question. What is a container here? – Vi. – 2011-12-16T18:38:47.753

    If you use that command (except of "-R"), ssh will open TCP port at your host (0.0.0.0:5555) that will be forwarded to 127.0.0.1:5555 on the remote host. – Vi. – 2011-12-16T18:40:18.873

    I have to create a tunnel between A and B. Where i give that command? In A and B changing what? A and B have IP addresses, and are connected directly. I have to create a ssh tunnel between them. Thats what i have to do. – Stanciu Alexandru-Marian – 2011-12-16T18:45:50.390

    Example: on host A you do "ssh user@hostB -L 6666:127.0.0.1:5555". On hostB you run program that listens 127.0.0.1:5555. When you connect program to 127.0.0.1:6666 at host A, it gets forwarded thought the SSH tunnel to host B, to the program that listens 127.0.0.1:555 at hostB. – Vi. – 2011-12-16T19:07:44.790

    May be use confuse SSH tunnel with VPN or PPP tunnel? – Vi. – 2011-12-16T19:08:51.980

    The problem that i have is the next one: there is C and she initiate a connection on A through port 11161 and this connection must go through the tunnel on port 1161 on B. C connects with ssh on A and the tunnel must be ssh. – Stanciu Alexandru-Marian – 2011-12-16T19:31:49.610

    i managed to make the tunnel work but i want to make it available when i restart the system. how can i do that? – Stanciu Alexandru-Marian – 2011-12-16T20:24:23.827

    Without using autossh. Please can somebody tell me a way? – Stanciu Alexandru-Marian – 2011-12-16T21:22:14.583

    You can create initscript (like in /etc/init.d) that will do something like while true; do ssh .....; sleep 5; done& at start. – Vi. – 2011-12-17T03:14:46.980

    (this will probably be a first sketch of "poor-man's autossh") – Vi. – 2011-12-17T03:28:00.717

    #!/bin/bash

    while true; do ssh -fNL monica.eth1:11161:joey:1161 joey ssh -fNL monica.eth2:11161:joey:1161 joey done& at start That's my script. I've saved him sshtunnel.sh in /etc/init.d, i gave him permissions chmod a+x sshtunnel.sh, but it doesn't work. I mean when i restart everything still the tunnel is missing and i have to create him manually. Any ideas why?(there are two command because the are two connection between joey and monica and if eth1 fail the second one must work and viceversa) – Stanciu Alexandru-Marian – 2011-12-17T07:38:09.467

    >

  • Missing separator between commands; 2. You can use single ssh command with multiple "-L" options: ssh -fN -L monica.eth1:11161:joey:1161 -L monica.eth2:11161:joey:1161 joey
  • < – Vi. – 2011-12-17T12:56:33.350

    #!/bin/bash while true; do ssh -fN -L monica.eth1:11161:joey:1161 -L monica.eth2:11161:joey:1161 joey; sleep 5; done& at start This is another version of the script. Still fails to create the tunnel at startup. – Stanciu Alexandru-Marian – 2011-12-17T13:21:09.670

    Debug it. Does it create the tunnel if you run the script from console? Are you sure it gets started? Write something like "2> /tmp/debug1.txt > /tmp/debug2.txt" at the end of script... – Vi. – 2011-12-17T20:20:11.270

    How can i be sure that it starts? I must confess it's something i didn't think. It's a command, or i have to give him some special wrights? – Stanciu Alexandru-Marian – 2011-12-18T01:12:29.663

    Write something like "echo hello > /tmp/hello_from_my_script" in the beginning. You will see if it does create the file at boot. On of ways to make your thing start at boot is to use initscript in /etc/init.d/ (with a symlink from etc/rc*.d/), or config file in /etc/init/ or entry in /etc/inittab. – Vi. – 2011-12-18T12:24:16.400