CentOS 6
Sponsored Link

SSH ファイル転送 (CentOSクライアント)2014/08/02

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

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

cent@www.srv.world's password:    
# 接続ユーザーのパスワード

test.txt                                                100%   10     0.0KB/s   00:00

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

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

cent@prox.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@www.srv.world

cent@www.srv.world's password:    
# ログインユーザーのパスワード

Connected to prox.srv.world.
sftp>
# リモートのカレントディレクトリ表示

sftp>
pwd

Remote working directory: /home/cent
# ローカルのカレントディレクトリ表示

sftp>
!pwd

/home/redhat
# リモートのカレントディレクトリのファイル一覧表示

sftp>
ls -l

drwxrwxr-x    2 cent     cent            6 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 28 22:53 test.txt
# ローカルのカレントディレクトリのファイル一覧表示

sftp>
!ls -l

total 4
-rw-rw-r-- 1 redhat redhat 10 Jul 29 21:31 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 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jul 28 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 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 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/redhat.txt to redhat.txt
/home/cent/redhat.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 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 21:46 test2.txt
drwxrwxr-x    2 cent     cent            6 Jul 29 21:53 testdir
# リモートのカレントディレクトリ内のディレクトリ削除

sftp>
rmdir testdir

rmdir ok, `testdir' removed
sftp>
ls -l

drwxrwxr-x    2 cent     cent            6 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 21:45 test.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 21:46 test2.txt
# リモートのファイル削除

sftp>
rm test2.txt

Removing /home/cent/test2.txt
sftp>
ls -l

drwxrwxr-x    2 cent     cent            6 Jul 29 21:33 public_html
-rw-rw-r--    1 cent     cent           10 Jul 29 21:39 redhat.txt
-rw-rw-r--    1 cent     cent           10 Jul 29 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.
関連コンテンツ