SSHのIP制限【AlmaLinux 8】

firewalldで制御して、SSHでのログインに対してIPアドレスでの制限をかけます。



1. firewall-cmdコマンドで実行する場合。

(1) 許可するIPアドレスの追加
# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="***.***.***.***" port protocol="tcp" port="22" accept"
もしくは
# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="***.***.***.***" service name="ssh" accept"
(2) 元のSSHサービスを削除
元々許可されているSSHサービスを削除します。これを削除しないと全部許可されてしまいます。
firewall-cmd --remove-service=ssh --zone=public --permanent
(3) 変更の反映
firewall-cmd --reload
(4) 設定の確認
firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources:
  services: cockpit dhcpv6-client http https
  ports: 
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
        rule family="ipv4" source address="***.***.***.***" service name="ssh" accept
 


2. public.xmlを編集する場合。

firewalldの設定ファイルが「/etc/firewalld/zones/public.xml」にあります。
これを直接編集します。
# cd /etc/firewalld/zones
# cp public.xml{,.default}
# vi public.xml
【変更前】
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <zone>
  3. <short>Public</short>
  4. <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  5. <service name="ssh"/>
  6. <service name="dhcpv6-client"/>
  7. </zone>

【変更後】
※【変更前】の5行目の「<service name="ssh"/>」があるとすべてのIPアドレスを許可することになりますので、必ず削除します。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <zone>
  3. <short>Public</short>
  4. <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  5. <service name="dhcpv6-client"/>
  6. <!-- (コメント行) -->
  7. <rule family="ipv4">
  8. <source address="***.***.***.***/32"/>
  9. <service name="ssh"/>
  10. <accept/>
  11. </rule>
  12. <!-- (コメント行) -->
  13. <rule family="ipv4">
  14. <source address="***.***.***.***/32"/>
  15. <service name="ssh"/>
  16. <accept/>
  17. </rule>
  18. </zone>
変更の反映
# firewall-cmd --reload
設定の確認
# firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client 
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
       rule family="ipv4" source address="***.***.***.***/32" service name="ssh" accept
       rule family="ipv4" source address="***.***.***.***/32" service name="ssh" accept
 


最終更新:2021年12月13日 10:40