관리 메뉴

공부공부 공부공부내용

SSH Key 충돌 문제 발생오류 본문

클라우드 구성 및 관리/오류& 궁금&키워드 이해

SSH Key 충돌 문제 발생오류

wkdth04 2020. 7. 1. 11:37

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

Someone could be eavesdropping on you right now (man-in-the-middle attack)!

It is also possible that a host key has just been changed.

The fingerprint for the RSA key sent by the remote host is

7a:1a:88:.......(생략)

Please contact your system administrator.

Add correct host key in /root/.ssh/known_hosts to get rid of this message.

Offending RSA key in /root/.ssh/known_hosts:12

RSA host key for 192.168.122.2 has changed and you have requested strict checking.

Host key verification failed. 

 

 


해석하면, 대략 내용은 원격 호스트 식별 정보가 변경되었다.

기존에 등록되어있던 RSA key가 다르다.

누군가 기존 접속 정보를 바꾸어 정보를 가로채는 man-in-the-middle attack 에 발생에 유의해라.

해당 경고를 제거하려면 /root/.ssh/known_hosts 올바른 Host Key를 추가해라.



 


student@student:~$ ssh root@192.168.122.11
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:8kNdSWEIzXswD5Nq2gbR1+LUR0ArgYx9siGzaCtTHNo.
Please contact your system administrator.
Add correct host key in /home/student/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/student/.ssh/known_hosts:7
  remove with:
  ssh-keygen -f "/home/student/.ssh/known_hosts" -R "192.168.122.11"
ECDSA host key for 192.168.122.11 has changed and you have requested strict checking.
Host key verification failed.


rm -rf ~/.ssh/known_hosts  로 진행할수도있고,

나와있는 것 처럼

 

 ssh-keygen -f "/home/student/.ssh/known_hosts" -R "192.168.122.11"  기존 생성된 호스트 내용을 삭제해도 된다.



######################
## openssh 명령어들 ##
######################

# ls /usr/local/openssh/bin
scp  sftp  slogin  ssh  ssh-add  ssh-agent  ssh-keygen  ssh-keyscan

# ls /usr/local/openssh/sbin
sshd
# ls /usr/local/openssh/libexec/
sftp-server  ssh-keysign  ssh-pkcs11-helper

==============================================================
OPENSSH-LAB> ssh 로 접속을 했더니 Warning 메세지가 나온다면 ?

!!! 작업은 클라이언트에서 한다 !!!

공개키가 저장되는 파일 : $HOME/.ssh/known_hosts

- 원인 -
1. 서버의 공개키가 변경이된 것이다.
2. 공격(man-in-the-middle attack)일 가능성이 있다.

- 해결방안 -
1. $HOME/.ssh/known_hosts 파일에 해당 호스트에 대한 키를 삭제한다.
2. $HOME/.ssh/known_hosts 파일을 삭제한다.

# ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
00:f3:19:ee:36:f3:1f:fe:5c:ec:23:e0:c3:ba:3f:26.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending RSA key in /root/.ssh/known_hosts:1
RSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.

# cd .ssh
# rm -f known_hosts  <-- 공개키가 담겨있는 파일을 삭제하고 접속한다.
# ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is e9:1f:5f:30:15:6d:e4:b0:1e:32:4d:ec:7c:79:ce:8c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.


ex2)
- 서버의 공개키가 클라이언트의 저장된 공개키가 동일하므로 
  로그인이 나온다.
# ssh localhost
root@localhost's password:   ^C 취소

- 서버에서 키를 모두 삭제
# rm -f /etc/ssh/ssh_host_*
- sshd 서버를 재시작하면 키를 모두 생성한다. (이전키와 달라진다.)
# /etc/init.d/sshd restart

- 클라이언에서 서버로 접속
- 서버의 공개키가 클라이언트의 저장된 공개키와 다르므로 
  워닝창이 나오는 것이다.
# ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
52:fe:d6:4b:af:46:e4:0f:f2:60:c5:ec:1b:42:5a:b9.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:1
ECDSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.

- 서버/클라이언트의 공개키가  서로 다르다는 것을 알 수 있다.

- 클라이어트에 저장된 공개키
# cat ~/.ssh/known_hosts 
localhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPk2rT0ocZ3clAnhUmfY0FU6/Fa+llyd0s5Lx+ZCRgJ6uNULkd5ng54EodlIeIaiNGlvmDCk3YEqkZaS+0R+7m4=

- 서버에 저장된 공개키
# cat /etc/ssh/ssh_host_ed25519_key.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMLfmd7F5zIbe76h16F3DsoyAE9VyX9i9BrVKXTxnZrd root@localhost.localdomain
==============================================================



출처: https://hoyoung2.tistory.com/106 [HoyoungE]
ssh 활용하기

호스트에서 SSH 서버로 접속해봅시다.

SSH 클라이언트 프로그램인 ssh는 우분투 리눅스에 기본적으로 설치되어 있습니다. ssh [사용자 계정]@[서버 주소] 또는 ssh -l [사용자 계정] [서버 주소] 형식으로 사용합니다.

접속 대상이 처음 접속하는 서버인 경우 접속을 진행할지 묻습니다. yes를 입력하고 접속하려는 사용자 계정의 패스워드 password를 입력합니다. 명령 프롬프트가 바뀐 것으로 SSH 서버인 server01에 접속한 사실을 알 수 있습니다.

