「SCP と SFTP」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(→‎chroot 監獄の追加: add ==== Uploads to Chroot jail root dir ====)
 
(同じ利用者による、間の10版が非表示)
22行目: 22行目:
 
''sftp'' プログラムや [[sshfs]] を使ってファイルを操作できるはずです。標準的な FTP プログラムも大抵は動作します。
 
''sftp'' プログラムや [[sshfs]] を使ってファイルを操作できるはずです。標準的な FTP プログラムも大抵は動作します。
   
== Secure file transfer protocol (SFTP) with a chroot jail ==
+
== chroot 監獄 による Secure file transfer protocol (SFTP) ==
   
  +
システム管理者は、{{Pkg|openssh}} を使って、ユーザのサブセットを chroot 監獄にし、特定のディレクトリツリーへのアクセスを制限することができます。これは、完全なシステムアクセスやシェルアクセスを許可することなく、単にいくつかのファイルを共有するのに便利です。このタイプの設定のユーザは、{{Pkg|filezilla}} のような SFTP クライアントを使用して、chroot 監獄内にファイルを置いたり取得したりすることができます。
Sysadmins can jail a subset of users to a chroot jail using {{Pkg|openssh}} thus restricting their access to a particular directory tree. This can be useful to simply share some files without granting full system access or shell access. Users with this type of setup may use SFTP clients such as {{Pkg|filezilla}} to put/get files in the chroot jail.
 
   
  +
=== ファイルシステムのセットアップ ===
=== Setup the filesystem ===
 
   
  +
監獄ディレクトリの作成:
Create a jail directory:
 
   
 
# mkdir -p /var/lib/jail
 
# mkdir -p /var/lib/jail
   
  +
オプションで、このディレクトリに共有するファイルシステムをバインドマウントします。この例では、{{ic|/mnt/data/share}} を使用します。所有者はrootで、8 進数のパーミッションは 755 です。
Optionally, bind mount the filesystem to be shared to this directory. In this example, {{ic|/mnt/data/share}} is to be used. It is owned by root and has octal permissions of 755.
 
   
 
# mount -o bind /mnt/data/share /var/lib/jail
 
# mount -o bind /mnt/data/share /var/lib/jail
   
  +
{{Tip|バインドマウントが再起動しても維持されるように、{{ic|/etc/fstab}} にエントリを追加することを検討してください。}}
{{Tip|Consider adding an entry to {{ic|/etc/fstab}} to make the bind mount survive a reboot.}}
 
   
=== Create an unprivileged user ===
+
=== 非特権ユーザーの作成 ===
   
  +
共有ユーザーを作成し、適切なパスワードを設定します。
Create the share user and setup a good password:
 
   
 
# useradd -g sshusers -d /var/lib/jail foo
 
# useradd -g sshusers -d /var/lib/jail foo
 
# passwd foo
 
# passwd foo
   
=== Setup OpenSSH ===
+
=== OpenSSHの設定 ===
   
  +
{{ic|/etc/ssh/sshd_config}} の末尾に以下を追加して、共有を有効にし、制限を実施するようにします。
Add the following to the end of {{ic|/etc/ssh/sshd_config}} to enable the share and to enforce the restrictions:
 
   
 
{{hc|/etc/ssh/sshd_config|
 
{{hc|/etc/ssh/sshd_config|
59行目: 59行目:
 
}}
 
}}
   
  +
{{ic|sshd.service}} を [[再起動]]して、設定ファイルを再読み込みします。chroot を使用する場合は [[SFTP chroot]] を参照して正しくキーを設定しないと、パーミッションが拒否されることがあります。
[[Restart]] {{ic|sshd.service}} to re-read the configuration file. See [[SFTP chroot]] to configure the keys correctly when using chroot or it will get permission denied.
 
   
  +
シェル経由で ssh 接続を試みて、実際に制限がかかっていることをテストしてください。ssh サーバは、設定されたことを丁寧に通知してくれるはずです。
Test that in fact, the restrictions are enforced by attempting an ssh connection via the shell. The ssh server should return a polite notice of the setup:
 
   
 
