CentOS Stream 9
Sponsored Link

OpenSSH : SSH ファイル転送 (CentOS)2022/01/07

 
SSH を利用して、暗号化通信によるセキュアなファイル転送をすることができます。
[1] SCP (Secure Copy) によるファイルコピーの例です。
# 書式 ⇒ scp [オプション] コピー元 コピー先
# ローカルホストの [test.txt] を リモートホスト [node01.srv.world] 上の [cent] ユーザーのホームディレクトリ直下へコピー

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

cent@node01.srv.world's password:     # 接続ユーザーのパスワード
test.txt                                      100%   10     0.0KB/s   00:00

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

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

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

[redhat@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/redhat 

# リモートのカレントディレクトリのファイル一覧表示
sftp> ls -l
drwxrwxr-x    2 cent     cent            7 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 22:53 test.txt

# ローカルのカレントディレクトリのファイル一覧表示
sftp> !ls -l
total 4
-rw-rw-r--    1 redhat     redhat       10 Jan 04 21:53 test.txt

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

# ローカルのファイルをリモートにリネームアップロード
sftp> put test.txt redhat.txt
Uploading test.txt to /home/cent/redhat.txt
test.txt                           100%   10     0.0KB/s   00:00
sftp> ls -l
drwxrwxr-x    2 cent     cent            6 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 22:53 test.txt

# ローカルの複数ファイルをリモートに一括アップロード
sftp> put *.txt
Uploading test.txt to /home/cent/test.txt
test.txt                                           100%   10     0.0KB/s   00:00
Uploading test2.txt to /home/cent/test2.txt
test2.txt                                          100%    0     0.0KB/s   00:00
sftp> ls -l
drwxrwxr-x    2 cent     cent            6 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:46 test2.txt

# リモートのファイルをローカルにダウンロード
sftp> get test.txt
Fetching /home/cent/test.txt to test.txt
/home/cent/test.txt                   100%   10     0.0KB/s   00:00

# リモートの複数ファイルをローカルに一括ダウンロード
sftp> get *.txt
Fetching /home/cent/cent.txt to cent.txt
/home/cent/cent.txt                                       100%   10     0.0KB/s   00:00
Fetching /home/cent/test.txt to test.txt
/home/cent/test.txt                                         100%   10     0.0KB/s   00:00
Fetching /home/cent/test2.txt to test2.txt
/home/cent/test2.txt                                        100%   10     0.0KB/s   00:00

# リモートのカレントディレクトリにディレクトリ作成
sftp> mkdir testdir
sftp> ls -l
drwxrwxr-x    2 cent     cent            6 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:46 test2.txt
drwxrwxr-x    2 cent     cent            6 Jan 04 21:53 testdir

# リモートのカレントディレクトリ内のディレクトリ削除
sftp> rmdir testdir
rmdir ok, `testdir' removed
sftp> ls -l
drwxrwxr-x    2 cent     cent            6 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:46 test2.txt

# リモートのファイル削除
sftp> rm test2.txt
Removing /home/cent/test2.txt
sftp> ls -l
drwxrwxr-x    2 cent     cent            6 Jan 04 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jan 04 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jan 04 21:45 test.txt

# ![command] で任意のコマンド実行
sftp> !cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
.....
.....
redhat:x:1001:1001::/home/redhat:/bin/bash

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