0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SSHDを複数起動してSFTPのportと参照先ディレクトリを分けてみる

Last updated at Posted at 2024-07-11

はじめに

sshdでsubsystemの指定でsftpサーバ利用できますが、sshのポートやディレクトリが同じ(port:22)になってしまいます。
sftpサーバは社外に公開しつつも、セキュリティを考えると使用するポートや参照先ディレクトリは分けたい、ってことで以下の設定を実施してみました。

前提条件

sftp構築済であること。
通常のsftp構築は以下が参考になります。
OpenSSHで1機能として備わっているsftpを有効化していくって感じです。
特にOpenSSHでSFTPを有効にする設定は必須。

/etc/ssh/sshd_config
Subsystem       sftp    /usr/libexec/openssh/sftp-server

要件(やりたいこと)

・通常のSFTP構築をする。(上記で実施済み前提)
・sftp getするユーザはsftp(同じく上記で作成済み前提)
・RSA暗号方式による公開鍵でのみログインを許可し、パスワード認証は無効化する。
・ポート番号は2222を使用する。
・sftpプロセスは、通常のsshdと別プロセスとして起動する。
・特定のport(port2222)指定の場合は別のディレクトリ(/tom/TEST)を参照する。

やり方

systemd用のファイルを用意する。

プロセスとして分けたいので/usr/lib/systemd/system配下のsshd.serviceをコピーする。

sshd.serviceをコピーする
[root@SERVER-01:~]# ll /usr/lib/systemd/system/sshd*
-rw-r--r-- 1 root root 313  8月  5  2023 /usr/lib/systemd/system/sshd-keygen.service
-rw-r--r-- 1 root root 373  8月  5  2023 /usr/lib/systemd/system/sshd.service  ★これをcpする
-rw-r--r-- 1 root root 181  8月  5  2023 /usr/lib/systemd/system/sshd.socket
-rw-r--r-- 1 root root 260  8月  5  2023 /usr/lib/systemd/system/sshd@.service

[root@SERVER-01:~]# cp -ip /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd_sftp.service

[root@SERVER-01:~]# ll /usr/lib/systemd/system/sshd*
-rw-r--r-- 1 root root 313  8月  5  2023 /usr/lib/systemd/system/sshd-keygen.service
-rw-r--r-- 1 root root 373  8月  5  2023 /usr/lib/systemd/system/sshd.service
-rw-r--r-- 1 root root 181  8月  5  2023 /usr/lib/systemd/system/sshd.socket
-rw-r--r-- 1 root root 260  8月  5  2023 /usr/lib/systemd/system/sshd@.service
-rw-r----- 1 root root 472  7月  8 13:50 /usr/lib/systemd/system/sshd_sftp.service  ★追加

sshd_sftp.serviceの中身はこんな感じにカスタマイズ

sshd_sftp.serviceの中身
[root@SERVER-01:~]# cat /usr/lib/systemd/system/sshd_sftp.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd_sftp        ★参照先変更
ExecStart=/usr/sbin/sshd_sftp -D $OPTIONS -f /etc/ssh_sftp/sshd_config ★読み込みconf変更
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

参照先は/etc/sysconfig/sshd → /etc/sysconfig/sshd_sftpに変更
読み込むconfを/etc/ssh_sftp/sshd_configを指定(オプション -f)

configを用意する。

/etc/sysconfig/sshd_sftp の中身はこんな感じ(/etc/sysconfig/sshdと同じ設定なので単純にcpでよさそう)

/etc/sysconfig/sshd_sftp
[root@SERVER-01:~]# cat /etc/sysconfig/sshd_sftp 
SSH_USE_STRONG_RNG=0

sshd_configもcpする
設定→テストを繰り返し、いろいろカスタマイズした結果、最終的に以下で落ち着いた。

/etc/ssh_sftp/sshd_config(コメントアウト除く)
[root@SERVER-01:~]# cat /etc/ssh_sftp/sshd_config
Port 2222
PidFile /var/run/sshd_sftp.pid
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
PubkeyAuthentication no
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
ChallengeResponseAuthentication no
GSSAPIAuthentication no
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp internal-sftp -l INFO
Match User sftp
       PasswordAuthentication no
       PubkeyAuthentication yes
       AuthorizedKeysFile /home/sftp/.ssh/authorized_keys
       AllowTcpForwarding no
       ChrootDirectory /tom/TEST
       ForceCommand internal-sftp -u 002 -l INFO

