VM 에서 점프 호스트로 private 인스턴스 접속

728x90

Bastion Host 생성

키 페어는 vagrant 의 컨트롤러에서 생성한 id_rsa.pub 를 사용하도록 한다.

 

보통 bastion host 는 ssh 포트를 변경한다. sshd_config 에서 포트 번호 변경 가능

[vagrant@controller ~]$ cat .ssh/config
Host bastion
        HostName 3.36.118.255
        User ec2-user
        Port 22
        IdentityFile ~/.ssh/id_rsa
[vagrant@controller ~]$ ssh bastion
Last login: Thu Apr 14 00:23:39 2022 from 1.236.26.25

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

<https://aws.amazon.com/amazon-linux-2/>
[ec2-user@ip-172-31-1-74 ~]$

 

잘 접속된다. 참고로 config 파일은 600이거나 400이어야 한다.

 

[vagrant@controller ~]$ ls -l .ssh/config
-rw-------. 1 vagrant vagrant 88 Apr 14 00:23 .ssh/config

이제 바스티온 호스트를 점프해서 접속해보자

[vagrant@controller ~]$ ssh -J bastion ec2-user@172.31.64.149
The authenticity of host '172.31.64.149 ()' can't be established.
ECDSA key fingerprint is SHA256:IUyJwdYuQrpVDgCs3IIHtimPJEKW9XpKYZou84ic3m8.
ECDSA key fingerprint is MD5:d1:8c:f2:25:a2:bf:2c:ce:a7:2c:79:31:a9:59:83:e3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.31.64.149' (ECDSA) to the list of known hosts.

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

<https://aws.amazon.com/amazon-linux-2/>
[ec2-user@ip-172-31-64-149 ~]$

https://www.jeffgeerling.com/blog/2022/using-ansible-playbook-ssh-bastion-jump-host → 사이트 참조

 

Using an Ansible playbook with an SSH bastion / jump host | Jeff Geerling

Using an Ansible playbook with an SSH bastion / jump host

www.jeffgeerling.com

 

.ssh/config 파일을 수정하여 ssh node1 으로 바로 접속이 가능하게 할 수 있다.

 

[vagrant@controller ~]$ cat ~/.ssh/config
Host bastion
        HostName 3.36.118.255
        User ec2-user
        Port 22
        IdentityFile ~/.ssh/id_rsa

Host node1
        HostName 172.31.64.149
        User ec2-user
        Port 22
        IdentityFile ~/.ssh/id_rsa
        ProxyJump bastion
[vagrant@controller ~]$ ssh node1
Last login: Thu Apr 14 00:27:50 2022 from ip-172-31-1-74.ap-northeast-2.compute.internal

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

<https://aws.amazon.com/amazon-linux-2/>
[ec2-user@ip-172-31-64-149 ~]$

그러나 위의 방식을 사용할 때 ansible 의 ad-hoc 명령이 실행안되는 경우가 있다.

[proxy]
bastion.example.com

[nodes]
private-server-1.example.com
private-server-2.example.com
private-server-3.example.com

[nodes:vars]
ansible_ssh_common_args='-o ProxyCommand="ssh -p 2222 -W %h:%p -q username@bastion.example.com"'

그럴 경우 인벤토리 파일에 위처럼 작성한다.

 

%h 는 호스트, %p 는 포트를 나타내고 nodes 그룹에는 접속하고자 하는 private 한 인스턴스들의 주소를 써주고, nodes:vars 의 매개변수 부분에 ProxyCommand 를 추가해주는 것이다.

 

그러면 ProxyCommand 가 프록시서버로 ssh 연결을 해준다. 그리고 프록시 서버에서 ssh 연결을 통해 ansible 명령이 nodes 그룹으로 도착한다.

[vagrant@controller ~]$ cat inventory.ini
[ec2]
172.31.64.149

[ec2:vars]
ansible_ssh_common_args='-o ProxyCommand="ssh -p 22 -W %h:%p -q ec2-user@3.36.118.255"'

172.31.64.149 가 접속하려는 대상이고, ProxyCommand 에 프록시 서버의 주소. 즉, bastion host 주소를 적어준다.

[vagrant@controller ~]$ ansible ec2 -i inventory.ini -m command -a id -u ec2-user
[WARNING]: Platform linux on host 172.31.64.149 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See <https://docs.ansible.com/ansible/2.9/referen>
ce_appendices/interpreter_discovery.html for more information.
172.31.64.149 | CHANGED | rc=0 >>
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal)

ansible ec2 -i inventory.ini -m command -a id -u ec2-user 을 입력하면 접속이 된다.

 

-u ec2-user 을 붙인 이유는 .ansible.cfg 파일에 remote_user 이름을 vagrant 로 기본 설정을 했기 때문

[vagrant@controller ~]$ cat .ansible.cfg
[defaults]
inventory=./inventory.ini
remote_user=vagrant
host_key_checking=False

[privilege_connection]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[vagrant@controller ~]$

실제로 remote_user=vagrant 로 설정되어 있어서 -u 로 유저명을 적어주지 않을 경우 vagrant 로 접속하게 된다.

[vagrant@controller ~]$ cp inventory.ini aws-ec2.ini
[vagrant@controller ~]$ cat aws-ec2.ini
[ec2]
172.31.64.149

[ec2:vars]
ansible_user=ec2-user
ansible_ssh_common_args='-o ProxyCommand="ssh -p 22 -W %h:%p -q ec2-user@3.36.118.255"'
[vagrant@controller ~]$ ansible ec2 -i aws-ec2.ini.ini -m command -a id
[WARNING]: Unable to parse /home/vagrant/aws-ec2.ini.ini as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: ec2

inventory.ini 와 aws-ec2.ini 파일을 분리시켰고 ec2:vars 에서 ansible_user=ec2-user 변수를 추가하여 -u ec2-user 를 입력하지 않아도 ec2-user 로 접속할 수 있게 했다.

728x90