AWS EC2 に SSH 接続する(その1 ビルトインユーザで)

先日立ち上げた AWS EC2 のインスタンスに、SSH 接続します。今日はひとまず、ビルトインユーザ( ec2-user ) で接続できるところまで。
必要なものは、以下の3点。

環境

  • OS X 10.8.4(12E3067)

EC2 のインスタンス の グローバル IP アドレスを調べる

インスタンスが起動している状態で、インスタンスの詳細をみる


"Public DNS" 欄には以下のような表示があり、"54-213-122-XXX" 部分が IP アドレスを示す。54-213-122-123 なら、IP アドレスは、 54.213.122.123 。
ec2-54-213-122-XXX.us-west-2.compute.amazonaws.com

ユーザを調べる

インスタンスを選択して、Action メニューより、Connect 。ec2-user ユーザで接続可能。

Key Pair を配置する

前回ダウンロードした .pem ファイルをホームディレクトリに配置し、所有者のみ読み書き*1を許可する。以下は、Key は、/tmp にあった想定で記載。


$ cd ~
$ mv /tmp/firstKey.pem .
$ chmod 600 firstKey.pem
$ ls -l firstKey.pem
-rw-------@ 1 hoge staff 1696 8 12 00:17 firstKey.pem

SSH から接続する

Mac の ターミナルを起動する。※「アプリケーション」-「ユーティリティ」-「ターミナル」。


Key Pair と user, host name( IP Address) を指定して SSH 接続する。*2


$ ssh -i firstKey.pem ec2-user@54.213.122.XXX
The authenticity of host '54.213.122.XXX (54.213.122.XX)' can't be established.
RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '54.213.122.XXX' (RSA) to the list of known hosts.

__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2013.03-release-notes/
There are 6 security update(s) out of 13 total update(s) available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-23-208 ~]$


接続できた!

root パスワードの設定

なにはなくとも、root にパスワードを設定しましょう。


sudo passwd

その他

ソフトウェアの更新

アップデートがあるよって言ってるので、更新しとく。


$ sudo yum update
Loaded plugins: priorities, security, update-motd, upgrade-helper
amzn-main | 2.1 kB 00:00
amzn-updates | 2.3 kB 00:00
Setting up Update Process
Resolving Dependencies
--> Running transaction check
---> Package aws-apitools-ec2.noarch 0:1.6.7.3-1.0.amzn1 will be updated
---> Package aws-apitools-ec2.noarch 0:1.6.8.0-1.0.amzn1 will be an update
---> Package aws-apitools-rds.noarch 0:1.13.002-1.0.amzn1 will be updated
---> Package aws-apitools-rds.noarch 0:1.14.001-1.1.amzn1 will be an update
---> Package curl.x86_64 0:7.27.0-9.32.amzn1 will be updated
---> Package curl.x86_64 0:7.27.0-11.34.amzn1 will be an update
---> Package iptables.i686 0:1.4.18-1.19.amzn1 will be updated
---> Package iptables.x86_64 0:1.4.18-1.19.amzn1 will be updated
---> Package iptables.i686 0:1.4.18-1.21.amzn1 will be an update
---> Package iptables.x86_64 0:1.4.18-1.21.amzn1 will be an update
---> Package kernel.x86_64 0:3.4.48-45.46.amzn1 will be installed
---> Package krb5-libs.i686 0:1.10.3-10.25.amzn1 will be updated
---> Package krb5-libs.x86_64 0:1.10.3-10.25.amzn1 will be updated
---> Package krb5-libs.i686 0:1.10.3-10.26.amzn1 will be an update
---> Package krb5-libs.x86_64 0:1.10.3-10.26.amzn1 will be an update
---> Package krb5-workstation.x86_64 0:1.10.3-10.25.amzn1 will be updated
---> Package krb5-workstation.x86_64 0:1.10.3-10.26.amzn1 will be an update
---> Package libcurl.x86_64 0:7.27.0-9.32.amzn1 will be updated
---> Package libcurl.x86_64 0:7.27.0-11.34.amzn1 will be an update
---> Package python-boto.noarch 0:2.9.2-1.0.amzn1 will be updated
---> Package python-boto.noarch 0:2.9.7-1.0.amzn1 will be an update
---> Package ruby.x86_64 0:1.8.7.371-2.26.amzn1 will be updated
---> Package ruby.x86_64 0:1.8.7.374-1.0.amzn1 will be an update
---> Package ruby-libs.x86_64 0:1.8.7.371-2.26.amzn1 will be updated
---> Package ruby-libs.x86_64 0:1.8.7.374-1.0.amzn1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
kernel x86_64 3.4.48-45.46.amzn1 amzn-updates 9.8 M
Updating:
aws-apitools-ec2 noarch 1.6.8.0-1.0.amzn1 amzn-updates 14 M
aws-apitools-rds noarch 1.14.001-1.1.amzn1 amzn-updates 6.5 M
curl x86_64 7.27.0-11.34.amzn1 amzn-updates 266 k
iptables i686 1.4.18-1.21.amzn1 amzn-updates 391 k
iptables x86_64 1.4.18-1.21.amzn1 amzn-updates 419 k
krb5-libs i686 1.10.3-10.26.amzn1 amzn-updates 876 k
krb5-libs x86_64 1.10.3-10.26.amzn1 amzn-updates 910 k
krb5-workstation x86_64 1.10.3-10.26.amzn1 amzn-updates 826 k
libcurl x86_64 7.27.0-11.34.amzn1 amzn-updates 220 k
python-boto noarch 2.9.7-1.0.amzn1 amzn-updates 1.5 M
ruby x86_64 1.8.7.374-1.0.amzn1 amzn-updates 382 k
ruby-libs x86_64 1.8.7.374-1.0.amzn1 amzn-updates 2.1 M

Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 12 Package(s)

