0
2

More than 3 years have passed since last update.

EC2インスタンス起動後に必要なセキュリティ設定

Posted at

EC2 に SSH でログインした後、最低限必要な設定をまとめます。

ユーザ設定

ユーザ追加、パスワード設定

# ユーザ追加とパスワード設定
sudo useradd hogehoge
sudo passwd hogehoge

wheel グループに追加

作成したユーザのディレクトリを確認

# wheelグループに追加
sudo usermod -aG wheel hogehoge

# wheelに所属しているユーザーの確認
less /etc/group | grep wheel

公開鍵設定

# 公開鍵のコピーとパーミッション設定
sudo cp -R ~/.ssh/ /home/hogehoge/.ssh
sudo chown -R hogehoge:hogehoge /home/hogehoge/.ssh
sudo chmod -R go-rwx /home/hogehoge/.ssh

SSH 設定

ec2-user へ直接 ssh を禁止

sudo echo "DenyUsers ec2-user" >> /etc/ssh/sshd_config
sudo systemctl restart sshd

SSH ポート変更

sudo vi /etc/ssh/sshd_config

# デフォルトListen Portを修正
# Port 22
Port 12345

sshd を再起動しておきます

sudo systemctl restart sshd

※指定したポートは AWS の Security Group で開けておいてください

セキュリティパッチの自動実行設定

賛否両論ありますが yum update を定期的に実行してくれる設定をします。

sudo yum install yum-cron

# 設定ファイルのバックアップ
sudo cp /etc/yum/yum-cron.conf /etc/yum/yum-cron.conf.backup

# sed コマンドで書き換え
sudo sed -i "s/^update_cmd.*$/update_cmd = security/g" /etc/yum/yum-cron.conf
sudo sed -i "s/^apply_updates.*$/apply_updates = yes/g" /etc/yum/yum-cron.conf

# yum-cronの起動と自動起動の設定
# 起動
sudo systemctl start yum-cron

# 自動起動の設定
sudo systemctl enable yum-cron

# ステータスの確認
systemctl status yum-cron

タイムゾーンの変更

timedatectl status
sudo timedatectl set-timezone Asia/Tokyo
0
2
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
0
2