CentOS Stream 10
Sponsored Link

OpenSSH : SSH ファイル転送 (CentOS)2024/12/19

 

SSH を利用して、暗号化通信によるセキュアなファイル転送をすることができます。

[1] SCP (Secure Copy) によるファイルコピーの例です。
# 書式 ⇒ scp [オプション] コピー元 コピー先
# ローカルホストの [test.txt] を リモートホスト [node01.srv.world] 上の [cent] ユーザーのホームディレクトリ直下へコピー

[cent@dlp ~]$
scp ./testfile.txt cent@node01.srv.world:~/

cent@node01.srv.world's password:     # 接続ユーザーのパスワード
testfile.txt                                  100%  685KB  71.9MB/s   00:00

# リモートホスト [node01.srv.world] 上の [/home/cent/test.txt] を ローカルホストのカレントディレクトリにコピー

[cent@dlp ~]$
scp cent@node01.srv.world:/home/cent/testfile.txt ./testfile2.txt

cent@node01.srv.world's password:
testfile.txt                                  100%  685KB 185.6MB/s   00:00
[2] SFTP (SSH File Transfer Protocol) によるファイル転送です。
SFTP サーバー機能は CentOS ではデフォルトで有効になっていますが、もし有効になっていない場合は [/etc/ssh/sshd_config] に [Subsystem sftp /usr/libexec/openssh/sftp-server] の行を追加して [sshd] をリロードしておきます。
# sftp [オプション] [ユーザー@ホスト名]

[cent@dlp ~]$
sftp cent@node01.srv.world

cent@node01.srv.world's password:    # ログインユーザーのパスワード
Connected to node01.srv.world.
sftp>

# リモートのカレントディレクトリ表示
sftp> pwd
Remote working directory: /home/cent

# ローカルのカレントディレクトリ表示
sftp> !pwd
/home/cent

# リモートのカレントディレクトリのファイル一覧表示
sftp> ls -l
drwxr-xr-x    ? cent     cent            6 Dec 19 11:08 testdir
-rw-r--r--    ? cent     cent       701707 Dec 19 11:05 testfile.txt

# ローカルのカレントディレクトリのファイル一覧表示
sftp> !ls -l
total 1376
-rw-r--r--. 1 cent cent 701707 Dec 19 11:06 testfile2.txt
-rw-r--r--. 1 cent cent 701707 Dec 19 11:04 testfile.txt

# ディレクトリ移動
sftp> cd testdir
sftp> pwd
Remote working directory: /home/cent/testdir

# ローカルのファイルをリモートにリネームアップロード
sftp> put testfile.txt testfile3.txt
Uploading testfile.txt to /home/cent/testdir/testfile3.txt
testfile.txt                                  100%  685KB 188.7MB/s   00:00

sftp> ls -l
-rw-r--r--    ? cent     cent       701707 Dec 19 11:10 testfile3.txt

# ローカルの複数ファイルをリモートに一括アップロード
sftp> put *.txt
Uploading testfile.txt to /home/cent/testdir/testfile.txt
testfile.txt                                  100%  685KB 294.0MB/s   00:00
Uploading testfile2.txt to /home/cent/testdir/testfile2.txt
testfile2.txt                                 100%  685KB 290.4MB/s   00:00
                                        100%    0     0.0KB/s   00:00

sftp> ls -l
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile2.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:10 testfile3.txt

# リモートのファイルをローカルにリネームダウンロード
sftp> get testfile.txt testfile4.txt
Fetching /home/cent/testdir/testfile.txt to testfile4.txt
testfile.txt                                  100%  685KB 219.8MB/s   00:00

# リモートの複数ファイルをローカルに一括ダウンロード
sftp> get *.txt
Fetching /home/cent/testdir/testfile.txt to testfile.txt
testfile.txt                                  100%  685KB 313.8MB/s   00:00
Fetching /home/cent/testdir/testfile2.txt to testfile2.txt
testfile2.txt                                 100%  685KB 388.1MB/s   00:00
Fetching /home/cent/testdir/testfile3.txt to testfile3.txt
testfile3.txt                                 100%  685KB 348.2MB/s   00:00
                                   100%   10     0.0KB/s   00:00

# リモートのカレントディレクトリにディレクトリ作成
sftp> mkdir testdir2
sftp> ls -l
drwxr-xr-x    ? cent     cent            6 Dec 19 11:14 testdir2
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile2.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:10 testfile3.txt

# リモートのカレントディレクトリ内のディレクトリ削除
sftp> rmdir testdir2
sftp> ls -l
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile2.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:10 testfile3.txt

# リモートのファイル削除
sftp> rm testfile3.txt
Removing /home/cent/testdir/testfile3.txt
sftp> ls -l
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile.txt
-rw-r--r--    ? cent     cent       701707 Dec 19 11:11 testfile2.txt

# ![command] で任意のコマンド実行
sftp> !cat /etc/passwd
root:x:0:0:Super User:/root:/bin/bash
bin:x:1:1:bin:/bin:/usr/sbin/nologin
daemon:x:2:2:daemon:/sbin:/usr/sbin/nologin
adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
.....
.....

# 終了
sftp> quit
関連コンテンツ