본문 바로가기
Project/3-Tier Architecture

[3-Tier] AWS WEB EC2 생성 후 Bastion Host에서 SSH 접속하기 (WEB 서버 구성1)

by ganyga 2024. 2. 22.

WEB EC2 생성하기

 

OS : Amazon Linux2

 

인스턴스 유형 : t2.micro

 

네트워크 설정

  • VPC : Ga-VPC
  • Subnet : Ga-WEB-Pri-A
  • 퍼블릭 IP 자동 할당 : 비활성화 (Private Subnet 이므로)
  • 보안 그룹 생성 : Ga-WEB-A-SG


보안그룹 설정(WEB-SG)

인바운드 규칙 편집(Bastion 사설 IP SSH허용)

  • Bastion Host에서 WEB EC2로 접근하기 위해 Bastion 사설 IP 인바운드규칙 SSH 허용


Bastion Host에 WEB Keypair 옮기기

Bastion 보안 그룹에서 SSH local 공인 IP 인바운드 허용하기

curl ifconfig.me로 공인 IP 조회하기

 

Keypair 파일이 저장된 경로로 이동 후 chmod 400 권한 부여

cd Desktop/Keypair

ll
total 16
-r--------@ 1 gany  staff   1.6K  3 15 16:10 Ga-Bastion.pem
-rw-r--r--@ 1 gany  staff   1.6K  3 15 16:13 Ga-WEB.pem

chmod 400 Ga-WEB.pem
-r--------@ 1 gany  staff   1.6K  3 15 16:10 Ga-Bastion.pem
-r--------@ 1 gany  staff   1.6K  3 15 16:13 Ga-WEB.pem

 

SCP 명령어) local(로컬) → remote(원격지)

scp -i [접속할 Keypair][옮길 Keypair][사용자 ID] @ [접속할 IP]:[경로]

scp -i "Ga-Bastion.pem" "Ga-WEB.pem" ec2-user@52.79.253.139:/home/ec2-user/

# yes

Ga-WEB.pem                                   100% 1674   173.9KB/s   00:00

 

Bastion EC2에 접속해서 Keypair 옮겨졌나 확인

 ssh -i Ga-Bastion.pem ec2-user@52.79.253.139
   ,     #_
   ~\_  ####_        Amazon Linux 2
  ~~  \_#####\
  ~~     \###|       AL2 End of Life is 2025-06-30.
  ~~       \#/ ___
   ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
      ~~._.   _/
         _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
       _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/

[ec2-user@ip-10-100-1-53 ~]$
[ec2-user@ip-10-100-1-53 ~]$ sudo su -
[root@ip-10-100-1-53 ~]# cd /home/ec2-user/
[root@ip-10-100-1-53 ec2-user]# ll
total 4
-r-------- 1 ec2-user ec2-user 1678 Mar 15 07:24 Ga-WEB.pem

Bastion hostname 변경하기

[root@ip-10-100-1-53 ec2-user]# hostnamectl set-hostname bastion
[root@ip-10-100-1-53 ec2-user]# su
[root@bastion ec2-user]# hostname
bastion

 

 

Bastion Host → WEB EC2 SSH 접속

[root@bastion ec2-user]# ssh -i "Ga-WEB.pem" ec2-user@10.100.2.155

The authenticity of host '10.100.2.155 (10.100.2.155)' can't be established.
ECDSA key fingerprint is SHA256:2JtTf/I6sz/YdNY/M00xDIWiIyYkYN9qbv8ylPX6rkc.
ECDSA key fingerprint is MD5:cf:1f:bc:cb:4b:b5:2b:26:6b:e5:8e:ee:5e:cb:41:51.
Are you sure you want to continue connecting (yes/no)?  yes

Warning: Permanently added '10.100.2.155' (ECDSA) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2
  ~~  \_#####\
  ~~     \###|       AL2 End of Life is 2025-06-30.
  ~~       \#/ ___
   ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
      ~~._.   _/
         _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
       _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
       

[ec2-user@ip-10-100-2-155 ~]$

 

hostname 변경하기

hostnamectl set-hostname [변경할 이름]

[ec2-user@ip-10-100-2-155 ~]$ sudo su -
[root@ip-10-100-2-155 ~]# hostnamectl set-hostname web-a

# 적용
[root@ip-10-100-2-155 ~]# su
[root@web-a ~]# hostname
web-a