EC2のLinux構築でやるべきこと
AmazonLinux_CentOS,EC2,OperatingSystem
Contents
概要
- インフラエンジニアの基本はIaaS構築から! 本記事は、EC2上に起動したLinux OSの構築でやるべきことをまとめた記事となります。対象はOSのベースのみであり、WebサーバーやDBなどミドルウェアの手順については含みません。
- 対象のディストリビューションは、Amazon Linux 2 or CentOS 7 です。他のディストリビューションであってもやるべき項目は変わりありません(手順が変わります)。また、一般的なオペレーションはコマンドレベルまで記載していない箇所もあります。ご了承ください。
- なお、本記事に完成はなく、Linux OSの構築で新しい発見があれば都度更新の予定です。
LinuxのOS初期設定
- 先ず、Linux が起動した後のOS初期設定を記載します。
初期ユーザーのパスワード変更
- 初期ユーザーのパスワード変更(鍵認証の場合は必要に応じて)
- Amazon Linuxの場合は"ec2-user"、CentOSの場合は"centos"(cloudpack-amiの場合は"cloudpack"ですね)
- 確認のためサインアウト/サインイン
rootのパスワード変更
- rootのパスワード変更(必要に応じて)
パッケージの更新
- yum update
$ sudo yum update
SELinux
- Amazon Linux 2 の場合はデフォルトで無効なため、確認のみ。(セキュリティポリシーによる)
$ getenforce
Disabled
- 以下、CentOSの場合に無効に変更する方法。
[centos@ip-xx-xx-xx-xx ~]$ getenforce
Enforcing
[centos@ip-xx-xx-xx-xx ~]$ sudo cp -p /etc/selinux/config /etc/selinux/config_20200327
[centos@ip-xx-xx-xx-xx ~]$ sudo vi /etc/selinux/config
[centos@ip-xx-xx-xx-xx ~]$ diff /etc/selinux/config_20200327 /etc/selinux/config
7c7
< SELINUX=enforcing
---
> SELINUX=disabled
[centos@ip-xx-xx-xx-xx ~]$ sudo shutdown -r now
[centos@ip-xx-xx-xx-xx ~]$ getenforce
Disabled
sudoers設定
- sudoers設定および確認。
- cloudpack-amiの場合、/etc/sudoers.d/cloudpack に以下のエントリがあるため、cloudpackユーザーはsudo可能、sudoにパスワード不要となる(通常のCentOSはcentosに読み替え)。
cloudpack ALL=(ALL) NOPASSWD: ALL
- 上記方法ではなく、対象ユーザーのセカンダリグループにwheel を追加した場合、/etc/sudoersに下記設定を行うことで、sudoにパスワード不要となる。
## Allows people in group wheel to run all commands
#%wheel ALL=(ALL) ALL
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
NTP設定(Amazon Time Sync Service)
- Amazon Time Sync Service(Server:169.254.169.123)が設定され、同期が取れていることを確認する。
- Amazon Linux 2 の場合はデフォルトで有効なため、不要。
$ systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-02-25 10:40:28 JST; 1h 40min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Main PID: 2967 (chronyd)
CGroup: /system.slice/chronyd.service
mq2967 /usr/sbin/chronyd
Feb 25 10:40:27 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal systemd[1]: ...
Feb 25 10:40:27 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal chronyd[2967]: ...
Feb 25 10:40:27 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal chronyd[2967]: ...
Feb 25 10:40:28 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal systemd[1]: ...
Feb 25 10:40:45 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal chronyd[2967]: S...
Feb 25 10:40:45 ip-xx-xx-xx-xx.ap-northeast-1.compute.internal chronyd[2967]: T...
Hint: Some lines were ellipsized, use -l to show in full.
$ chronyc tracking
Reference ID : A9FEA97B (169.254.169.123)
Stratum : 4
Ref time (UTC) : Mon Feb 25 03:21:03 2019
System time : 0.000034801 seconds fast of NTP time
Last offset : +0.000031882 seconds
RMS offset : 0.000065280 seconds
Frequency : 4.703 ppm fast
Residual freq : +0.016 ppm
Skew : 0.196 ppm
Root delay : 0.003376933 seconds
Root dispersion : 0.000516520 seconds
Update interval : 258.8 seconds
$ chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 169.254.169.123 3 6 377 19 +68ns[ +176ns] +/- 412us
- 以下、CentOSの場合にAmazon Time Sync Serviceに変更する方法。
← 変更前
[centos@ip-xx-xx-xx-xx ~]$ chronyc tracking
Reference ID : 85F3EEA3 (ntp-b2.nict.go.jp)
Stratum : 2
Ref time (UTC) : Fri Mar 27 10:52:31 2020
System time : 0.000014093 seconds fast of NTP time
Last offset : +0.000090461 seconds
RMS offset : 0.000077362 seconds
Frequency : 3.035 ppm slow
Residual freq : +0.002 ppm
Skew : 0.056 ppm
Root delay : 0.003940267 seconds
Root dispersion : 0.000853725 seconds
Update interval : 1025.1 seconds
Leap status : Normal
[centos@ip-xx-xx-xx-xx ~]$ chronyc sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^- 30-213-226-103-static.ch> 1 10 377 693 +208us[ +208us] +/- 20ms
^- kuroa.me 2 10 377 646 -1613us[-1613us] +/- 41ms
^- mx.execve.net 3 8 377 229 +22us[ +22us] +/- 48ms
^* ntp-b2.nict.go.jp 1 10 377 773 +249ns[ +91us] +/- 1971us
[centos@ip-xx-xx-xx-xx ~]$ sudo cp -p /etc/chrony.conf /etc/chrony.conf_20200327
[centos@ip-xx-xx-xx-xx ~]$ ls -l /etc/chrony.*
-rw-r--r--. 1 root root 1108 Aug 8 2019 /etc/chrony.conf
-rw-r--r--. 1 root root 1108 Aug 8 2019 /etc/chrony.conf_20200327
-rw-r-----. 1 root chrony 481 Aug 8 2019 /etc/chrony.keys
[centos@ip-xx-xx-xx-xx ~]$ sudo vi /etc/chrony.conf
[centos@ip-xx-xx-xx-xx ~]$
[centos@ip-xx-xx-xx-xx ~]$ diff /etc/chrony.conf_20200327 /etc/chrony.conf
2a3
> server 169.254.169.123 prefer iburst
[centos@ip-xx-xx-xx-xx ~]$ sudo systemctl restart chronyd
← 変更後
[centos@ip-xx-xx-xx-xx ~]$ chronyc tracking
Reference ID : A9FEA97B (169.254.169.123)
Stratum : 4
Ref time (UTC) : Fri Mar 27 11:13:21 2020
System time : 0.000000010 seconds fast of NTP time
Last offset : -0.000011045 seconds
RMS offset : 0.000011045 seconds
Frequency : 3.028 ppm slow
Residual freq : -2.836 ppm
Skew : 0.050 ppm
Root delay : 0.000233802 seconds
Root dispersion : 0.000343711 seconds
Update interval : 1.9 seconds
Leap status : Normal
[centos@ip-xx-xx-xx-xx ~]$ chronyc sources
210 Number of sources = 5
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 169.254.169.123 3 6 17 29 +336ns[ -11us] +/- 365us
^- time.cloudflare.com 3 6 17 30 +713us[ +702us] +/- 60ms
^- sv1.localdomain1.com 2 6 17 29 +90us[ +79us] +/- 29ms
^- time.cloudflare.com 3 6 17 29 +868us[ +857us] +/- 60ms
^- ntp-5.jonlight.com 2 6 17 29 +158us[ +158us] +/- 42ms
Timezone設定
- 下記サンプルは、TimezoneをJSTからUTCへ変更。(要件に合わせて変更)
$ date
Mon Feb 25 12:28:24 JST 2019
$ timedatectl status
Local time: Mon 2019-02-25 12:28:36 JST
Universal time: Mon 2019-02-25 03:28:36 UTC
RTC time: Mon 2019-02-25 03:28:35
Time zone: Asia/Tokyo (JST, +0900)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
$ sudo timedatectl set-timezone UTC
$ timedatectl status
Local time: Mon 2019-02-25 03:31:21 UTC
Universal time: Mon 2019-02-25 03:31:21 UTC
RTC time: Mon 2019-02-25 03:31:20
Time zone: UTC (UTC, +0000)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
$ date
Mon Feb 25 03:31:25 UTC 2019
Language設定
- 下記サンプルでは、デフォルトがEnglishになっていたため、今回は変更なし。(要件に合わせて変更)
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
swap領域を追加する
- インスタンスにswap領域が構成されていない場合、追加でswap領域を作成する。下記記事を参考に、swap領域を追加する。
拡張側のEBSボリュームにFSを作成
- インスタンスに拡張側のEBSボリュームが構成されている場合、追加のファイルシステムを作成する必要がある。下記記事を参考に、EBSボリュームにファイルシステムを作成する。
cloud-init設定
- 次は、cloud-initの設定を変更します。cloud-initは、Linux OSを初期設定するためのサービスです。
- Linux OSの構築後、インスタンスのイメージ(AMI)を取得することは一般的かと思います。AMIは、Auto Scalingによるスケールアウト時や単純に2台目以降を複製するケース、あるいは障害からの復旧や人為的ミスから以前の状態を復元するなどの目的で取得します。
- 下記のcloud-initの設定変更によって、インスタンスの起動時に本記事で設定した内容がデフォルトに戻らないようにします。
- 以下、設定変更するパラメータの説明です。他のパラメータは必要に応じて、調査・変更ください。
- ssh_pwauth → “1"を設定して、インスタンス初回起動時に、デフォルトでパスワード認証を選択する(後述するプライベートなサブネットに配置したEC2で認証方法にパスワード認証を選択した場合のみ)。
- preserve_hostname → “true"を設定して、インスタンス初回起動時にホスト名を初期化しない。
- repo_upgrade: none → “none"を設定して、インスタンス初回起動時にパッケージを更新しない。
- locale: en_US.UTF-8 → インスタンス初回起動時に設定するlocaleを選択する(前述の設定値に合わせる)。
- timezone: UTC → インスタンス初回起動時に設定するtimezoneを選択する(前述の設定値に合わせる)。
$ cd /etc/cloud
$ sudo cp -p cloud.cfg cloud.cfg.bak_20190308
$ sudo vi cloud.cfg
* 以下を変更
ssh_pwauth: 1
* 以下を追記
preserve_hostname: true
repo_upgrade: none
locale: en_US.UTF-8
timezone: UTC
NitroベースのインスタンスにKernelパラメータ変更
- インスタンスタイプがt3やc5、m5などNitroベースのインスタンスを使用する場合、以下Kernelパラメータを変更する。一部のディストリビューションにおいて、NVMe ドライバーのI/O オペレーションタイムアウト変更が推奨されているため。詳細はこちら。
- すでに現在のio_timeoutが最大値(例:4294967295)であれば、追加で変更の必要なし。
$ cat /sys/module/nvme_core/parameters/io_timeout
$ sudo sed -i 's/^nvme_core.io_timeout=.*//g;s/^nvme_core.max_retries=.*//g' /etc/sysctl.conf
$ sudo sed -i '/^GRUB_CMDLINE_LINUX/s/"$/ nvme_core.io_timeout=255 nvme_core.max_retries=128"/' /etc/default/grub
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
$ sudo yum -y install nvme-cli
← Kernelパラメータが変更されたことを確認
$ cat /sys/module/nvme_core/parameters/io_timeout
ホスト名変更(*)
- ホスト名を変更する(但し、AMIを取得してAuto Scalingを行う場合は、Auto Scaling時のホスト名を管理するか検討の必要あり)。
$ hostname
ip-xx-xx-xx-xx.ap-northeast-1.compute.internal
$ sudo hostnamectl set-hostname new-hostname
$ hostname
new-hostname
管理者以外のユーザーを作成
- 管理者以外のユーザー(例:develop)を作成し、認証のための秘密鍵を準備する。(要件に合わせて変更)
- 必要に応じて、作成したユーザーに管理者権限を付与する。前述のsudoers設定を参照すること。
-
ユーザー作成後、秘密鍵を使用してログインできることを確認する(パスフレーズを指定した場合は、ログイン時にパスフレーズも入力すること)。
$ sudo groupadd -g 500 develop
$ cat /etc/group
$ sudo useradd -u 500 -g develop -c develop -d /home/develop -s /bin/bash develop
$ cat /etc/passwd
$ id -a develop
$ sudo passwd develop
$ sudo su - develop
$ mkdir .ssh
$ chmod g-w .ssh
$ ls -lad .ssh
drwxr-xr-x 2 develop develop 61 Mar 6 03:50 .ssh
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/develop/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/develop/.ssh/id_rsa.
Your public key has been saved in /home/develop/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VwBbtcHe2e8aMKZnCP5t92pMtd3WbTdBz9RcNYBQpuB develop@ip-xx-xx-xx-xx.ap-northeast-1.compute.internal
The key's randomart image is:
+---[RSA 2048]----+
| o. ...++ |
| E . . +.=o+ |
| . o + . o.=.+..|
|. . . =. .o .=. |
|. o O +S. . =.|
|.. = X . + . o|
|o. + o . + . |
|.. o . |
| .. |
+----[SHA256]-----+
$ cd .ssh
$ cat id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 authorized_keys
$ ls -la
total 12
drwxr-xr-x 2 develop develop 61 Mar 6 03:50 .
drwx------ 3 develop develop 95 Mar 6 03:40 ..
-rw------- 1 develop develop 435 Mar 6 03:50 authorized_keys
-rw------- 1 develop develop 1766 Mar 6 03:48 id_rsa
-rw-r--r-- 1 develop develop 435 Mar 6 03:48 id_rsa.pub
Linuxの監査ログ設定
- CentOS あるいは Amazon Linux に標準でインストールされているauditd を使って監査ログの取得を設定する。下記記事を参考に、auditdにルールを追加する。
Linuxのユーティリティをインストール
- wgetやgitなど使用頻度の高いユーティリティをインストールする。
$ sudo yum install wget
$ sudo yum install git
- CentOSでjqをインストールする場合は、CentOS の標準リポジトリにjq が含まれないため、先にEPELリポジトリをインストールする必要あり。
[centos@ip-xx-xx-xx-xx ~]$ sudo yum install epel-release
[centos@ip-xx-xx-xx-xx ~]$ sudo yum install jq
[centos@ip-xx-xx-xx-xx ~]$ yum list installed | grep jq
jq.x86_64 1.6-1.el7 @epel
EC2 管理ツールのインストール/セットアップ
- 次に、EC2 の管理に必要なツール類をインストールする。
AWS CLI のインストール
- AWS CLI をインストールする。Amazon Linux 2 の場合はデフォルトでインストール済みのため、確認のみ。
$ which aws
/bin/aws
$ aws --version
aws-cli/1.16.69 Python/2.7.5 Linux/3.10.0-957.1.3.el7.x86_64 botocore/1.12.59
- CentOSの場合は、pipを使用してAWS CLIをインストールする。
[centos@ip-xx-xx-xx-xx ~]$ sudo yum install python3
[centos@ip-xx-xx-xx-xx ~]$ sudo pip3 install awscli
[centos@ip-xx-xx-xx-xx ~]$ which aws
/usr/local/bin/aws
[centos@ip-xx-xx-xx-xx ~]$ sudo pip3 list | grep awscli
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
awscli (1.18.30)
- 初期ユーザーおよび追加にて作成したユーザーのそれぞれにおいて、AWS CLI のセットアップを実施する。
$ aws configure
SSM Agentのインストール
- SSM Agent をインストールする。Amazon Linux 2 の場合はデフォルトでインストール済みのため、確認のみ。
# systemctl status amazon-ssm-agent
● amazon-ssm-agent.service - amazon-ssm-agent
Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-02-25 01:40:42 UTC; 1h 57min ago
Main PID: 3615 (amazon-ssm-agen)
CGroup: /system.slice/amazon-ssm-agent.service
mq3615 /usr/bin/amazon-ssm-agent
Feb 25 03:10:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:11:05 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:15:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:20:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:20:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:25:46 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:25:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:30:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:30:59 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Feb 25 03:35:57 ip--xx-xx-xx.ap-northeast-1.compute.internal amazon-ssm-agent[3615]: ...
Hint: Some lines were ellipsized, use -l to show in full.
- CentOSの場合は、yumでrpmファイルを指定してインストールする。
[centos@ip-xx-xx-xx-xx ~]$ sudo systemctl status amazon-ssm-agent
Unit amazon-ssm-agent.service could not be found.
[centos@ip-xx-xx-xx-xx ~]$ sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
Loaded plugins: fastestmirror
amazon-ssm-agent.rpm | 31 MB 00:04
Examining /var/tmp/yum-root-tlF3nd/amazon-ssm-agent.rpm: amazon-ssm-agent-2.3.930.0-1.x86_64
Marking /var/tmp/yum-root-tlF3nd/amazon-ssm-agent.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package amazon-ssm-agent.x86_64 0:2.3.930.0-1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
amazon-ssm-agent x86_64 2.3.930.0-1 /amazon-ssm-agent 119 M
Transaction Summary
================================================================================
Install 1 Package
Total size: 119 M
Installed size: 119 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : amazon-ssm-agent-2.3.930.0-1.x86_64 1/1
Created symlink from /etc/systemd/system/multi-user.target.wants/amazon-ssm-agent.service to /etc/systemd/system/amazon-ssm-agent.service.
Verifying : amazon-ssm-agent-2.3.930.0-1.x86_64 1/1
Installed:
amazon-ssm-agent.x86_64 0:2.3.930.0-1
Complete!
[centos@ip-xx-xx-xx-xx ~]$ sudo systemctl status amazon-ssm-agent
● amazon-ssm-agent.service - amazon-ssm-agent
Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-03-27 13:10:37 UTC; 4s ago
Main PID: 8923 (amazon-ssm-agen)
CGroup: /system.slice/amazon-ssm-agent.service
mq8923 /usr/bin/amazon-ssm-agent
Mar 27 13:10:39 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:40 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Mar 27 13:10:41 ip-xx-xx-xx-xx amazon-ssm-agent[8923]: 2020-03-27 13:10:...
Hint: Some lines were ellipsized, use -l to show in full.
CloudWatch Agent のインストール
- SSM(Systems Manager)のRun Commandを使用して、CloudWatch Agent をインストールする
- AWS-ConfigureAWSPackage
- Action: Install
- Name: AmazonCloudWatchAgent
- 事前に、EC2のロールにAmazonSSMFullAccessがアタッチされていることを確認する。
- CloudWatch Agentがインストールされたことを確認する。また、サービスの自動起動を有効にして、サービスを開始する。
$ yum list installed | grep cloudwatch
amazon-cloudwatch-agent.x86_64 1.208036.0-1 installed
$ systemctl status amazon-cloudwatch-agent
● amazon-cloudwatch-agent.service - Amazon CloudWatch Agent
Loaded: loaded (/etc/systemd/system/amazon-cloudwatch-agent.service; disabled; vendor preset: disabled)
Active: inactive (dead)
$ sudo systemctl enable amazon-cloudwatch-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/amazon-cloudwatch-agent.service to /etc/systemd/system/amazon-cloudwatch-agent.service.
$ sudo systemctl start amazon-cloudwatch-agent
$ systemctl status amazon-cloudwatch-agent
● amazon-cloudwatch-agent.service - Amazon CloudWatch Agent
Loaded: loaded (/etc/systemd/system/amazon-cloudwatch-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-02-25 11:18:39 UTC; 2s ago
Main PID: 10548 (start-amazon-cl)
CGroup: /system.slice/amazon-cloudwatch-agent.service
tq10548 /opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwat...
mq10562 /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-age...
Feb 25 11:18:39 ip--xx-xx-xx.ap-northeast-1.compute.internal systemd[1]: Star...
Hint: Some lines were ellipsized, use -l to show in full.
CloudWatch Agent のセットアップ(*)
- 以下記事を参考に、CloudWatch Agent をセットアップする。
OSのネットワーク/セキュリティ関連の設定
rootユーザーのsshログインを拒否
- Amazon Linux 2 の場合はデフォルトで無効なため、確認のみ。
$ sudo cat /etc/ssh/sshd_config | grep "PermitRootLogin"
PermitRootLogin no
# the setting of "PermitRootLogin without-password".
- 以下、CentOSの場合に無効に変更する方法。
[centos@ip-xx-xx-xx-xx ~]$ sudo cat /etc/ssh/sshd_config | grep "PermitRootLogin"
#PermitRootLogin yes
# the setting of "PermitRootLogin without-password".
[centos@ip-xx-xx-xx-xx ~]$ sudo cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config_20200327
[centos@ip-xx-xx-xx-xx ~]$ sudo vi /etc/ssh/sshd_config
[centos@ip-xx-xx-xx-xx ~]$ sudo diff /etc/ssh/sshd_config_20200327 /etc/ssh/sshd_config
38c38
< #PermitRootLogin yes
---
> PermitRootLogin no
[centos@ip-xx-xx-xx-xx ~]$ sudo systemctl restart sshd
鍵認証からパスワード認証に変更(セキュリティポリシーによる)
- プライベートのサブネットに配置したEC2は、鍵認証からパスワード認証に認証方法を変更する場合もある。
-
認証方法を変更後、パスワードを使用してログインできることを確認する(パスワード認証に変更後、初期ユーザーでのログイン不可を回避するため、必ず初期ユーザーのパスワードを変更後に実施すること)。
$ cd /etc/ssh
$ sudo cp -p sshd_config sshd_config.org
$ ls -l sshd_config*
-rw------- 1 root root 3905 Mar 7 09:50 sshd_config
-rw------- 1 root root 3905 Mar 7 09:50 sshd_config.org
$ sudo vi sshd_config
※下記に変更↓
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication no
$ systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-03-07 09:50:18 UTC; 25min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 7947 (sshd)
CGroup: /system.slice/sshd.service
mq7947 /usr/sbin/sshd -D
$ sudo systemctl restart sshd
sshの暗号方式からcbcモードを無効化(セキュリティポリシーによる)
- EC2がPublic Subnetに配置されている場合、念のためsshの暗号化方式を強化。
ADに登録(*)
- システムでADサービスを利用する場合は、Linux をADに登録する。
- 以下記事を参考に、ADに登録する。
ミドルウェアのインストール(*)
ミドルウェアのインストール
- システムの要件に応じて、各種ミドルウェアやツールをインストールする。
AMI を使った横展開
- 構築後にAMIを取得し、新たなサーバーに横展開 or Auto Scalingによる スケールアウトを実施する。なお、横展開後にサーバーごとに変更が必要と思われる箇所を(*)にて、マークする。参考にして頂きたい。
AmazonLinux_CentOS,EC2,OperatingSystem
Posted by takaaki
関連記事
やさしい自己認証局と自己署名証明書の作り方
概要 今回はテストのため、開発環境に自己認証局を構築し、自己認証局による自己署名 ...
CloudWatch AgentによるLinuxプロセス監視
概要 今回は、CloudWatch Agent を使用して、手軽に Linuxプ ...
EC2 Windows のsysprepにタイムゾーンを渡す方法
概要 今回は、Windows Server OS の環境で、sysprepを使っ ...
Linux Swap設定方法(CentOS 7)
LinuxにSwap領域を追加する方法 AWSから提供されているLinux のA ...
Windows Serverのネットワークレベル認証設定
Windows Server セキュリティ強化: ネットワークレベル認証 前提条 ...
ディスカッション
コメント一覧
まだ、コメントがありません