본문 바로가기

OPS

[윈도우] Powershell을 통한 원격접속 및 파일전송 (SSH, SCP, SFTP)

 

 

보통 관리자가 많은 리눅스 서버들을 관리할때는 Putty나 Xshell과 같은 프로그램을 통해 

서버의 IP, ssh Port 정보를 저장하고 바로 원격 접속할 수 있게 설정해두며,

WinSCP, FileZila와 같은 프로그램을 통해 GUI로 쉽게 파일전송이 가능하다.

 

Putty, Xshell, WinSCP, FileZila와 같은 프로그램이 설치되어 있지 않은 PC에서 작업을 해야 할 경우

윈도우 파워쉘을 통해서도 원격접속 및 파일전송이 가능하다는 것을 알아두자!

 

SSH (Secure Shell Protocol)

기존 Telnet, Rlogin, RSH를 대체하기 위해 만들어졌으며 네트워크 접속 도구로 원격지로 연결을 가능하게 해줌.

Telnet, Rlogin, RSH는 데이터 전송 시 평문으로 전송되기 때문에 스니핑을 통해 노출되기 쉬움.

SSH는 암호화 기법을 사용하기 때문에 노출되더라도 암호화된 문자로 노출됨.

 

 

파워쉘을 통한 ssh 접속 방법

 

ssh [접속할 username]@[서버주소] -p [ssh 포트번호]

#Powershell

#default 22번 포트 
ssh username@1.1.1.1 

#ssh 포트 설정
ssh username@1.1.1.1 -p 10022

 

윈도우 known_hosts 경로는 아래와 같다.

#Powershell

#ssh known_hosts 경로
cd $HOME/.ssh
#파일확인
ls

 

ssh 명령어 옵션

usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
           [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
           [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
           [-i identity_file] [-J [user@]host[:port]] [-L address]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] destination [command]

 

SCP (Secure Copy)

 

SCP는 SSH 프로토콜을 기반으로 파일을 전송하는 프로토콜 (SSH 포트 사용)

파일 전송만 허용하는 프로토콜.

 

파워쉘을 통한 scp 파일 전송 방법

 

로컬 → 원격 파일 전송

scp [파일경로] [접속할 username]@[서버주소]:[저장할 경로]

 

로컬 → 원격 파일 전송

scp [접속할 username]@[서버주소]:[파일경로] [저장할 경로]

 

[주의] 접속할 유저가 전송할 파일과 저장될 디렉터리에 대한 접근 권한이 없을 경우 파일 전송이 실패할 수 있음.

 

#Powershell

#윈도우 → 리눅스
scp C:\Users\user\test.txt username@1.1.1.1:/tmp/

#리눅스 → 윈도우
scp username@1.1.1.1:/tmp/test.txt C:\Users\user\

#포트 설정
scp -P 10022 username@1.1.1.1:/tmp/test.txt C:\Users\user\

#디렉터리 단위 전송
scp -r username@1.1.1.1:/tmp/test C:\Users\user\

#디렉터리 내 모든파일 전송
scp username@1.1.1.1:/tmp/* C:\Users\user\

 

scp 명령어 옵션은 아래와 같다

usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]
            [-J destination] [-l limit] [-o ssh_option] [-P port]
            [-S program] source ... target

 

 

SFTP (Secure File Transfer Protocol)

SFTP는 secure FTP로 전송 파일이 평문으로 전달되는 FTP의 문제점을 보완한 프로토콜이다.

SFTP는 SSH 방식을 이용하여 안전하게 암호화된 구간에서 FTP 기능을 사용.

전송 파일의 내용이 암호화되어 전송되며 SSH와 동일하게 22번 포트를 공유한다.

 

[주의] SFTP는 SSH 기반이 아닌 IETF에 의해 설계된 새로운 프로토콜

        SSH 포트 변경 시 SFTP 포트도 자동으로 변경됨.

        SSH와 SFTP 포트를 분리하여 사용 가능하다. (/etc/ssh/sshd_config 수정)

 

 

SFTP는 여러가지 원격 파일 관리 기능을 제공함. (파일 제거, 중단된 파일 전송 재시작과 같은 추가 기능)

 

파워쉘을 통한 sftp 파일 전송 방법

 

? 나 help 명령어로 사용 가능한 명령어 목록을 확인할 수 있다.

명령어 앞에 l이 붙으면 local에서 수행하는 명령어이고, l이 없으면 remote에서 수행하는 명령어이다.

#Powershell

#sftp prompt 접속
sftp username@1.1.1.1

#가능한 명령어 확인
sftp> ?		#or help

#현재 디렉터리 확인
sftp> lpwd	#local
sftp> pwd	#remote

#리스팅 파일
sftp> lls	#local
sftp> ls	#remote

#change directory
sftp> lcd	#local
sftp> cd	#remote

#디렉터리 생성
sftp> lmkdir test	#local
sftp> mkdir test	#remote

#파일 업로드
sftp> put test.txt	
sftp> mput *.txt	#여러 파일 업로드

#파일 다운로드
sftp> get test.txt	
sftp> mget *.txt	#여러 파일 다운로드

#삭제
sftp> rm test.txt
sftp> rmdir test

#sftp prompt 접속해제
sftp> !

 

 

관리자의 입장이라면, 솔직히 파워쉘을 통해 스크립트를 작성해 서버를 관리하는 것보다

관리프로그램을 통해 서버 정보를 저장하고 관리하는 것이 더 편할것이다.

인터넷 접속이 가능한 환경이거나, 프로그램 반입 자유롭다면 관리 프로그램들을 다운받아서 사용할 수 있다.

 

※ 원격 서버 관리 프로그램 다운로드

PuTTY : https://www.putty.org/

 

Download PuTTY - a free SSH and telnet client for Windows

Is Bitvise affiliated with PuTTY? Bitvise is not affiliated with PuTTY. We develop our SSH Server for Windows, which is compatible with PuTTY. Many PuTTY users are therefore our users as well. From time to time, they need to find the PuTTY download link. W

www.putty.org

WinSCP : https://winscp.net/eng/download.php

 

WinSCP :: Official Site :: Download

WinSCP 5.19 Download WinSCP 5.19 is a major application update. New features and enhancements include: A complete list of files that are part of a background transfer can be shown. Support for PPK version 3 keys from PuTTY 0.75. Stream interface in .NET as

winscp.net

FileZila : https://filezilla-project.org/

 

FileZilla - The free FTP solution

Overview Welcome to the homepage of FileZilla®, the free FTP solution. The FileZilla Client not only supports FTP, but also FTP over TLS (FTPS) and SFTP. It is open source software distributed free of charge under the terms of the GNU General Public Licen

filezilla-project.org