Port 2222はport2222指定
Match User sftpでsftpユーザ指定
PasswordAuthentication noでパスワード認証は拒否 (パスワード認証にしたい場合はyes)
PubkeyAuthentication yesで公開鍵認証はOK
鍵は/home/sftp/.ssh/authorized_keysとする。
ChrootDirectoryでsftp参照先を指定(今回は/tom/TEST)
ForceCommandの-l INFOでsftp系のログ出力のレベルを指定できる。
何を取りにきたかは知りたいのでINFOで設定

ディレクトリ作成する。

sftp参照先ディレクトリ作成する

sftp参照先ディレクトリ作成
mkdir /tom/kiwi_link
chmod 755 /tom/kiwi_link

鍵情報配置

鍵は事前にsftp取得元サーバで作っておきましょう
https://qiita.com/soma_sekimoto/items/35845495bc565c38ae9d

公開鍵情報をauthorized_keysに追加します。
パスワード認証の場合は不要です。

公開鍵登録
echo "公開鍵鍵情報をここに入れる">> /home/sftp/.ssh/authorized_keys
chown sftp:sftp /home/sftp/.ssh/authorized_keys
chmod 400 /home/sftp/.ssh/authorized_keys

プロセスリスタート

エラーでなければOK
一応sshd.serviceもリスタートする。

プロセスリスタート
sudo systemctl restart sshd.service
sudo systemctl restart sshd_sftp.service

テスト実施

公開鍵を発行したサーバからsftp getするか確かめる。
sftp参照先ディレクトリ「/tom/TEST」にダミーファイル「sample2.txt」を配置

ダミーファイル「sample2.txt」を配置
[root@SERVER-01:~]# ll /tom/TEST
-rwxrwxrwx 1 root root        4  6月 26 17:46 sample2.txt

Port 2222指定でsftpログイン&getしてみる
sftpでポート指定するオプションは[-oPort="ポート番号"]

sftpログイン
[user01@SERVER-02:~]$ sftp -oPort="2222" sftp@SERVER-01
Connected to SERVER-01.
sftp>
sftp> get sample2.txt
Fetching /sample2.txt to sample2.txt
/sample2.txt                                                                                                                                                100%    4     4.3KB/s   00:00    
sftp> 
sftp> exit

[user01@SERVER-02:~]$ ll
-rwxrwxr-x  1 user01 user01        4  7月  9 19:05 sample2.txt

取得できた!

ログは「/var/log/secure」に保存される。
ログ出力状況としてはこんな感じ

port2222指定sftpログイン
Jul  8 13:10:21 SERVER-01 sshd_sftp[5725]: Accepted publickey for sftp from 100.64.204.26 port 48262 ssh2: RSA SHA256:
Jul  8 13:10:21 SERVER-01 sshd_sftp[5725]: pam_unix(sshd_sftp:session): session opened for user sftp by (uid=0)
Jul  8 13:10:21 SERVER-01 sshd_sftp[5725]: session opened for local user sftp from [100.64.204.26] [postauth]
pwd (/tom/TEST配下確認)
Jul  8 13:10:37 SERVER-01 sshd_sftp[5725]: opendir "/" [postauth]
Jul  8 13:10:37 SERVER-01 sshd_sftp[5725]: closedir "/" [postauth]
get sample2.txt (存在するファイルsample2.txt取得)
Jul  8 13:11:29 SERVER-01 sshd_sftp[5725]: open "/sample2.txt" flags READ mode 0666 [postauth]
Jul  8 13:11:29 SERVER-01 sshd_sftp[5725]: close "/sample2.txt" bytes read 5 written 0 [postauth]
get sample3.txt (存在しないファイルGETしようとした場合)
Jul  8 13:11:35 SERVER-01 sshd_sftp[5725]: sent status No such file [postauth]

「/var/log/sftp」に出力させようといろいろ設定試みたが、うまくいかず、最終的に「/var/log/secure」に落ち着いた。
こちらであれば特に追加の設定は不要だが、「/var/log/secure」の場合はsudo系のコマンド等も記録されるのでログ監視系にかませる場合は複数条件にするとよい。

通常のsftp操作ログは以下文献が参考になる。(実際出力された)
https://qiita.com/Sophick12224/items/b62e5f1af335a29217e0

まとめ

サーバ構築する上で、パスワードや鍵情報を持っているサーバが自由に取りに行くのは望ましくないとお偉いさんに言われたら今回の設定をしてみましょう。

おわおわり

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?