{{hc|$ ssh foo@someserver.com|
 
{{hc|$ ssh foo@someserver.com|
75行目: 75行目:
 
{{Aur|rssh}} や {{Pkg|scponly}} などのパッケージをインストールすることでさらに機能を追加できます。下を参照。
 
{{Aur|rssh}} や {{Pkg|scponly}} などのパッケージをインストールすることでさらに機能を追加できます。下を参照。
   
=== General Usage ===
+
=== 一般的な使用方法 ===
   
==== Linux to Linux ====
+
==== Linux から Linux ====
   
  +
リモートホストからローカルホストにファイルをコピーする SCP の例:
Copy file from a remote host to local host SCP example:
 
   
 
$ scp username@from_host:file.txt /local/directory/
 
$ scp username@from_host:file.txt /local/directory/
   
  +
ローカルホストからリモートホストにファイルをコピーする SCP の例:
Copy file from local host to a remote host SCP example:
 
   
 
$ scp file.txt username@to_host:/remote/directory/
 
$ scp file.txt username@to_host:/remote/directory/
   
  +
リモートホストからローカルホストにディレクトリをコピーする SCP の例:
Copy directory from a remote host to local host SCP example:
 
   
 
$ scp -r username@from_host:/remote/directory/ /local/directory/
 
$ scp -r username@from_host:/remote/directory/ /local/directory/
   
  +
ローカルホストからリモートホストにディレクトリをコピーする SCP の例:
Copy directory from local host to a remote host SCP example:
 
   
 
$ scp -r /local/directory/ username@to_host:/remote/directory/
 
$ scp -r /local/directory/ username@to_host:/remote/directory/
   
  +
リモートホストからリモートホストにファイルをコピーする SCP の例:
Copy file from remote host to remote host SCP example:
 
   
 
$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
 
$ scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
   
==== Linux to Windows ====
+
==== Linux から Windows ====
   
Use a Windows program such as [https://winscp.net/eng/download.php WinSCP]
+
[https://winscp.net/eng/download.php WinSCP] などの Windows プログラムを使用します。
   
 
=== Scponly ===
 
=== Scponly ===
125行目: 125行目:
 
* sftp-server は libnss_files などの libnss モジュールを必要とします。モジュールを chroot の {{ic|/lib}} パスにコピーしてください。
 
* sftp-server は libnss_files などの libnss モジュールを必要とします。モジュールを chroot の {{ic|/lib}} パスにコピーしてください。
   
  +
==== Chroot 監獄のルートディレクトリにアップロードする ====
==== Uploads to Chroot jail root dir ====
 
   
  +
セキュリティ上の理由から、chroot ディレクトリとして設定されたディレクトリは、rootが所有し、rootのみが書き込み権限を持つ必要があり、root でなければ sftp/ssh 接続が拒否されます。もちろん、これは一般ユーザがルートディレクトリにファイルをアップロードできないことを意味します。この問題を回避し、セキュリティを損なわないようにするには、chroot ディレクトリ内に、通常のユーザまたはグループが書き込み権限を持つフォルダを作成します。例えば、
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
 
# cd /var/lib/jail
134行目: 134行目:
 
# chmod 730 uploads
 
# chmod 730 uploads
   
  +
{{Note|この場合、"sshusers" グループのユーザのみが "uploads" ディレクトリにアップロードすることができます (ただし、その内容を表示することはできません)。sshusers がコンテンツを閲覧できるようにするには、 {{ic|chmod 770}} を使用してください。}}
{{Note|This will only allow users of group "sshusers" to upload to (but not list the contents of) the "uploads" directory. Use {{ic|chmod 770}} to allow sshusers to view contents.}}
 
   
  +
SFTP を利用するアプリケーションの中には、操作(ファイルのアップロードなど)を行う際にサブディレクトリの入力を許可せず、chroot のベースディレクトリにファイルをアップロードしようとする(これは拒否される)ものがあります。これらのアプリケーションで特定のサブディレクトリを使用するように強制するには、"ForceCommand" オプションに以下を追加してください。
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:
 
   
 
{{hc|/etc/ssh/sshd_config|
 
{{hc|/etc/ssh/sshd_config|
145行目: 145行目:
 
}}
 
}}
   
  +
接続したユーザーのスタートディレクトリは、指定したサブディレクトリに変更されます(sshdサーバーの再起動を忘れずに)。
Users on connect will then have their start directory change to the specified sub-directory (remember to restart the sshd server).
 
  +
  +
{{TranslationStatus|SCP and SFTP|2023-1-19|730790}}

2023年1月19日 (木) 13:47時点における最新版

関連記事

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 監獄 による Secure file transfer protocol (SFTP)

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

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

監獄ディレクトリの作成:

# mkdir -p /var/lib/jail

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

# mount -o bind /mnt/data/share /var/lib/jail
ヒント: バインドマウントが再起動しても維持されるように、/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 パスにコピーしてください。

Chroot 監獄のルートディレクトリにアップロードする

セキュリティ上の理由から、chroot ディレクトリとして設定されたディレクトリは、rootが所有し、rootのみが書き込み権限を持つ必要があり、root でなければ sftp/ssh 接続が拒否されます。もちろん、これは一般ユーザがルートディレクトリにファイルをアップロードできないことを意味します。この問題を回避し、セキュリティを損なわないようにするには、chroot ディレクトリ内に、通常のユーザまたはグループが書き込み権限を持つフォルダを作成します。例えば、

# cd /var/lib/jail
# mkdir uploads
# chown :sshusers uploads
# chmod 730 uploads
ノート: この場合、"sshusers" グループのユーザのみが "uploads" ディレクトリにアップロードすることができます (ただし、その内容を表示することはできません)。sshusers がコンテンツを閲覧できるようにするには、 chmod 770 を使用してください。

SFTP を利用するアプリケーションの中には、操作(ファイルのアップロードなど)を行う際にサブディレクトリの入力を許可せず、chroot のベースディレクトリにファイルをアップロードしようとする(これは拒否される)ものがあります。これらのアプリケーションで特定のサブディレクトリを使用するように強制するには、"ForceCommand" オプションに以下を追加してください。

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

接続したユーザーのスタートディレクトリは、指定したサブディレクトリに変更されます(sshdサーバーの再起動を忘れずに)。

翻訳ステータス: このページは en:SCP and SFTP の翻訳バージョンです。最後の翻訳日は 2023-1-19 です。もし英語版に 変更 があれば、翻訳の同期を手伝うことができます。