manywaypark's Blog
개발, 검색, 함수

'tunneling'에 해당되는 글 2건

  1. 2018.03.12 ssh reverse tunneling
  2. 2015.09.16 Reverse SSH Tunneling

ssh reverse tunneling

tips & tricks/Linux/Unix 2018. 3. 12. 16:01 by manywaypark

기본적으로 ssh, autossh 등을 사용하면 됨. 

systemd 내에서 autossh를 쓸 경우는 좀 tricky한 상황이 생기는 듯 (portforwarding 환경에서는 port forwading에 실패했을 경우 재시작할 때 제대로되지 않는다.

autossh는 자동재시작 등의 기능이 있는데 이것은 systemd에서 제공하는(아주 잘 하는) 기능이다. 두개를 섞으면 좀 이상해지는 것으로 보인다.

좀 검색을 해보니 ssh + systemd가 제대로 잘 동작하는 것으로 보인다. 

좀더 시간을 두고 해결하는 것도 고려해보자.


/etc/systemd/system/secure-tunnel@.service 파일:


[Unit]

Description=Setup a secure tunnel to %I

After=network.target


[Service]

Environment="LOCAL_ADDR=localhost"

EnvironmentFile=/etc/default/secure-tunnel@%i

ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -NR ${LOCAL_PORT}:localhost:${REMOTE_PORT} ${TARGET} -p ${TARGET_PORT}


# Restart every >2 seconds to avoid StartLimitInterval failure

RestartSec=5

Restart=always


[Install]

WantedBy=multi-user.target


/etc/default/secure-tunnel@jupiter 파일:


TARGET=jupiter

TARGET_PORT=2222

LOCAL_PORT=20022

REMOTE_PORT=22


부팅시 실행되게 활성화 및 시작:

systemctl enable secure-tunnel@jupiter.service

systemctl start secure-tunnel@jupiter.service

현재 상태보기:

systemctl status secure-tunnel@jupiter.service

상태 연속으로 보기:

journalctl -f -u secure-tunnel@jupiter.service


refs:

http://system-monitoring.readthedocs.io/en/latest/ssh.html

https://gist.github.com/thomasfr/9707568 -> autossh + systemd

https://gist.github.com/drmalex07/c0f9304deea566842490 -> 최종적으로 (약간 수정해서) 내가 사용한 방법 (ssh + systemd)

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units -> systemd 관련 상세 설명


2018-03-23 추가:
가끔 network configuration을 변경하면 재접속이 안되는 경우가 발생하는데...
아래와 같은 로그가 찍히면서 재접속 시도가 계속 실패한다.

~$ sudo journalctl -f -u secure-tunnel@tmp01.service
[sudo] password for xxxxxx: 
-- Logs begin at Fri 2018-03-23 16:45:46 KST. --
Mar 23 16:55:37 teevr-lt2 systemd[1]: secure-tunnel@tmp01.service: Failed with result 'exit-code'.
Mar 23 16:55:42 teevr-lt2 systemd[1]: secure-tunnel@tmp01.service: Service hold-off time over, scheduling restart.
Mar 23 16:55:42 teevr-lt2 systemd[1]: Stopped Setup a secure tunnel to tmp01.
Mar 23 16:55:42 teevr-lt2 systemd[1]: Started Setup a secure tunnel to tmp01.
Mar 23 16:55:42 teevr-lt2 ssh[2679]: Error: remote port forwarding failed for listen port 1907
Mar 23 16:55:42 teevr-lt2 systemd[1]: secure-tunnel@tmp01.service: Main process exited, code=exited, status=255/n/a
Mar 23 16:55:42 teevr-lt2 systemd[1]: secure-tunnel@tmp01.service: Unit entered failed state.
Mar 23 16:55:42 teevr-lt2 systemd[1]: secure-tunnel@tmp01.service: Failed with result 'exit-code'.

~$ sudo netstat -plant | grep 1907
tcp        0      0 127.0.0.1:1907          0.0.0.0:*               LISTEN      9949/sshd: xxxxxx
tcp6       0      0 ::1:1907                :::*                    LISTEN      9949/sshd: xxxxxx
tcp6      42      0 ::1:1907                ::1:40842               CLOSE_WAIT  9949/sshd: xxxxxx
tcp6       0      0 ::1:1907                ::1:40838               CLOSE_WAIT  9949/sshd: xxxxxx
tcp6      42      0 ::1:1907                ::1:40848               CLOSE_WAIT  9949/sshd: xxxxxx
tcp6      42      0 ::1:1907                ::1:40844               CLOSE_WAIT  9949/sshd: xxxxxx
tcp6      42      0 ::1:1907                ::1:40840               CLOSE_WAIT  9949/sshd: xxxxxx

접속을 이미 물고 있는 상태(LISTEN)에서 계속 재접속(port bind)을 시도하는 것이었다.
문제는 설정의 ServerAliveInterval이 60초로 설정되어 있고, 재접속시도는 5초만에 하게한 것(RestartSec) 이었다. Orz.
RestartSec을 최소한 ServerAliveInterval 보다는 길게 설정해야한다.

아래 처럼 변경후에 다른 subnet으로 옮기거나해도 잘 동작했다.
ServerAliveInterval=7
RestartSec=10

라고 쓸려고 했으나...
ClientAliveCountMax(default가 3)도 고려해야하므로, ServerAliveInterval * 3 보다 큰 값으로 RestartSec을 설정하면 잘 될거같은데...
실패하는 경우가 생긴다.

그냥 접속안되면 netstat 때려보고 port 물고 있는 pid를 죽이자!

happy hackin'

Reverse SSH Tunneling

tips & tricks 2015. 9. 16. 18:41 by manywaypark

한쪽에서 접속하면서 역방향 연결을 하는 방법이 있지 않을까?

https://www.howtoforge.com/reverse-ssh-tunneling


이런 거도 있고,

http://superuser.com/questions/37738/how-to-reliably-keep-an-ssh-tunnel-open


최종적으로 원하는 형태:

https://www.async.fi/2013/07/autossh-with-ubuntu-upstart/


참고로, cygwin에서도 됨.


happy hackin'



1 
분류 전체보기 (306)
잡담 (20)
함수형 언어 (65)
emacs (16)
java (18)
tips & tricks (154)
사랑 (1)
가사 (0)
독서 (4)
mobile (6)
비함수형 언어 (2)

공지사항

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

03-29 19:33