리눅스에서 파일/폴더 전송을 위한 10가지 SCP 명령어

리눅스 관리자는 명령어 환경에 익숙해야 합니다. 왜냐하면 GUI (그래픽 사용자 인터페이스) 모드가 리눅스 서버에 설치되는 경우가 일반적이지 않기 때문입니다.

SSH는 리눅스 관리자가 원격으로 안전한 방식으로 서버를 관리할 수 있게 해주는 가장 인기 있는 프로토콜일 수 있습니다. SSH 명령어와 함께 SCP 명령어가 있는데, 이는 서버 간 파일(들)을 안전한 방식으로 복사하는 데 사용됩니다.

SCP 명령의 기본 구문

다음 명령은 “source_file_name”을 “destination_folder”로 “destination_host”에서 “username” 계정을 사용하여 복사한다고 읽을 수 있습니다.

scp source_file_name username@destination_host:destination_folder

사용할 수 있는 SCP 명령에는 많은 매개변수가 있습니다. 다음은 일상적인 사용에서 사용할 수 있는 매개변수입니다.

리눅스에서 파일을 안전하게 전송

기본 SCP 명령어는 매개변수 없이 백그라운드에서 파일을 복사합니다. 프로세스가 완료되거나 오류가 발생할 때까지 사용자는 아무것도 보지 못합니다.

디버그 정보를 화면에 인쇄하려면 ” -v ” 매개변수를 사용할 수 있습니다. 연결, 인증 및 구성 문제를 디버깅하는 데 도움이 될 수 있습니다.

로컬 호스트에서 원격 서버로 파일 복사

다음 명령어는 로컬에서 원격 Linux 시스템의 /home/tecmint 디렉토리로 “scp-cheatsheet.pdf” 파일을 복사합니다.

$ scp -v scp-cheatsheet.pdf [email protected]:/home/tecmint/.

샘플 출력:

SCP – Copy File to Remote Linux Server
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -t /home/tecmint/.
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_xmss type -1
...

원격 호스트에서 로컬 호스트로 파일 복사

다음 명령어는 원격 호스트에서 로컬 시스템의 /home/tecmint 디렉토리로 “ssh-cheatsheet.pdf” 파일을 복사합니다.

$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.

샘플 출력:

SCP – Copy File to Local System
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -f /home/ravi/ssh-cheatsheet.pdf
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
...

원격 호스트에서 다른 호스트로 파일 복사

다음 명령어는 원격 호스트에서 다른 원격 호스트 시스템의 /home/tecmint 디렉토리로 “ssh-cheatsheet.pdf” 파일을 복사합니다.

$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf [email protected]:/home/anusha/.

원래 생성 날짜 및 시간으로 파일 복사

-p” 매개변수는 파일 복사 시 원래 수정 및 액세스 시간을 보존하며 예상 시간과 연결 속도가 화면에 표시됩니다.

$ scp -p scp-cheatsheet.pdf [email protected]:/home/tecmint/.

샘플 출력:

SCP – Preserve File Timestamps
[email protected]'s password: 
scp-cheatsheet.pdf                                                                                                                                                                 100%  531   721.4KB/s   00:00    

파일 복사 중 SCP 압축

파일 전송을 더 빠르게 만들어주는 매개변수 중 하나는 “-C” 매개변수로, 이는 파일을 이동 중에 압축하는 데 사용됩니다. 독특한 점은 압축이 네트워크에서만 발생한다는 것입니다. 파일이 대상 서버에 도착하면 압축이 이루어지기 전의 원래 크기로 돌아갑니다.

다음 명령어를 살펴보세요. 93 Mb의 단일 파일을 사용하고 있습니다.

$ scp -pv messages.log [email protected]:.

샘플 출력:

SCP Transfers File Without Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t.
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t.
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

-C” 매개변수를 사용하지 않고 파일을 복사하면 1661.3초가 소요됩니다. 아래 명령에서 “-C" 매개변수를 사용한 결과와 비교해보세요.

$ scp -Cpv messages.log [email protected]:.

샘플 출력:

SCP – Transfers File with Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t.
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

압축을 사용할 때 보시다시피 전송 과정은 162.5초 만에 완료됩니다. “-C” 매개변수를 사용하지 않는 것보다 10배 빠릅니다. 네트워크에서 많은 파일을 복사하는 경우 “-C” 매개변수는 총 시간을 줄여드릴 수 있습니다.

우리가 주의해야 할 점은 압축 방법이 모든 파일에 적용되지 않는다는 것입니다. 원본 파일이 이미 압축되어 있으면 거기에 개선이 없습니다. .zip, .rar, 사진, .iso 파일과 같은 파일은 “-C” 매개변수에 의해 영향을 받지 않습니다.

SCP 암호를 변경하여 파일 암호화

기본적으로 SCP는 파일을 암호화하기 위해 “AES-128“를 사용합니다. 다른 암호를 사용하여 암호화하려면 ” -c ” 매개변수를 사용할 수 있습니다.

이 명령을 살펴보십시오.

$ scp -c 3des Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

위의 명령은 SCP에게 파일을 암호화하기 위해 3des 알고리즘을 사용하도록 지시합니다. 이 매개변수는 ” -c “를 사용하지 ” -C “를 사용하지 않는다는 점에 주의하세요.

SCP 명령으로 대역폭 사용 제한

