LoginSignup
10
14

More than 5 years have passed since last update.

Amazon EC2でCentOSを使う際の初期設定

Last updated at Posted at 2015-07-11

はじめに

こんにちは、新卒クラウドゆとりのえのかわです。
EC2のCentOSはAmazon Linuxとは違った点があるので初期設定をまとめてみました。なにか間違っている点などありましたらご指摘して頂けると嬉しいです。

パッケージのアップデート

ec2-userではなくrootでSSHログインしてパッケージをアップデートします。

[enokawa]$ ssh -i key-pair.pem root@ec2-**-**-**-**.ap-northeast-1.compute.amazonaws.com 
# cat /etc/redhat-release
CentOS release 6.5 (Final)

# yum -y update

# cat /etc/redhat-release
CentOS release 6.6 (Final)

日本時間に設定する

デフォルトではUTCなので、JSTに設定します。

# cp -p /usr/share/zoneinfo/Japan /etc/localtime
cp: overwrite `/etc/localtime'? y
# date
Fri Jul 10 10:50:43 JST 2015

※追記
シンボリックリンクを作る方法もあるみたいです。

$ ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

SELinux無効化

デフォルトではSELinuxが有効になっていますので無効化します。

# setenforce 0
# vi /etc/sysconfig/selinux
# cat /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

ファイアーウォールの無効化

AWSのSecurityGroupでアクセスを制御しましょう。

# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

# chkconfig iptables off
# chkconfig iptables --list
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

rootでSSHログインできないようにする

新たにec2-userを作成し、rootアカウントではSSHログインできないように設定します。

# adduser ec2-user
# cp -r ~/.ssh/ /home/ec2-user/
# chown -R ec2-user:ec2-user /home/ec2-user/

# usermod -G wheel ec2-user
# visudo
%wheel        ALL=(ALL)       ALL
%wheel        ALL=(ALL)       NOPASSWD: ALL

# vi /etc/ssh/sshd_config
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no

# /etc/init.d/sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

これでrootアカウントではログインできなくなります。一旦ログアウトしてec2-userでしかログインできないことを確認します。
ここまで出来たらAMIを取得しておきましょう。後で使い回す際に便利です。

参考

EC2でCentOSを使う « サーバーワークス エンジニアブログ http://blog.serverworks.co.jp/tech/2013/02/25/centosonec2/

10
14
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
10
14