LoginSignup
1
3

More than 3 years have passed since last update.

Linuxサーバを作成した後に行うセキュリティ設定メモ(随時更新)

Last updated at Posted at 2020-09-27

はじめに

Linuxサーバを作成した後に行うセキュリティ関連の設定を毎回忘れがちであり、
後になってから脆弱な点に気づきヒヤリするときがあるので以下にメモしておく。
(随時更新)

環境

  • Ubuntu 18.04 LTS
  • CentOS 7

インストール済みパッケージのアップデート

Ubuntu18.04
$ sudo apt-get update
$ sudo apt-get upgrade
CentOS7
$ yum update

root、ユーザパスワードの変更

※英語の大文字・小文字、数字、記号を最低1文字以上、8文字以上がベスト
 記号や環境文字はクラウドサービスの仮想コンソールで入力できる文字とした方が良い

Ubuntu18.04、CentOS7
$ sudo passwd root
$ sudo passwd user

root(デフォルトアカウント)のシェル無効化

注.) 下記実行前に1つ以上のアカウントにsudo権限があることを確認する事

Ubuntu18.04、CentOS7
$ cat /etc/group
(sudo権限を付与する場合)
$ usermod -G wheel user
  • rootのシェル状態確認(有効状態)
Ubuntu18.04、CentOS7
$ cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
  • nologinのありかを探す
Ubuntu18.04、CentOS7
$ where nologin
/usr/sbin/nologin
  • root(デフォルトアカウント)のシェル無効化
Ubuntu18.04、CentOS7
$ usermod -s /usr/sbin/nologin root
$ cat /etc/passwd | grep root
root:x:0:0:root:/root:/usr/sbin/nologin
$ su -
This account is currently not available.

SSH関連設定

公開鍵認証の設定

  • 鍵作成
接続するPC
$ ssh-keygen -t rsa -b 4096
  • 鍵の置き場作成
Ubuntu18.04、CentOS7
$ cd                                    
$ mkdir .ssh
$ chmod 700 .ssh/
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
  • 鍵送信
接続するPC
$ cat ~/.ssh/id_rsa.pub | ssh user@xxx.xxx.xxx.xxx 'cat >> .ssh/authorized_keys' 
  • 公開鍵認証のよるSSHアクセス確認
接続するPC
$ ssh user@xxx.xxx.xxx.xxx -i .ssh/id_rsa -v

※この後、パスワード認証を無効化するので必ず鍵認証でのログイン成功を確認する事

SSHサーバの設定見直し

Ubuntu18.04、CentOS7
$ vi /etc/ssh/sshd_config
  • SSHをLISTENするポート番号の変更
- #Port 22
+ Port 22222
  • SSHをLISTENするIPを限定(複数NICの場合)
- #ListenAddress 0.0.0.0
+ ListenAddress xxx.xxx.xxx.xxx
  • rootのログイン禁止
- #PermitRootLogin yes
+ PermitRootLogin no
  • パスワード認証の禁止
- #PasswordAuthentication yes
+ PasswordAuthentication no
  • パスワードなしでの認証禁止
- #PermitEmptyPasswords no
+ PermitEmptyPasswords no
  • (CentOSの場合)SELinuxの設定変更
CentOS7
$ getenforce
Enforcing
$ yum install policycoreutils-python
$ which semanage
/usr/sbin/semanage
$ /usr/sbin/semanage port --list | grep ssh
ssh_port_t                    tcp      22
$ /usr/sbin/semanage port --add --type ssh_port_t --proto tcp 22222
$ /usr/sbin/semanage port --list | grep ssh
ssh_port_t                    tcp      22222,22
  • (CentOSの場合)firewalldの設定変更
CentOS7
$ firewall-cmd --permanent --remove-service=ssh
$ cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-22222.xml
$ vi /etc/firewalld/services/ssh-22222.xml
-  <port protocol="tcp" port="22"/>
+  <port protocol="tcp" port="22222"/>
$ firewall-cmd --permanent --add-service=ssh-22222
$ firewall-cmd --reload
$ firewall-cmd --list-all
  • 設定変更したらSSHプロセスを再起動
Ubuntu18.04、CentOS7
$ systemctl restart sshd
$ systemctl status sshd

不要なポートが空いてないか確認

内部から確認する

  • 接続待ちをしているTCP、UDPポートを表示

※1 LISTENポートはどこで開放しているか(Local Address:Port)要確認
※2 ESTABポートはどこと接続しているか(Peer Address:Port)要確認

Ubuntu18.04、CentOS7
$ ss -antu
$ netstat -antu

外部から確認する(nmap)

  • フルスキャン(時間かかるので注意)
サーバにアクセスできるPC
$ nmap -p 1-65535 xxx.xxx.xxx.xxx
  • ウェルノウンポートスキャン
サーバにアクセスできるPC
$ nmap -p 0-1023 xxx.xxx.xxx.xxx
  • UDPスキャン
サーバにアクセスできるPC
$ nmap -sU xxx.xxx.xxx.xxx
1
3
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
1
3