C.W.K.
Stream
Lesson 02 of 08 · published

SSH 터널 — -L, -R, -D

~13 min · ssh, tunnel, port-forward

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

TCP 한 파이프, 많은 트릭

SSH 가 임의의 TCP 포트를 자기 암호화 connection 으로 포워딩 가능. flag 셋이 거의 다 처리:

  • -L localport:remoteHost:remotePort — local forward.
  • -R remoteport:localHost:localPort — remote forward.
  • -D socksport — SOCKS proxy.

-L: 노트북에서 원격 서비스 접근

ssh -L 5432:localhost:5432 dbserver

해석: '내 노트북 5432 포트 열기. 거기 접속하면 ssh 통해 dbserver 까지 터널링되고, dbserver 가 자기 localhost:5432 로 연결.' 이제 psql -h localhost 가 인터넷에 노출 안 된 원격 Postgres 를 직접.

-R: 노트북을 서버에 노출

ssh -R 8080:localhost:3000 publicbox

'publicbox 의 8080 열기. 거기 들어오는 connection 은 내 노트북 3000 으로.' 팀원에게 공인 호스트 통해 로컬 dev 서버 공유할 때.

-D: 서버 통해 SOCKS proxy

ssh -D 1080 jumpbox

브라우저의 SOCKS proxy 를 127.0.0.1:1080 로 설정하면 모든 브라우징이 jumpbox 통해 나가. 일회용 VPN.

config 에 영구 터널

Host dbserver
  HostName db.internal
  LocalForward 5432 localhost:5432
  LocalForward 6379 localhost:6379

그러면 ssh dbserver 만으로 터널 자동 오픈. -N (command 없음) + -f (백그라운드) 와 짝지으면 fire-and-forget.

autossh 로 안정성

brew install autossh. autossh 가 끊기는 네트워크 에서 자동 재접속. 장시간 터널에 완벽.

Code

내부망 Postgres 접근·bash
ssh -fNL 5432:localhost:5432 dbserver
psql -h localhost -p 5432 -U me production
# Kill the tunnel later
pgrep -f 'ssh -fNL 5432' | xargs kill

External links

Exercise

원격 박스에 내부 서비스 (DB / Redis / web) 있으면 터널 오픈: ssh -fNL <localport>:localhost:<remoteport> host. 노트북에서 접속. 같은 내용을 ssh_config 의 LocalForward 로 박고 재테스트.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.