「SCP と SFTP」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(→‎chroot 監獄の追加: add ==== Uploads to Chroot jail root dir ====)
22行目: 22行目:
 
''sftp'' プログラムや [[sshfs]] を使ってファイルを操作できるはずです。標準的な FTP プログラムも大抵は動作します。
 
''sftp'' プログラムや [[sshfs]] を使ってファイルを操作できるはずです。標準的な FTP プログラムも大抵は動作します。
   
== Secure file transfer protocol (SFTP) with a chroot jail ==
+
== chroot jail による Secure file transfer protocol (SFTP) ==
   
  +
システム管理者は、{{Pkg|openssh}} を使って、ユーザのサブセットを chroot jail にし、特定のディレクトリツリーへのアクセスを制限することができます。これは、完全なシステムアクセスやシェルアクセスを許可することなく、単にいくつかのファイルを共有するのに便利です。このタイプの設定のユーザは、{{Pkg|filezilla}} のような SFTP クライアントを使用して、chroot jail 内にファイルを置いたり取得したりすることができます。
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 ===
 
=== Setup the filesystem ===

2023年1月16日 (月) 10:14時点における版

関連記事

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 内にファイルを置いたり取得したりすることができます。

Setup the filesystem

Create a jail directory:

# mkdir -p /var/lib/jail

Optionally, bind mount the filesystem to be shared to this directory. In this example, /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
ヒント: Consider adding an entry to /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
# passwd foo

Setup OpenSSH

Add the following to the end of /etc/ssh/sshd_config to enable the share and to enforce the restrictions:

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

Restart 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.

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:

$ 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 などのパッケージをインストールすることでさらに機能を追加できます。下を参照。

General Usage

Linux to Linux

Copy file from a remote host to local host SCP example:

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

Copy file from local host to a remote host SCP example:

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

Copy directory from a remote host to local host SCP example:

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

Copy directory from local host to a remote host SCP example:

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

Copy file from remote host to remote host SCP example:

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

Linux to Windows

Use a Windows program such as WinSCP

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).