유용할 수 있는 또 다른 매개변수는 ” -l ” 매개변수입니다. “-l” 매개변수는 사용할 대역폭을 제한합니다. 많은 파일을 복사하는 자동화 스크립트를 수행하지만 SCP 프로세스로 인해 대역폭이 고갈되지 않길 원하는 경우에 유용합니다.

$ scp -l 400 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

-l ” 매개변수 뒤의 400 값은 SCP 프로세스에 대해 대역폭을 최대 50KB/sec로 제한한다는 것을 의미합니다.

기억해야 할 한 가지는 대역폭이 킬로비트/sec(kbps)로 지정된다는 것입니다. 즉, 8비트1바이트와 같습니다.

동안 SCP킬로바이트/ (KB/s)로 계산합니다. 따라서 대역폭을 SCP 최대치인 50 KB/s로 제한하려면 50 x 8 = 400으로 설정해야 합니다.

다른 포트를 사용하는 SCP

일반적으로 SCP는 기본 포트로 22를 사용하지만 보안상의 이유로 포트를 다른 포트로 변경할 수 있습니다. 예를 들어, 우리는 포트 2249를 사용하고 있습니다.

그런 다음 명령어는 다음과 같아야 합니다.

$ scp -P 2249 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

대문자 “P”를 사용하는지 확인하십시오. “p”는 이미 보존된 시간과 모드에 사용되고 있습니다.

SCP – 재귀적으로 파일 및 디렉토리 복사

때로는 디렉토리와 그 안에 있는 모든 파일/디렉토리를 복사해야 할 때가 있습니다. “r” 매개변수를 사용하여 전체 디렉토리를 재귀적으로 복사하는 단일 명령으로 수행할 수 있다면 더 좋을 것입니다.

$ scp -r documents [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

복사 프로세스가 완료되면 대상 서버에서 “documents”라는 디렉토리와 그 안에 있는 모든 파일을 찾을 수 있습니다. “documents” 폴더는 자동으로 생성됩니다.

SCP – 진행 메시지 비활성화

SCP의 진행 미터와 경고/진단 메시지를 보지 않으려면 “-q” 매개변수를 사용하여 비활성화할 수 있습니다. 다음은 예시입니다.

$ scp -q Label.pdf [email protected]:.

[email protected]'s password:
pungki@mint ~/Documents $

암호를 입력한 후 SCP 프로세스에 대한 정보가 표시되지 않는 것을 볼 수 있습니다. 프로세스가 완료되면 다시 프롬프트가 표시됩니다.

SCP – 프록시를 사용하여 파일 복사

프록시 서버는 일반적으로 사무실 환경에서 사용됩니다. 기본적으로 SCP는 프록시가 구성되어 있지 않습니다. 프록시를 사용하는 환경에서는 SCP에게 프록시와 통신하도록 “알려주어야” 합니다.

다음은 시나리오입니다. 프록시 주소는 10.0.96.6이고 프록시 포트는 8080입니다. 프록시는 사용자 인증도 구현되어 있습니다. 먼저 “~/.ssh/config” 파일을 생성해야 합니다. 둘째, 이 명령어를 그 안에 넣습니다.

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

그런 다음 “~/.ssh/proxyauth” 파일을 생성하여 다음을 포함시킵니다.

myusername:mypassword

그 후 일반적으로 SCP를 수행할 수 있습니다.

주목할 점은 corkscrew가 시스템에 아직 설치되지 않았을 수도 있다는 것입니다. 저의 Linux Mint에서는 먼저 표준 Linux Mint 설치 절차를 사용하여 설치해야 합니다.

$ apt-get install corkscrew

다른 yum 기반 시스템의 경우, 사용자는 다음 yum 명령어를 사용하여 corkscrew를 설치할 수 있습니다.

# yum install corkscrew

또 다른 점은 “~/.ssh/proxyauth” 파일에 명시적인 형태로 “사용자 이름”과 “비밀번호”가 포함되어 있다는 것입니다. 이 파일이 오직 사용자만이 접근할 수 있도록 해야 합니다.

다른 ssh_config 파일 선택

회사 네트워크와 공용 네트워크 사이를 자주 전환하는 모바일 사용자의 경우, SCP에서 설정을 계속 변경하는 것은 고통스러울 것입니다. 우리의 요구에 맞는 다른 ssh_config 파일을 사용하는 것이 더 좋을 것입니다.

회사 네트워크에서는 프록시가 사용되지만 공용 네트워크에서는 사용되지 않으며, 네트워크를 정기적으로 전환합니다.

$ scp -F /home/pungki/proxy_ssh_config Label.pdf

[email protected]:.
[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

기본적으로 “ssh_config” 파일은 “~/.ssh/config“에 사용자별로 배치됩니다. 프록시 호환성을 갖춘 특정 “ssh_config” 파일을 생성하면 네트워크 간 전환이 더 쉬워집니다.

회사 네트워크에 있을 때는 “-F” 매개변수를 사용할 수 있습니다. 공용 네트워크에 있을 때는 “-F” 매개변수를 생략할 수 있습니다.

이것으로 SCP에 대한 설명을 마치겠습니다. 자세한 내용은 SCPman 페이지를 참조하십시오. 의견과 제안은 언제든지 환영합니다.

Source:
https://www.tecmint.com/scp-commands-examples/