8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7 のSSHでとりあえずセキュリティ

Last updated at Posted at 2016-09-30

ログイン確認

良からぬログイン試行されてない?

$ sudo lastb
username ssh:notty    hogehogehogeho.a Sun Sep 18 19:44 - 09:44  (00:00)
username ssh:notty    hogehogehogeho.a Sun Sep 18 19:39 - 09:39  (00:00)

某大陸あたりから手当たり次第に大量のログイン試行されてて鬱陶しいことがあります。
どうやらとりあえずストレートにport22を狙ってきてる様子。

Port変更とroot無効化

とりあえず手取り早い対策としてrootでのログイン無効化とSSHのPortを換えてしまいましょう。

$ sudo vi /etc/ssh/sshd_config
/etc/ssh/sshd_config
# ポート2222に変更する場合
Port 2222

...中略...

PermitRootLogin no

SE Linux対策

必要な人のみ。

$ getenforce
Enforcing

となってる場合は以下の実施が必要。でないと設定したポート番号でdaemonがlistenできず、起動しなくなります。

semanageコマンドが使えない場合は予めインストールしておきます。

 sudo yum install -y policycoreutils-python

TCPポート2222をオブジェクトタイプssh_port_tに

 sudo semanage port -a -t ssh_port_t -p tcp 2222

Deaemon再起動

daemon再起動して、2222/tcpがListenしていることを確認します。

$ sudo systemctl restart sshd
$ ss -ltn
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      100    127.0.0.1:25                       *:*                 
LISTEN     0      128          *:2222                     *:*
LISTEN     0      100        ::1:25                      :::*
LISTEN     0      128         :::2222                    :::*

firewall

$ sudo firewall-cmd --add-port=2222/tcp --permanent
$ sudo firewall-cmd --remove-service=ssh --permanent
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client
  ports: 2222/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

sshdのポート変更せずにfirewalldでポート転送する(お手軽)

2016/11/23追記

sshdのポートは22のままで、2222/tcpなどのアクセスを22/tcpに転送します。
外からみれば22は塞がれているので、ポート変更と同じ効果がありますが、sshdがlistenするポートは変わらないので、SELinux対策も不要。

また、publicでないzoneからはそのまま22/tcpを利用することも可能で、柔軟に対応できるのでこっちのほうがオススメかも。

firewalldを設定

$ sudo firewall-cmd --add-forward-port=port=2222:proto=tcp:toport=22 --permanent
$ sudo firewall-cmd --remove-service=ssh --permanent
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client
  ports:
  masquerade: no
  forward-ports: port=2222:proto=tcp:toport=22:toaddr=
  icmp-blocks:
  rich rules:

これで多少は。。。 なにもしないよりだいぶいいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?