ssh key를 여러개를 생성해서 사용해야 하는 경우, 어떠한 public key에 어떤 private key를 사용할 것인지 ssh에 알려주어야 합니다.
Github에서 사용하는 key와 원격 서버에 사용할 key를 구분해서 사용하고 싶어 새로 ssh key를 생성하고 원격 서버에 public key를 등록해주었습니다. password를 따로 설정 안 해준 key였는데도 원격 서버에 접속할 때마다 로그인 prompt가 나타나면서 비밀번호를 요구하길래 무언가를 설정해주어야 하나보다 라는 생각이 들게되었습니다.
결론적으로, 단순히 public key를 알려주는 것만으로는 컴퓨터가 해당 public key와 pair되는 private key를 알지 못하고, 그렇기 때문에 어떤 private key를 사용하면 되는 지 알려주어야 합니다.
등록하는 방법은 ssh의 option `-i` 를 이용해주면 됩니다. 해당 옵션에 대한 설명은 아래 처럼 나와있습니다.
-i _identity_file_
Selects a file from which the identity (private key) for
public key authentication is read. You can also specify a
public key file to use the corresponding private key that
is loaded in _ssh-agent_(1) when the private key file is not
present locally. The default is _~/.ssh/id_rsa_,
_~/.ssh/id_ecdsa_, _~/.ssh/id_ecdsa_sk_, _~/.ssh/id_ed25519_ and
_~/.ssh/id_ed25519_sk_. Identity files may also be
specified on a per-host basis in the configuration file.
It is possible to have multiple -i options (and multiple
identities specified in configuration files). If no
certificates have been explicitly specified by the
CertificateFile directive, ssh will also try to load
certificate information from the filename obtained by
appending _-cert.pub_ to identity filenames.
읽어 보면 "Selects a file from which the identity (private key) for public key authentication is read." 라고 해서 이러한 상황에 필요한 기능을 해준다는 것을 알 수 있습니다.
아래처럼 어떤 private key를 어디에서 사용할 것인지 알려줍니다.
ssh -i ./.ssh/{private_key_name} {user}@{ip-address}
이렇게 설정을 해주니 pass key 입력 없이 바로 서버에 로그인이 가능해졌습니다 ✨
반응형