LoginSignup
6
8

More than 3 years have passed since last update.

firewalldで特定IPアドレスのみsshを許可する

Last updated at Posted at 2020-10-04

はじめに

腐るほど記事があるが、ピンポイントでこれをやろうとすると結構手間だったので、このトピックに絞って記載する。

環境

  • CentOS8

手順

ファイアウォールサービスを有効にする

systemctl enable firewalld.service

ファイアウォールサービスを起動する

systemctl start firewalld.service

許可されているサービスを表示する

firewall-cmd --list-services --zone=public

sshがデフォルトで許可になっているが、今回は限定するのでサービスから消す

firewall-cmd --remove-service=ssh --zone=public --permanent

richルールにより特定IPアドレス(ここでは192.168.11.29)のみ許可

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.11.29" port protocol="tcp" port="22" accept"

リロード

firewall-cmd --reload

設定確認

[root@localhost ~]# firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: cockpit dhcpv6-client
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
        rule family="ipv4" source address="192.168.11.29" port port="22" protocol="tcp" accept

別端末からポートスキャンで確認

接続が許可されている端末、されていない端末からnmapでポートスキャンし、許可されている端末からのみ22番が見えることを確認する。

[kimisyo@localhost ~]$ nmap -p 1-500 192.168.11.24
Starting Nmap 7.70 ( https://nmap.org ) at 2020-10-04 12:06 JST
Nmap scan report for 192.168.11.24
Host is up (0.62s latency).
Not shown: 498 filtered ports
PORT    STATE SERVICE
22/tcp  open  ssh

Nmap done: 1 IP address (1 host up) scanned in 38.56 seconds

参考

6
8
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
6
8