Prerequisite
SFTP + 터널링을 통한 File 다운로드 방법에 대한 내용을 기술하는 문서입니다.
1. Client (Mac OS or Windows) 에 filezilla 를 미리 설치 해줍니다.
FileZilla Install : https://filezilla-project.org/download.php?type=client
2. 베스천 인스턴스, 프라이빗 인스턴스가 프로비저닝 되어 있어야 하며 SFTP Port 22번을 열어줘야 합니다.
Process
1. SSH Tunneling 설정
-N 옵션은 SSH 세션을 열지 않고 포워딩만 수행하게 하고, -v 옵션은 상세한 정보를 출력하게 합니다.
ssh -i <.pem> -L localhost:<Local_Port>:<Private_Instance_IP> <user_name>@<Bastion_Instance_IP> -N -v
예)
ssh -i ejl_test_key.pem -L localhost:2222:10.0.225.26:22 ec2-user@3.34.124.84 -N -v
위 명령어를 해석하면, localhost의 2222 포트는 3.34.124.84 호스트의 22번 포트를 통해 10.0.225.26 호스트의 22번 포트로 포워딩하라는 뜻입니다.
또한, 다중의 Private 서버를 적용하고 싶으시면 아래와 같이 터널링을 설정해주면 됩니다. 여기서 주의할 점은 로컬의 Port 를 구분해줘야 합니다.
ssh -i <.pem> -L localhost:<Local_Port1>:<Private_Instance_IP> -L localhost:<Local_Port2>:<Private_Instance_IP2> <user_name>@<Bastion_Instance_IP> -N -v
예)
ssh -i ejl_test_key.pem -L localhost:2222:10.0.225.26:22 -L localhost:2223:10.0.225.28:22 ec2-user@3.34.124.84 -N -v
명령어를 입력하게 되면 아래와 같은 결과가 출력됩니다.
결과
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/leeeuijoo/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to 3.34.123.80 [3.34.123.80] port 22.
debug1: Connection established.
.
.
.
debug1: Local connections to localhost:2222 forwarded to remote address 10.0.225.26:22
debug1: Local forwarding listening on ::1 port 2222.
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 2222.
debug1: channel 1: new [port listener]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update
debug1: Remote: /home/ec2-user/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
터널링이 완료 됐는지 확인 하기 위해 2222번 포트를 사용하는지 체크해줍니다.
lsof -i :2222
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 8572 leeeuijoo 5u IPv6 0x6f619de699f3d43f 0t0 TCP localhost:rockwell-csp2 (LISTEN)
ssh 8572 leeeuijoo 6u IPv4 0x6f619de69c321957 0t0 TCP localhost:rockwell-csp2 (LISTEN)
2. FileZilla 로 연결 설정
FileZilla 에 접속하여 새 연결(새 사이트)를 생성합니다.
프로토콜 : SFTP
호스트 : localhost
포트 : 2222 (포워딩 포트)
로그온 유형 : 키 파일 (.pem)
사용자 : ec2-user
키 파일 : 사용하는 키파일 .pem 선택
호스트 신뢰해줍니다.
3. TEST
1. 파일 조회
테스트를 위해 간단하게 Private 인스턴스에 접속하여 /home/ec2-user/ 경로에 testdir 이름의 디렉토리와 testfile.txt 라는 파일을 만들어 보겠습니다.
[프라이빗 인스턴스]
mkdir testdir && touch testdir/testfile
Reference
SSH 터널링 문서 : https://smilesharkhelp.zendesk.com/hc/ko/articles/6222653124623--SSH-SSH-Tunneling-Linux-Windows
댓글
댓글 0개
댓글을 남기려면 로그인하세요.