LoginSignup
0
0

More than 1 year has passed since last update.

【AWS】 EC2にSSHで接続できたときのおはなし

Posted at

Uploading-amico.png

はじめに

今回の記事をかいたきっかけ

  • 個人開発で触れたときからEC2接続がうまくいかないときが多かった。
  • 案件で必要になり試行錯誤してようやく流れるようにできるようになったのでメモです。

※個人情報は***などで消してあります。

※今回はアカウント作成者から送ってもらった情報をもとに接続するため、アカウントの作成やキーペアの取得を知りたい方は公式サイトのチュートリアルまたは検索することをおすすめします(本当は公式が良いですが所見だとわかりにくい部分もあるかと思います)。

今回接続するために共有した必要情報

①ホスト名

ec2-**-**-***-***.ap-northeast-1.compute.amazonaws.com

.ap-northeast-1はリージョン(地域)の指定です。詳しくはこちら

②ユーザー名

******

変更されているので一応伏せ字にしましたが、デフォルトだとec2-userです。

③キーペア

保存先はこちら

自分のPC名
    └── .ssh
		  ├── config
		  └── ********.pem    

隠しフォルダになるので、Finderで表示させる場合は ++.
スクリーンショット 2022-06-27 21.20.28.jpg

下準備

~/.ssh/configUserKeyChain:yesを削除しておく

ターミナルを起動して操作
.sshフォルダに移動

cd ~/.ssh

移動先でキーペアの権限を変更

chmod 700 ********.pem

600(変更権限)で!と書かれている教材が多いですが、700(フルコントロール)で行います。
参考サイトはこちら

公式も参考にしました。

いよいよSSHでEC2に接続

% ssh -i ********.pem ******@ec2-**-**-***-***.ap-northeast-1.compute.amazonaws.com

コマンドの解説

ssh -i ③キーペア名.pem ②ユーザー名@①ホスト名

接続に成功すると以下のメッセージが出る

Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-1128-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

UA Infra: Extended Security Maintenance (ESM) is not enabled.

33 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

105 additional security updates can be applied with UA Infra: ESM
Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
https://ubuntu.com/16-04

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


Last login: Sun Jun 26 06:50:07 2022 from 59.153.252.37

Git操作

Gitの操作のため、リポジトリの作成場所まで移動

******@ip-***-**-**-**:~$ cd /var/www/html/dev/

ブランチ確認

******@ip-***-**-**-**:/var/www/html/dev$ git branch
  feature/Phase_3_Dev
  feature/auto_search
  master
  quest_page
* world
  world_1
  work_page

Git操作

******@ip-***-**-**-**:/var/www/html/dev$ sudo git pull

使用しているGitのアカウントが表示されるのでパスワードを入力

Password for 'https://**********@bitbucket.org': 

最後に差分や変更内容が表示されれば完了です。

remote: Enumerating objects: 46, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 22 (delta 20), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (22/22), done.
From https://bitbucket.org/********/********
   15ec565..ba0e0e2  world      -> origin/world
   650fbfc..00aedd3  fix_toppage -> origin/fix_toppage
Updating 15ec565..ba0e0e2
Fast-forward
 public/client/css/custom.css                        |  1 +
 public/client/css/responsive.css                    | 14 +++++++++++++-
 resources/views/client/commons/head.blade.php       |  4 ++--
 resources/views/client/commons/header_top.blade.php |  6 +++---
 4 files changed, 19 insertions(+), 6 deletions(-)

接続の解除

exit

以上

0
0
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
0