웹사이트 검색

SCP Linux 명령 – Linux에서 안전하게 파일 전송


Linux 관리자는 명령줄 환경에 익숙해야 합니다. Linux 서버에서는 GUI(Graphical User Interface) 모드가 일반적으로 설치되지 않기 때문에.

SSH는 Linux 관리자가 원격 보안 방식으로 서버를 관리할 수 있도록 하는 가장 널리 사용되는 프로토콜일 수 있습니다. SSH 명령에는 안전한 방법으로 서버 간에 파일을 복사하는 데 사용되는 SCP 명령이 내장되어 있습니다.

SCP 명령의 기본 구문

아래 명령은 "사용자 이름을 사용하여 "destination_host"의 "destination_folder"에 "source_file_name"을 복사하여 읽습니다. " 계정.

scp source_file_name username@destination_host:destination_folder

SCP 명령에는 사용할 수 있는 매개변수가 많이 있습니다. 일일 사용량에 사용할 수 있는 매개변수는 다음과 같습니다.

Linux에서 안전하게 파일 전송

매개변수가 없는 기본 SCP 명령은 백그라운드에서 파일을 복사합니다. 프로세스가 완료되거나 일부 오류가 나타나지 않는 한 사용자에게는 아무 것도 표시되지 않습니다.

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

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

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

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

샘플 출력:

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 :/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.

샘플 출력:

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 :/home/ravi/ssh-cheatsheet.pdf [email :/home/anusha/.

원본 생성 날짜 및 시간이 포함된 파일 복사

"-p" 매개변수는 예상 시간과 함께 파일을 복사하는 동안 파일의 원래 수정 및 액세스 시간을 보존하며 연결 속도가 화면에 나타납니다.

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

샘플 출력:

[email 's password: 
scp-cheatsheet.pdf                                                                                                                                                                 100%  531   721.4KB/s   00:00    

파일을 복사하는 동안 SCP 압축

파일 전송 속도를 높일 수 있는 매개변수 중 하나는 이동 중에 파일을 압축하는 데 사용되는 "-C" 매개변수입니다. 독특한 점은 압축이 네트워크에서만 발생한다는 것입니다. 파일이 대상 서버에 도착하면 압축이 발생하기 전의 원래 크기로 돌아갑니다.

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

scp -pv messages.log [email :.

샘플 출력:

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 '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 :.

샘플 출력:

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 '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 :.

[email '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 :.

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

'-l' 매개변수 뒤에 있는 400 값은 SCP 프로세스의 대역폭을 50KB/로만 제한한다는 의미입니다. 초.

한 가지 기억해야 할 점은 대역폭이 킬로비트/(kbps) 단위로 지정된다는 것입니다. 이는 8비트1바이트와 동일하다는 의미입니다.

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

다른 포트를 가진 SCP

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

그러면 명령은 다음과 같아야 합니다.

scp -P 2249 Label.pdf [email :.

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

"p"가 이미 보존된 시간과 모드에 사용되었으므로 "p"가 아닌 대문자 "P"를 사용해야 합니다.

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

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

scp -r documents [email :.

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

복사 프로세스가 완료되면 대상 서버에서 모든 파일이 포함된 "documents"라는 디렉터리를 찾을 수 있습니다. '문서' 폴더가 자동으로 생성됩니다.

SCP – 진행 메시지 비활성화

SCP의 진행률 표시기와 경고/진단 메시지를 표시하지 않기로 선택한 경우 "-q" 매개변수를 사용하여 비활성화할 수 있습니다. 여기에 예가 있습니다.

scp -q Label.pdf [email :.

[email '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를 수행할 수 있습니다.

코크스크루가 시스템에 아직 설치되지 않았을 수 있습니다. 내 Linux Mint에서는 표준 Linux Mint 설치 절차를 사용하여 먼저 설치해야 합니다.

apt-get install corkscrew

다른 yum 기반 시스템의 경우 사용자는 다음 yum 명령을 사용하여 코르크 나사를 설치할 수 있습니다.

yum install corkscrew

또 다른 점은 "~/.ssh/proxyauth" 파일에 "사용자 이름"과 "비밀번호"가 일반 텍스트 형식으로 포함되어 있다는 것입니다. , 파일에 본인만 액세스할 수 있는지 확인하세요.

다른 ssh_config 파일 선택

회사 네트워크와 공용 네트워크를 자주 전환하는 모바일 사용자의 경우 SCP에서 항상 설정을 변경하는 데 어려움을 겪을 것입니다. 필요에 맞게 다른 ssh_config 파일을 넣을 수 있다면 더 좋습니다.

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

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

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

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

회사 네트워크에 있는 경우 "-F" 매개변수를 사용할 수 있습니다. 공용 네트워크에 있는 경우 "-F" 매개변수를 건너뛸 수 있습니다.

SCP에 관한 모든 것입니다. 자세한 내용은 SCPman 페이지를 참조하세요. 자유롭게 의견과 제안을 남겨주세요.