IPアドレスに基づいて特定のユーザーのsshログインを制限する

Aug 20 2020

ssh特定のユーザーのログインを1つのIPアドレスからのみ許可されるように制限するにはどうすればよいですか?

fooサーバーでIPアドレス127.0.0.5(最初のサーバー)などの名前のユーザーがいてssh、公開鍵のみを使用してログインを終了しました。特定のIP(別のサーバー)(IPなど)からこのユーザーにのみアクセスログインを許可したい127.0.0.6

どうしてそれは可能ですか?

回答

2 GiacomoCatenazzi Aug 20 2020 at 20:50

sshdの構成は次の場所で変更できます/etc/ssh/sshd_config

AllowUsers [email protected] [email protected] ....

一部のユーザーが一部の認証方法のみを使用できるように選択することもできます。

Match User my_login
AuthenticationMethods publickey

ただし、両方の点で注意してください。設定を変更する前に、物理的に制御するか、非常に注意深く行う必要があります。また、影響をよりよく理解するために、AllowUsersansAuthenticationMethodsオプションのマニュアルページを参照することをお勧めします。最終的には、必要なオプションとメソッドを追加します。

dirdi Aug 20 2020 at 20:27

ファイアウォールを設定して、特定のIPのみがポート22に接続できるようにすることができます。

/etc/nftables.conf

#!/usr/sbin/nft -f

flush ruleset

table inet firewall {

    chain inbound {
        # By default, drop all traffic unless it meets a filter
        # criteria specified by the rules that follow below.
        type filter hook input priority 0; policy drop;

        # Allow traffic from established and related packets.
        ct state established,related accept

        # Drop invalid packets.
        ct state invalid drop

        # Allow loopback traffic.
        iifname lo accept

        # Allow all ICMP and IGMP traffic, but enforce a rate limit
        # to help prevent some types of flood attacks.
        ip protocol icmp limit rate 4/second accept
        ip6 nexthdr ipv6-icmp limit rate 4/second accept
        ip protocol igmp limit rate 4/second accept

        # Allow SSH on port 22 but only from 127.0.0.6
        ip saddr 127.0.0.6 tcp dport 22 accept
    }

    chain forward {
        # Drop everything (assumes this device is not a router)
        type filter hook forward priority 0; policy drop;
    }

    chain outbound {
        # Allow all outbound traffic
        type filter hook output priority 0; policy accept;
    }
}