[Linux] scp 명령어로 (로컬↔서버) 파일 전송

scp는 SecureCopy의 약자로, 원격서버에 있는 파일과 폴더를 전송하거나 가져오기 위해 사용하는 명령어이다. ssh 원격 접속 프로토콜을 기반으로 하며, ssh와 동일한 22번 포트를 이용하기 때문에 password/identity file을 이용해 안전하게 송수신 할 수 있다.

local→remoteremote→localremote→remote 로의 송수신이 모두 가능하다.


사용법

> scp [options] [source] [target]

# source : 파일을 보낼 주소# target : 파일을 받을 주소

보낼 파일의 주소로 파일명만 명시했더니 permission denied가 떴다. 이후 절대경로로 변경해주니 파일이 잘 전송된 것을 확인 할 수 있었다.

1. 로컬에서 원격으로 (Local→Remote)

> scp [전송할 파일 경로] [유저명]@[IP주소]:[받을 경로]

EX) 로컬의 /home/example.txt 파일을 원격지의 /home/test 디렉토리로 전송
> scp /home/example.txt [email protected]:/home/test

2. 원격에서 로컬로 (Remote→Local)

> scp [유저명]@[IP주소]:[전송할 파일 경로] [받을 경로]

EX) 원격지의 /home/test.txt 파일을 로컬의 /home/example 디렉토리로 전송
> scp [email protected]:/home/test.txt /home/example

3. 원격에서 원격으로 (Remote→Remote)

> scp [유저명]@[IP주소]:[전송할 파일 경로] [유저명]@[IP주소]:[받을 경로]

EX) A 원격지의 /home/test.txt 파일을 B 원격지의 /home/example 디렉토리로 전송
> scp [email protected]:/home/test.txt [email protected]:/home/example

옵션 (options)

> ex) scp -i ~/.ssh/dhj-server-private ~/example.txt [email protected]:/home/test