Fedora 37
Sponsored Link

OpenSSH : SSH ファイル転送 (Fedora)2022/11/18

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

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

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

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

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

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

[redhat@dlp ~]$
sftp fedora@node01.srv.world

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

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

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

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

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

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

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

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

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

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

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

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

# リモートホストのファイル削除
sftp> rm test2.txt
Removing /home/fedora/test2.txt
sftp> ls -l
drwxrwxr-x    2 fedora     fedora            6 Nov 18 21:33 public_html
-rw-rw-r--    1 fedora     fedora           10 Nov 18 21:39 redhat.txt
-rw-rw-r--    1 fedora     fedora           10 Nov 18 Nov 18 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.
関連コンテンツ