Total download size: 38 M
Is this ok [y/N]: y
Downloading Packages:
(1/13): aws-apitools-ec2-1.6.8.0-1.0.amzn1.noarch.rpm | 14 MB 00:00
(2/13): aws-apitools-rds-1.14.001-1.1.amzn1.noarch.rpm | 6.5 MB 00:00
(3/13): curl-7.27.0-11.34.amzn1.x86_64.rpm | 266 kB 00:00
(4/13): iptables-1.4.18-1.21.amzn1.i686.rpm | 391 kB 00:00
(5/13): iptables-1.4.18-1.21.amzn1.x86_64.rpm | 419 kB 00:00
(6/13): kernel-3.4.48-45.46.amzn1.x86_64.rpm | 9.8 MB 00:00
(7/13): krb5-libs-1.10.3-10.26.amzn1.i686.rpm | 876 kB 00:00
(8/13): krb5-libs-1.10.3-10.26.amzn1.x86_64.rpm | 910 kB 00:00
(9/13): krb5-workstation-1.10.3-10.26.amzn1.x86_64.rpm | 826 kB 00:00
(10/13): libcurl-7.27.0-11.34.amzn1.x86_64.rpm | 220 kB 00:00
(11/13): python-boto-2.9.7-1.0.amzn1.noarch.rpm | 1.5 MB 00:00
(12/13): ruby-1.8.7.374-1.0.amzn1.x86_64.rpm | 382 kB 00:00
(13/13): ruby-libs-1.8.7.374-1.0.amzn1.x86_64.rpm | 2.1 MB 00:00
--------------------------------------------------------------------------------
Total 13 MB/s | 38 MB 00:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : krb5-libs-1.10.3-10.26.amzn1.x86_64 1/25
Updating : libcurl-7.27.0-11.34.amzn1.x86_64 2/25
Updating : ruby-libs-1.8.7.374-1.0.amzn1.x86_64 3/25
Updating : ruby-1.8.7.374-1.0.amzn1.x86_64 4/25
Updating : curl-7.27.0-11.34.amzn1.x86_64 5/25
Updating : krb5-workstation-1.10.3-10.26.amzn1.x86_64 6/25
Updating : python-boto-2.9.7-1.0.amzn1.noarch 7/25
Updating : iptables-1.4.18-1.21.amzn1.x86_64 8/25
Updating : aws-apitools-rds-1.14.001-1.1.amzn1.noarch 9/25
Updating : aws-apitools-ec2-1.6.8.0-1.0.amzn1.noarch 10/25
Installing : kernel-3.4.48-45.46.amzn1.x86_64 11/25
W: Could not find 'strip'. Not stripping binaries in the initramfs.
Updating : iptables-1.4.18-1.21.amzn1.i686 12/25
Updating : krb5-libs-1.10.3-10.26.amzn1.i686 13/25
Cleanup : python-boto-2.9.2-1.0.amzn1.noarch 14/25
Cleanup : iptables-1.4.18-1.19.amzn1 15/25
Cleanup : aws-apitools-rds-1.13.002-1.0.amzn1.noarch 16/25
Cleanup : aws-apitools-ec2-1.6.7.3-1.0.amzn1.noarch 17/25
Cleanup : krb5-libs-1.10.3-10.25.amzn1 18/25
Cleanup : ruby-1.8.7.371-2.26.amzn1.x86_64 19/25
Cleanup : krb5-workstation-1.10.3-10.25.amzn1.x86_64 20/25
Cleanup : curl-7.27.0-9.32.amzn1.x86_64 21/25
Cleanup : libcurl-7.27.0-9.32.amzn1.x86_64 22/25
Cleanup : krb5-libs-1.10.3-10.25.amzn1 23/25
Cleanup : ruby-libs-1.8.7.371-2.26.amzn1.x86_64 24/25
Cleanup : iptables-1.4.18-1.19.amzn1 25/25
Verifying : kernel-3.4.48-45.46.amzn1.x86_64 1/25
Verifying : krb5-libs-1.10.3-10.26.amzn1.i686 2/25
Verifying : aws-apitools-ec2-1.6.8.0-1.0.amzn1.noarch 3/25
Verifying : krb5-libs-1.10.3-10.26.amzn1.x86_64 4/25
Verifying : aws-apitools-rds-1.14.001-1.1.amzn1.noarch 5/25
Verifying : curl-7.27.0-11.34.amzn1.x86_64 6/25
Verifying : krb5-workstation-1.10.3-10.26.amzn1.x86_64 7/25
Verifying : iptables-1.4.18-1.21.amzn1.i686 8/25
Verifying : ruby-libs-1.8.7.374-1.0.amzn1.x86_64 9/25
Verifying : iptables-1.4.18-1.21.amzn1.x86_64 10/25
Verifying : python-boto-2.9.7-1.0.amzn1.noarch 11/25
Verifying : libcurl-7.27.0-11.34.amzn1.x86_64 12/25
Verifying : ruby-1.8.7.374-1.0.amzn1.x86_64 13/25
Verifying : aws-apitools-rds-1.13.002-1.0.amzn1.noarch 14/25
Verifying : libcurl-7.27.0-9.32.amzn1.x86_64 15/25
Verifying : krb5-libs-1.10.3-10.25.amzn1.i686 16/25
Verifying : krb5-workstation-1.10.3-10.25.amzn1.x86_64 17/25
Verifying : krb5-libs-1.10.3-10.25.amzn1.x86_64 18/25
Verifying : iptables-1.4.18-1.19.amzn1.x86_64 19/25
Verifying : curl-7.27.0-9.32.amzn1.x86_64 20/25
Verifying : ruby-1.8.7.371-2.26.amzn1.x86_64 21/25
Verifying : iptables-1.4.18-1.19.amzn1.i686 22/25
Verifying : aws-apitools-ec2-1.6.7.3-1.0.amzn1.noarch 23/25
Verifying : python-boto-2.9.2-1.0.amzn1.noarch 24/25
Verifying : ruby-libs-1.8.7.371-2.26.amzn1.x86_64 25/25

Installed:
kernel.x86_64 0:3.4.48-45.46.amzn1

Updated:
aws-apitools-ec2.noarch 0:1.6.8.0-1.0.amzn1
aws-apitools-rds.noarch 0:1.14.001-1.1.amzn1
curl.x86_64 0:7.27.0-11.34.amzn1
iptables.i686 0:1.4.18-1.21.amzn1
iptables.x86_64 0:1.4.18-1.21.amzn1
krb5-libs.i686 0:1.10.3-10.26.amzn1
krb5-libs.x86_64 0:1.10.3-10.26.amzn1
krb5-workstation.x86_64 0:1.10.3-10.26.amzn1
libcurl.x86_64 0:7.27.0-11.34.amzn1
python-boto.noarch 0:2.9.7-1.0.amzn1
ruby.x86_64 0:1.8.7.374-1.0.amzn1
ruby-libs.x86_64 0:1.8.7.374-1.0.amzn1

Complete!