pam + opensshでユーザごとのログインIP制限

たいていは/etc/hosts.allowで事足りるんだけど、厳密に制限したいなっていう時に。いろいろman 見てたらpam使えばできそう。

ref.

opensshの設定

最近のはデフォルトでyesになってるはず。

UsePam yes

pamの設定

account required の行を足す。で、accessfileは指定しなかったら/etc/security/access.conf になるんだけど、管理しやすい用に専用のファイルで作ってみた。

account required pam_access.so accessfile=/etc/security/sshd_access.conf

accessfileの設定

前述したように、access.confでもいいんだけど、/etc/security/sshd_access.conf に書いた。

  • ユーザhoge 192.168.0.1 から
  • ユーザfuga 10.0.0.0/24 からのみ許可する例

全部拒否って、特定のIPだけ許可(EXCEPT)

- : hoge : ALL EXCEPT 192.168.0.1
- : fuga : ALL EXCEPT 10.0.0.

もしくは、特定のIPを許可して、他を全て拒否。

+ : hoge : 192.168.0.1
+ : fuga : 10.0.0.
- : ALL : ALL
# or 
# - : hoge : ALL
# - : fuga : ALL

"+"で許可する場合は、最後にdenyするルールを書かないと他のIPからも接続できてしまうので注意。

追記

ぐぐってhitしたページ(忘れた)にはIPアドレスの末尾に"."を書くこと、と記述されたりしていたけど、たぶんそれは必要はない。末尾に"."を書くのはnetwork で書く場合だけ。

# allow from only 10.0.0.0/24 network
- : fuga : ALL EXCEPT 10.0.0.

man access.conf にも書いてる。

The third field, the origins field, should be a list of one or more tty names (for non-networked logins), host names, domain names (begin with "."), host addresses, internet network numbers (end with "."), internet network addresses with network mask (where network mask can be a decimal number or an internet address also), ALL (which always matches) or LOCAL (which matches any string that does not contain a "." character).
If supported by the system you can use @netgroupname in host or user patterns.