FTP( File Transfer Protocol )




FTP ( File Transfer Protocol ) はサーバとクライアントの間で
ファイル転送を行うためのプロトコルです。

具体的には、サーバに置いてあるファイルをクライアントにダウンロードしたり、
あるいは、クライアントにあるファイルをサーバへアップロードしたりする時に
使います。

FTP でファイルのやり取りを行うためには、
サーバ側に FTP のデーモン ( サービス ) が起動している必要があります。

では、FTP を具体的に試してみます。

FTP でファイル転送を行うためには、クライアント側で ftp コマンドを実行します。

FTP サーバ : 1.1.1.100
クライアント : 1.1.1.1

まずは、FTP サーバに FTP で接続する。

# ftp 1.1.1.100
Connected to 1.1.1.100.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (1.1.1.100:root): test1
331 Please specify the password.
Password: testtest
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

FTP で接続するためのアカウントとパスワードが聞かれます。

どのアカウントで接続できるかは FTP サーバの設定次第。

通常、デフォルトでは root アカウントは FTP 接続できません。

匿名アクセスが許可されていれば anonymous アカウントが使えます。
anonymous アカウントで接続する際はパスワードは任意の文字列で構いません。

分からなければ root 以外のシステムに存在するユーザで FTP 接続を試してみましょう。

FTP で接続 ( ログイン ) できれば、次は FTP によるサーバとのやり取り。

help コマンドを使用して、
FTP クライアントと FTP サーバの間でどのようなコマンドが使えるかを確認してみます。
これはクライアントが CentOS 5 の場合の出力。

ftp> help
Commands may be abbreviated.  Commands are:

!               cr              mdir            proxy           send
$               delete          mget            sendport        site
account         debug           mkdir           put             size
append          dir             mls             pwd             status
ascii           disconnect      mode            quit            struct
bell            form            modtime         quote           system
binary          get             mput            recv            sunique
bye             glob            newer           reget           tenex
case            hash            nmap            rstatus         trace
ccc             help            nlist           rhelp           type
cd              idle            ntrans          rename          user
cdup            image           open            reset           umask
chmod           lcd             passive         restart         verbose
clear           ls              private         rmdir           ?
close           macdef          prompt          runique
cprotect        mdelete         protect         safe
ftp>
ftp> 

cd や ls などのコマンドが使えることが分かります。
これらのコマンドを使って、目的のファイルやディレクトリを探して、
ファイルをダウンロード・アップロードします。

では、適当なファイルを FTP サーバからダウンロードしてみます。

ftp> cd /tmp
250 Directory successfully changed.
ftp> 
ftp> ls
227 Entering Passive Mode (1.1.1.100,22,220)
150 Here comes the directory listing.
-rw-rw-r--    1 500      500           350 Jan 24 02:47 test.txt
drwx------    2 500      500          4096 Jan 24 02:47 test1
drwx------    2 500      500          4096 Jan 24 02:47 test2
226 Directory send OK.
ftp> 
ftp> lcd /var/tmp
Local directory now /var/tmp
ftp> 
ftp> mget test.txt
mget test.txt? y
227 Entering Passive Mode (1.1.1.100,183,189)
150 Opening BINARY mode data connection for test.txt (350 bytes).
226 File send OK.
350 bytes received in 0.0043 seconds (79 Kbytes/s)
ftp> 
ftp> quit
221 Goodbye.
# 

上記で実施した流れは以下の通り。

まずは、cd コマンドで FTP サーバの /tmp ディレクトリへ移動しています。

次に、ls コマンドで FTP サーバの /tmp ディレクトリに置いてあるファイルを確認。

続いて、lcd コマンドでクライアント側のファイル保存先となるローカルディレクトリを
/var/tmp へ移動しています。

そして、mget コマンドで /tmp/test.txt ファイルをダウンロードしています。
保存先は、/var/tmp です。

最後に、quit コマンドで FTP サーバとの接続を切断しています。
これで FTP によるサーバとのやり取りは終了です。

クライアント側の /var/tmp を確認してみると、
test.txt ファイルが保存されています。


FTP( File Transfer Protocol )のモードの違い