SCP と SFTP

提供: ArchWiki
2023年1月18日 (水) 10:12時点におけるKusanaginoturugi (トーク | 投稿記録)による版 (→‎General Usage: 訳出)
ナビゲーションに移動 検索に移動

関連記事

Secure copy (SCP)Secure Shell 接続を使ってファイルを転送するプロトコルです。SSH file transfer protocol (SFTP) は同じようなプロトコルで、SCP と同様に Secure Shell をバックエンドとして使います。どちらのプロトコルでもパスワードや転送データが暗号化されるので、安全にファイルを転送できます。ただし、SFTP プロトコルには、中断した転送の再開やリモートのファイルを削除したりする機能が追加されています。

Secure file transfer protocol (SFTP)

OpenSSH をインストール・設定するだけで SFTP のセットアップも完了しています。SSH が動作しているのであれば、SSH のデフォルト設定で有効になっているので SFTP も同じく動作しています。設定が古い場合は以下の手順に従ってください。

適当なエディタで /etc/ssh/sshd_config を開いて、以下の行を (存在しない場合) 追加してください:

Subsystem sftp /usr/lib/ssh/sftp-server

sshd.service デーモンを再起動すれば SFTP が機能します。

sftp プログラムや sshfs を使ってファイルを操作できるはずです。標準的な FTP プログラムも大抵は動作します。

chroot jail による Secure file transfer protocol (SFTP)

システム管理者は、openssh を使って、ユーザのサブセットを chroot jail にし、特定のディレクトリツリーへのアクセスを制限することができます。これは、完全なシステムアクセスやシェルアクセスを許可することなく、単にいくつかのファイルを共有するのに便利です。このタイプの設定のユーザは、filezilla のような SFTP クライアントを使用して、chroot jail 内にファイルを置いたり取得したりすることができます。

ファイルシステムのセットアップ

jail ディレクトリの作成:

# mkdir -p /var/lib/jail

オプションで、このディレクトリに共有するファイルシステムをバインドマウントします。この例では、/mnt/data/share を使用します。所有者はrootで、8 進数のパーミッションは 755 です。

# mount -o bind /mnt/data/share /var/lib/jail
ヒント: bind mount が再起動しても大丈夫なように、/etc/fstab にエントリを追加することを検討してください。

非特権ユーザーの作成

共有ユーザーを作成し、適切なパスワードを設定します。

# useradd -g sshusers -d /var/lib/jail foo
# passwd foo

OpenSSHの設定

/etc/ssh/sshd_config の末尾に以下を追加して、共有を有効にし、制限を実施するようにします。

/etc/ssh/sshd_config
...
 Match group sshusers
  ChrootDirectory %h
  X11Forwarding no
  AllowTcpForwarding no
  PasswordAuthentication yes
  ForceCommand internal-sftp

sshd.service再起動して、設定ファイルを再読み込みします。chroot を使用する場合は SFTP chroot を参照して正しくキーを設定しないと、パーミッションが拒否されることがあります。

シェル経由で ssh 接続を試みて、実際に制限がかかっていることをテストしてください。ssh サーバは、設定されたことを丁寧に通知してくれるはずです。

$ ssh foo@someserver.com
foo@someserver.com's password:
This service allows sftp connections only.
Connection to someserver.com closed.

Secure copy protocol (SCP)

opensshインストール・設定して起動してください。パッケージにはファイルを転送するための scp コマンドが含まれています。詳しくは Secure Shell を参照。

rsshAURscponly などのパッケージをインストールすることでさらに機能を追加できます。下を参照。

一般的な使用方法

Linux から Linux

リモートホストからローカルホストにファイルをコピーする SCP の例:

$ scp username@from_host:file.txt /local/directory/

ローカルホストからリモートホストにファイルをコピーする SCP の例:

$ scp file.txt username@to_host:/remote/directory/

リモートホストからローカルホストにディレクトリをコピーする SCP の例:

$ scp -r username@from_host:/remote/directory/  /local/directory/

ローカルホストからリモートホストにディレクトリをコピーする SCP の例:

$ scp -r /local/directory/ username@to_host:/remote/directory/

リモートホストからリモートホストにファイルをコピーする SCP の例:

$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/

Linux から Windows

WinSCP などの Windows プログラムを使用します。

Scponly

Scponly は scp/sftp だけでしかコンピュータにアクセスできないようにする制限的なシェルです。さらに、scponly で特定のディレクトリに chroot をセットアップして更にセキュリティを高めることもできます。

scponlyインストールしてください。

既にユーザーを作成している場合、ユーザーのシェルを scponly に設定するだけです:

# usermod -s /usr/bin/scponly username

これで完了です。適当な sftp クライアントを使用してテストしてみてください。

chroot 監獄の追加

パッケージには chroot を作成するスクリプトが付属しています。使用するには:

# /usr/share/doc/scponly/setup_chroot.sh
  • 質問に答えてください。
  • /path/to/chroot の所有者を root:root にして others の権限を r-x にします。
  • 使用するユーザーのシェルを /usr/bin/scponlyc に変更します。
  • sftp-server は libnss_files などの libnss モジュールを必要とします。モジュールを chroot の /lib パスにコピーしてください。

Uploads to Chroot jail root dir

For security reasons the directory set as the chroot directory must be owned by root with only root having write access to it otherwise sftp/ssh connections will be denied. This of course means regular users cannot upload files to the root directory. In order to get around this while not compromising security you can create a folder inside the chroot directory which the regular user or group has write access to, e.g:

# cd /var/lib/jail
# mkdir uploads
# chown :sshusers uploads
# chmod 730 uploads
ノート: This will only allow users of group "sshusers" to upload to (but not list the contents of) the "uploads" directory. Use chmod 770 to allow sshusers to view contents.

Some applications utilizing SFTP do not allow input of sub-directories when performing operations (e.g. uploading files), and will attempt to upload files to the chroot base directory (which will be denied). In order to force these applications to use a specific sub-directory you can append the following to the "ForceCommand" option:

/etc/ssh/sshd_config
...
 Match group sshusers
  ...
  ForceCommand internal-sftp -d /uploads

Users on connect will then have their start directory change to the specified sub-directory (remember to restart the sshd server).