この記事を読むと・・・ |
---|
scpコマンドの基本的な使い方が理解できる |
目次
【scpコマンド】どんな時に使う?
以下のようなことがしたい場合にscp
コマンドが役立ちます。
- ファイルを他のサーバに送信したい
- ファイルを他のサーバから取得したい
【scpコマンド】基本オプション
書式
scp [オプション] 転送元パス 転送先パス
※[ ]は省略可能です
基本的なオプションは以下の表の通りです。
オプション | 説明 |
---|---|
-C | 圧縮を有効にする |
-p | コピー元のタイムスタンプやパーミッションを保持する |
-v | 詳細情報を表示する |
-i 秘密鍵ファイル | ssh接続に使用する秘密鍵ファイルを指定する |
-r | ディレクトリ内を再帰的にコピーする |
-P ポート番号 | ssh接続に使用するポート番号を指定する ※sshのポート番号を変更している場合に使用する |
-l バンド幅 | 使用するバンド幅(Kbit/s)を制限する |
【scpコマンド】オプション使用例
「-C」オプション
(圧縮を有効にする)
# scp -C /root/100M-Data testuser01@TARGETDRV:/tmp/
testuser01@targetdrv's password:
100M-Data 100% 100MB 94.2MB/s 00:01
「-p」オプション
(コピー元のタイムスタンプやパーミッションを保持する)
[root@SORCESRV ~]# ls -l /root/100M-Data
-rw-r--r-- 1 root root 104857600 10月 25 22:08 /root/100M-Data
# scp /root/100M-Data testuser01@TARGETDRV:/tmp/
testuser01@targetdrv's password:
100M-Data 100% 100MB 148.5MB/s 00:00
[root@TARGETDRV ~]# ls -l /tmp/100M-Data
-rw-r--r-- 1 testuser01 testuser01 104857600 10月 25 22:19 /tmp/100M-Data
「-v」オプション
(詳細情報を表示する)
# scp -v /root/100M-Data testuser01@TARGETDRV:/tmp/
Executing: program /usr/bin/ssh host TARGETDRV, user testuser01, command sftp
OpenSSH_8.7p1, OpenSSL 3.0.1 14 Dec 2021
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/50-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: configuration requests final Match pass
debug1: re-parsing configuration
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/50-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: Connecting to targetdrv [192.168.56.102] port 22.
debug1: Connection established.
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa_sk type -1
debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: identity file /root/.ssh/id_ed25519_sk type -1
debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /root/.ssh/id_xmss type -1
debug1: identity file /root/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.7
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.7
debug1: compat_banner: match: OpenSSH_8.7 pat OpenSSH* compat 0x04000000
debug1: Authenticating to targetdrv:22 as 'testuser01'
debug1: load_hostkeys: fopen /root/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:BbZW87sA5Z8UhtmOuHEclIZZw65T8xpeXUqBxKrLCjo
debug1: load_hostkeys: fopen /root/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'targetdrv' is known and matches the ED25519 host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 4294967296 blocks
debug1: Will attempt key: /root/.ssh/id_rsa
debug1: Will attempt key: /root/.ssh/id_dsa
debug1: Will attempt key: /root/.ssh/id_ecdsa
debug1: Will attempt key: /root/.ssh/id_ecdsa_sk
debug1: Will attempt key: /root/.ssh/id_ed25519
debug1: Will attempt key: /root/.ssh/id_ed25519_sk
debug1: Will attempt key: /root/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,webauthn-sk-ecdsa-sha2-nistp256@openssh.com>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: No credentials were supplied, or the credentials were unavailable or inaccessible
No Kerberos credentials available (default cache: KCM:)
debug1: No credentials were supplied, or the credentials were unavailable or inaccessible
No Kerberos credentials available (default cache: KCM:)
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ecdsa_sk
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Trying private key: /root/.ssh/id_ed25519_sk
debug1: Trying private key: /root/.ssh/id_xmss
debug1: Next authentication method: password
testuser01@targetdrv's password:
Authenticated to targetdrv ([192.168.56.102]:22) using "password".
debug1: pkcs11_del_provider: called, provider_id = (null)
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: filesystem full
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: client_input_hostkeys: searching /root/.ssh/known_hosts for targetdrv / (none)
debug1: client_input_hostkeys: searching /root/.ssh/known_hosts2 for targetdrv / (none)
debug1: client_input_hostkeys: hostkeys file /root/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update
debug1: Sending subsystem: sftp
100M-Data 100% 100MB 149.6MB/s 00:00
debug1: truncating at 104857600
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 105090476, received 187268 bytes, in 0.8 seconds
Bytes per second: sent 137772985.0, received 245507.2
debug1: Exit status 0
「-i 秘密鍵ファイル」オプション
(ssh接続に使用する秘密鍵ファイルを指定する)
# scp -i /home/testuser01/.ssh/id_rsa /home/testuser01/test.txt testuser01@TARGETDRV:/tmp/
Enter passphrase for key '/home/testuser01/.ssh/id_rsa':
test.txt 100% 0 0.0KB/s 00:00
「-P ポート番号」オプション
(ssh接続に使用するポート番号を指定する)
# ls -l /root/100M-Data
-rw-r--r-- 1 root root 104857600 10月 25 22:08 /root/100M-Data
# scp -P 2222 /root/100M-Data testuser01@TARGETDRV:/tmp/
testuser01@targetdrv's password:
100M-Data 100% 100MB 132.4MB/s 00:00
「-r」オプション
(ディレクトリ内を再帰的にコピーする)
[root@SORCESRV ~]# ls -lR /root/TEST
/root/TEST:
合計 0
drwxr-xr-x 3 root root 17 10月 25 22:58 aaa
/root/TEST/aaa:
合計 0
drwxr-xr-x 2 root root 22 10月 25 22:59 bbb
/root/TEST/aaa/bbb:
合計 0
-rw-r--r-- 1 root root 0 10月 25 22:59 test.txt
# scp -r /root/TEST testuser01@TARGETDRV:/tmp/
testuser01@targetdrv's password:
test.txt 100% 0 0.0KB/s 00:00
[root@TARGETDRV ~]# ls -lR /tmp/TEST
/tmp/TEST:
合計 0
drwxr-xr-x 3 testuser01 testuser01 17 10月 25 23:03 aaa
/tmp/TEST/aaa:
合計 0
drwxr-xr-x 2 testuser01 testuser01 22 10月 25 23:03 bbb
/tmp/TEST/aaa/bbb:
合計 0
-rw-r--r-- 1 testuser01 testuser01 0 10月 25 23:03 test.txt
「-l バンド幅」オプション
(使用するバンド幅(Kbit/s)を制限する)
# scp -l 10000 /root/100M-Data testuser01@TARGETDRV:/tmp/
testuser01@targetdrv's password:
100M-Data 100% 100MB 1.2MB/s 01:23