sshd_config の設定で接続元ごとに認証方法などの設定を変える

スポンサーリンク
スポンサーリンク

sshd_config の設定で、接続元ごとに認証方法などの設定を変える方法です。

以前、この記事 で、sshサーバの複数起動によって、接続元によって通常のパスワード認証と公開鍵認証を使い分ける方法を説明しました。
今頃になって知りましたが、CentOS7,Ubuntuでは、sshd_config の設定で同様のことが出来ます。
またCentOS6 では、 鍵ファイルの指定 AuthorizedKeysFile が接続元ごとに出来ませんが、それを除けば同様のことが出来ます。

まず、/etc/ssh/sshd_config で以下のように、Portを複数行書くと、各ポートで sshd は待ち受けます。

Port 22
Port 2222

また、/etc/ssh/sshd_config で Match を使えば、接続元や接続ポート番号等によって、sshdの挙動を変えることが出来ます。

sshd_config の末尾に、次の書式で追加します。

Match 条件
オプション
オプション
   :

条件にマッチした接続では、オプションで指定した内容で設定が上書きされます。
オプション は、他のMatchが現れるか、ファイル末尾になるまでになります。

例えば、次のようにすると、接続元アドレスが 192.168.0.100 の場合に、PasswordAuthentication no(パスワード認証しない。公開鍵認証の必要あり)で接続します。

Match Address 192.168.0.100
      PasswordAuthentication no

指定できる条件と、オプションは Ubuntu、CentOS6/7 で異なります。
これは SSHD のバージョンによるものです。

CentOS6

指定できる条件
User, Group, Host, Address
指定できるオプション
AllowAgentForwarding, AllowTcpForwarding, Banner,
ChrootDirectory, ForceCommand, GatewayPorts, GSSAPIAuthentication,
HostbasedAuthentication, KbdInteractiveAuthentication,
KerberosAuthentication, KerberosUseKuserok, MaxAuthTries, MaxSessions,
PubkeyAuthentication, AuthorizedKeysCommand, AuthorizedKeysCommandRunAs,
PasswordAuthentication, PermitEmptyPasswords, PermitOpen, PermitRootLogin,
RequiredAuthentications1, RequiredAuthentications2, RhostsRSAAuthentication,
RSAAuthentication, X11DisplayOffset, X11Forwarding and X11UseLocalHost

CentOS7

指定できる条件
User, Group, Host, LocalAddress, LocalPort, Address
指定できるオプション
AcceptEnv, AllowAgentForwarding, AllowGroups,
AllowTcpForwarding, AllowUsers, AuthenticationMethods, AuthorizedKeysCommand,
AuthorizedKeysCommandUser, AuthorizedKeysFile, AuthorizedPrincipalsFile, Banner,
ChrootDirectory, DenyGroups, DenyUsers, ForceCommand, GatewayPorts,
GSSAPIAuthentication, HostbasedAuthentication, HostbasedUsesNameFromPacketOnly,
KbdInteractiveAuthentication, KerberosAuthentication, KerberosUseKuserok,
MaxAuthTries, MaxSessions, PasswordAuthentication, PermitEmptyPasswords, PermitOpen,
PermitRootLogin, PermitTunnel, PubkeyAuthentication, RekeyLimit,
RhostsRSAAuthentication, RSAAuthentication, X11DisplayOffset, X11Forwarding and
X11UseLocalHost.

Ubuntu12.04以降

指定できる条件
User, Group, Host, LocalAddress, LocalPort,Address
指定できるオプション
AcceptEnv,
AllowAgentForwarding, AllowGroups, AllowTcpForwarding,
AllowUsers, AuthenticationMethods, AuthorizedKeysCommand,
AuthorizedKeysCommandUser, AuthorizedKeysFile,
AuthorizedPrincipalsFile, Banner, ChrootDirectory, DenyGroups,
DenyUsers, ForceCommand, GatewayPorts, GSSAPIAuthentication,
HostbasedAuthentication, HostbasedUsesNameFromPacketOnly,
KbdInteractiveAuthentication, KerberosAuthentication,
MaxAuthTries, MaxSessions, PasswordAuthentication,
PermitEmptyPasswords, PermitOpen, PermitRootLogin, PermitTTY,
PermitTunnel, PermitUserRC, PubkeyAuthentication, RekeyLimit,
RhostsRSAAuthentication, RSAAuthentication, X11DisplayOffset,
X11Forwarding and X11UseLocalHost.

条件では Address、LocalAddress で指定するアドレスは、192.168.0.0/24 といったCIDR形式も可能です。
また 192.168.0.*、192.168.0.?、*.co.uk といったワイルドカードも使用可能です。
否定を表す場合は、! を付けて、!10.0.0.5 のようにします。

複数条件では、下記のようにスペース区切りで条件を列挙すると AND になります。
下記では、Address が 192.168.0.10 で、LocalPort 2222 の場合にマッチします。

Match Address 192.168.0.10 LocalPort 2222
      PasswordAuthentication no

条件を,で区切ると OR になります。
下記では、「Address が 10.0.1.0/24 または 127.0.0.1」かつ「LocalPort 22」の場合にマッチします。

Match Address 10.0.1.0/24,127.0.0.1 LocalPort 22
      PasswordAuthentication no

参考

SSH力をつけよう
外からは公開鍵、内側だけはパスワード許可 – sshd の ローカル側だけパスワードを使えるようにする。 – それマグで!
2014/12/06 – sshd のコンフィグファイルの条件分岐 ― togakushi.bitbucket.org 0.1 documentation
Ubuntu Manpage: sshd_config ― OpenSSH SSH daemon configuration file

コメント

タイトルとURLをコピーしました