shinjaehun@losttemple:~$ ssh administrator@192.168.122.201
The authenticity of host '192.168.122.201 (192.168.122.201)' can't be
established.
ECDSA key fingerprint is
d9:ca:30:2b:6c:80:7a:41:ac:07:7e:ec:f2:ec:af:57.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.201' (ECDSA) to the list of
known hosts.
administrator@192.168.122.201's password: ********
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic i686)
...
Last login: Mon Oct 27 21:58:26 2014
administrator@server01:~$

이렇게 하세요!

SSH 서버는 접속을 시도하는 호스트의 키 지문(key fingerprint)을 저장합니다. 기본적으로 ECDSA 방식의 암호화 기술을 사용하기 때문에 호스트 공개키는 /etc/ssh/ssh_host_ecdsa_key.pub에 저장됩니다. ssh-keygen 명령으로 호스트 키 지문을 확인할 수 있습니다.

administrator@server001:~$ ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub
256 d9:ca:30:2b:6c:80:7a:41:ac:07:7e:ec:f2:ec:af:57 root@server01 (ECDSA)

접속이 이루어진 다음 SSH 서버에서 lsof 명령을 실행해서 네트워크 연결 여부를 살펴봅시다. SSH 서버 192.168.122.201:22와 SSH 클라이언트 losttemple(192.168.122.1):41561(임의의 포트 번호) 사이에 연결(ESTABLISHED) 상태를 확인할 수 있습니다.

administrator@server01:~$ sudo lsof -i
COMMAND  PID         USER      FD  TYPE  DEVICE  SIZE/OFF  NODE NAME
sshd   2134          root      3u  IPv4  13548   0t0 TCP   192.168.122.201:ssh->losttemple:41561 (ESTABLISHED)
sshd   2182 administrator      3u  IPv4  13548   0t0 TCP   192.168.122.201:ssh->losttemple:41561 (ESTABLISHED)
sshd   2655          root      3u  IPv4  14499   0t0 TCP   *:ssh (LISTEN)
sshd   2655          root      4u  IPv6  14501   0t0 TCP   *:ssh (LISTEN)

텔넷과 마찬가지로 SSH 세션을 중단하려면 exit 명령을 내립니다. 프롬프트의 호스트 이름도 원래대로 돌아옵니다.

administrator@server01:~$ exit
logout
Connection to server01 closed.
shinjaehun@losttemple:~$

이렇게 하세요!

‘SSH 서버 설치하기’에서 살펴봤듯이 어떤 이유로 sshd가 기본 포트 번호인 22가 아닌 다른 포트에서 운영될 수 있습니다. 이럴 때는 접속하는 쪽에서 해당 포트로 접속 요청을 해야 하는데 -p 옵션을 사용합니다. sshd가 1022 포트에서 접속 요청을 기다리고 있으면 다음과 같이 ssh로 접속합니다.

shinjaehun@losttemple:~$ ssh administrator@192.168.122.201 -p 1022

ssh로 원격에서 수행할 명령을 바로 입력할 수도 있습니다. ssh [사용자 계정]@[서버 주소] [서버에서 수행할 명령] 형식으로 입력합니다. 다음 명령을 실행하면 server01에 접속하여 하드디스크 상태를 보여주는 df -h 명령을 실행하고 다시 원래 호스트로 돌아옵니다.

shinjaehun@losttemple:~$ ssh administrator@192.168.122.201 df -h
administrator@192.168.122.201's password: ********
Filesystem  Size   Used  Avail Use%   Mounted on
/dev/vda2   7.9G  1014M   6.5G   14%  /
none        4.0K      0   4.0K    0%  /sys/fs/cgroup
udev        492M          4.0K  492M  1%  /dev
tmpfs       101M          336K  100M  1%  /run
none        5.0M      0   5.0M    0%  /run/lock
none        501M      0   501M    0%  /run/shm
none        100M      0   100M    0%  /run/user

scp는 ssh를 통해 원격으로 파일을 복사하는 명령입니다. scp [원본 파일 경로] [복사할 파일 경로] 형태로 입력합니다. 원본이나 복사할 파일의 원격 시스템 경로는 [사용자 계정]@[서버 주소]:[파일 경로]가 되어야 합니다. 다음 명령을 실행하면 현재 디렉터리에 있는 ‘test_ssh_client’ 파일을 server01으로 전송하고 /tmp 디렉터리로 복사할 것입니다.

shinjaehun@losttemple:~$ touch test_ssh_client
shinjaehun@losttemple:~$ scp test_ssh_client administrator@192.168.122.201:/tmp/
administrator@server01's password: ********
test_ssh_client                            100%   17    0.0KB/s    00:00

파일이 제대로 복사되었는지 server01의 /tmp 디렉터리에서 확인합니다.

administrator@server01:~$ ls /tmp
test_ssh_client

이번에는 SSH 서버에 존재하는 파일을 현재 디렉터리로 복사해봅시다. 테스트를 위해 SSH 서버에 ‘test_ssh_server’ 파일을 생성합니다.

administrator@server01:~$ touch test_ssh_server

다음 명령은 server01의 홈 디렉터리에 있는 ‘test_ssh_server’ 파일을 현재 디렉터리(.)로 복사할 것입니다. 파일을 제대로 복사해왔는지 확인합니다.

shinjaehun@losttemple:~$ scp administrator@192.168.122.201:test_ssh_server .
administrator@server01's password: ********
test_ssh_server                             100%   13    0.0KB/s    00:00
shinjaehun@losttemple:~$ ls
test_ssh_server