Windows 환경에서 대부분의 인증은 공통 도메인을 공유하는 시스템에서 잘 작동하는 사용자 이름-암호 쌍으로 수행됩니다. 온-프레미스 및 클라우드 호스팅 시스템과 같은 도메인 간에 작업하는 경우 이러한 유형의 인증은 무차별 암호 대입에 취약해집니다.
이에 비해 Linux 환경에서는 일반적으로 퍼블릭/프라이빗 키 쌍을 사용하여 추측 가능한 암호를 사용할 필요가 없는 인증을 구동합니다. OpenSSH에는 특히 키 기반 인증을 지원하는 데 도움이 되는 도구가 포함되어 있습니다.
- ssh-keygen for generating secure keys.
- ssh-agent and ssh-add for securely storing private keys.
- scp and sftp to securely copy public key files during initial use of a server.
이 문서에서는 Windows에서 이러한 도구를 사용하여 SSH(Secure Shell)에서 키 기반 인증 사용을 시작하는 방법에 대한 개요를 제공합니다. SSH 키 관리에 익숙하지 않은 경우 SSH(Secure Shell) 사용하여 대화형 및 자동화된 액세스 관리의보안이라는 NIST 문서 IR 7966검토하는 것이 좋습니다.
Key pairs
키 쌍은 특정 인증 프로토콜에 사용되는 공개 키 및 프라이빗 키 파일을 나타냅니다.
SSH public key authentication uses asymmetric cryptographic algorithms to generate two key files—one private and the other public. 각 프라이빗 키 파일은 암호와 동일하며 모든 상황에서 보호 상태를 유지해야 합니다. 누군가가 사용자의 프라이빗 키를 획득하면 해당 사용자가 액세스할 수 있는 모든 SSH 서버에 사용자로 로그인할 수 있습니다. 공개 키는 SSH 서버에 배치되며 프라이빗 키를 손상시키지 않고 공유할 수 있습니다.
SSH 서버와 클라이언트는 키 기반 인증을 사용하여 제공된 사용자 이름의 공개 키를 프라이빗 키와 비교할 수 있습니다. 클라이언트 쪽 프라이빗 키에 대해 서버 쪽 공개 키의 유효성을 검사할 수 없으면 인증이 실패합니다.
다단계 인증은 키 쌍이 생성될 때 암호를 입력하여 키 쌍으로 구현할 수 있습니다. 자세한 내용은 사용자 키 생성참조하세요. 인증 중에 암호를 입력하라는 메시지가 사용자에게 표시됩니다. 프라이빗 키의 존재와 결합된 암호는 SSH 클라이언트에서 사용자를 인증하는 데 사용됩니다.
Important
키 기반 인증을 통해 열린 원격 세션에는 연결된 사용자 자격 증명이 없습니다. 따라서 세션은 사용자로서 아웃바운드 인증을 수행할 수 없습니다. 이 동작은 의도된 것입니다.
호스트 키 생성
공개 키에는 Windows에서 관리자 및 시스템 사용자에 대한 액세스만 허용하는 것과 같은 특정 ACL(액세스 제어 목록) 요구 사항이 있습니다.
sshd
서비스를 처음 사용할 때 호스트의 키 쌍이 자동으로 생성됩니다.
Important
이 문서의 명령을 실행하려면 OpenSSH Server를 설치해야 합니다. 자세한 내용을 보려면 Windows용 OpenSSH 시작하기를 참조하세요.
기본적으로 수동으로 sshd
시작해야 합니다. 서버를 다시 시작할 때마다 자동으로 시작되도록 구성하려면 서버의 관리자 권한 PowerShell 프롬프트에서 다음 명령을 실행합니다.
# Set the sshd service to be started automatically.
Get-Service -Name sshd | Set-Service -StartupType Automatic
# Start the sshd service.
Start-Service sshd
Because there's no user associated with the sshd
service, the host keys are stored under C:\ProgramData\ssh.
사용자 키 생성
키 기반 인증을 사용하려면 먼저 클라이언트에 대해 공개/프라이빗 키 쌍을 생성해야 합니다.
ssh-keygen.exe
사용하여 키 파일을 생성할 수 있으며 다음 키 생성 알고리즘을 지정할 수 있습니다.
- DSA(디지털 서명 알고리즘)
- Rivest–Shamir–Adleman (RSA)
- ECDSA(타원 곡선 디지털 서명 알고리즘)
- Ed25519
알고리즘을 지정하지 않으면 Ed25519가 사용됩니다. 이 예제에서는 ECDSA와 같은 강력한 알고리즘과 키 길이를 사용해야 합니다.
ECDSA 알고리즘을 사용하여 키 파일을 생성하려면 클라이언트의 PowerShell 또는 명령 프롬프트 창에서 다음 명령을 실행합니다.
ssh-keygen -t ecdsa
명령의 출력은 username
사용자 이름으로 대체된다는 점을 제외하고 다음 줄과 같이 표시됩니다.
Generating public/private ecdsa key pair.
Enter file in which to save the key (C:\Users\username/.ssh/id_ecdsa):
At the prompt, you can select Enter to accept the default file path, or you can specify a path or file name for your generated keys.
다음으로 암호를 사용하여 프라이빗 키 파일을 암호화하라는 메시지가 표시됩니다. 일반적으로 암호는 키 파일과 함께 작동하여 2단계 인증을 제공하기 때문에 빈 암호를 사용하지 않는 것이 좋습니다. 그러나 이 예제에서는 암호를 비워 둘 수 있습니다.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\username/.ssh/id_ecdsa.
Your public key has been saved in C:\Users\username/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:OIzc1yE7joL2Bzy8!gS0j8eGK7bYaH1FmF3sDuMeSj8 username@LOCAL-HOSTNAME
The key's randomart image is:
+--[ECDSA 256]--+
| . |
| o |
| . + + . |
| o B * = . |
| o= B S . |
| .=B O o |
| + =+% o |
| *oo.O.E |
|+.o+=o. . |
+----[SHA256]-----+
이제 지정된 위치에 퍼블릭/프라이빗 ECDSA 키 쌍이 있습니다. .pub 파일은 공개 키이며 확장 프로그램이 없는 파일은 프라이빗 키입니다.
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 6/3/2021 2:55 PM 464 id_ecdsa
-a---- 6/3/2021 2:55 PM 103 id_ecdsa.pub
프라이빗 키 파일은 암호와 동일하며 암호를 보호하는 것과 동일한 방식으로 보호되어야 합니다.
ssh-agent
사용하여 Windows 계정과 연결된 Windows 보안 컨텍스트 내에 프라이빗 키를 안전하게 저장할 수 있습니다. 컴퓨터를 다시 시작할 때마다 자동으로 시작되도록 ssh-agent
서비스를 구성하고 ssh-add
사용하여 프라이빗 키를 저장하려면 서버의 관리자 권한 PowerShell 프롬프트에서 다음 명령을 실행합니다.
# By default, the ssh-agent service is disabled. Configure it to start automatically.
# Run the following command as an administrator.
Get-Service ssh-agent | Set-Service -StartupType Automatic
# Start the service.
Start-Service ssh-agent
# The following command should return a status of Running.
Get-Service ssh-agent
# Load your key files into ssh-agent.
ssh-add $env:USERPROFILE\.ssh\id_ecdsa
클라이언트의 ssh-agent
서비스에 키를 추가하면 ssh-agent
서비스가 자동으로 로컬 프라이빗 키를 검색하고 SSH 클라이언트에 전달합니다.
Important
We recommend that you back up your private key to a secure location and then delete it from the local system after you add it to the ssh-agent
service. 이 예제의 ECDSA와 같은 강력한 알고리즘을 사용하는 경우 에이전트에서 프라이빗 키를 검색할 수 없습니다. 프라이빗 키에 대한 액세스 권한이 끊어지면 새 키 쌍을 만들고 상호 작용하는 모든 시스템에서 공개 키를 업데이트해야 합니다.
공개 키 배포
To use the user key that you created previously, you need to place the contents of your public key (\.ssh\id_ecdsa.pub) on the server into a text file. 파일의 이름과 위치는 사용자 계정이 로컬 관리자 그룹의 구성원인지 표준 사용자 계정인지에 따라 달라집니다. 다음 섹션에서는 표준 및 관리 사용자 모두에 대해 설명합니다.
Standard user
You need to place the contents of your public key (\.ssh\id_ecdsa.pub) on the server into a text file called authorized_keys
in C:\Users\username\.ssh\. OpenSSH scp
보안 파일 전송 유틸리티를 사용하거나 PowerShell을 사용하여 파일에 키를 작성하여 공개 키를 복사할 수 있습니다.
다음 코드를 사용하여 공개 키를 서버에 복사할 수 있습니다. 마지막 줄에서 username
를 사용자 이름으로 변경하십시오. 처음에는 서버의 사용자 계정에 대한 암호를 입력하라는 메시지가 표시됩니다.
# Get the public key file generated previously on your client.
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub
# Generate the PowerShell command to run remotely that copies the public key file generated previously on your client to the authorized_keys file on your server.
$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"
# Connect to your server and run the PowerShell command by using the $remotePowerShell variable.
ssh username@domain1@contoso.com $remotePowershell
Administrative user
You need to place the contents of your public key (\.ssh\id_ecdsa.pub) on the server into a text file called administrators_authorized_keys
in C:\ProgramData\ssh\. OpenSSH scp
보안 파일 전송 유틸리티를 사용하거나 PowerShell을 사용하여 파일에 키를 작성하여 공개 키를 복사할 수 있습니다. 이 파일의 ACL은 관리자 및 시스템 사용자에 대한 액세스만 허용하도록 구성해야 합니다.
다음 코드를 사용하여 공개 키를 서버에 복사하고 ACL을 구성할 수 있습니다. 마지막 줄에서 username
를 사용자 이름으로 변경하십시오. 처음에는 서버의 사용자 계정에 대한 암호를 입력하라는 메시지가 표시됩니다.
Note
이 예제에서는 administrators_authorized_keys
파일을 만드는 단계를 보여 줍니다. 이 파일은 관리자 계정에만 적용됩니다. 사용자의 프로필 위치 내에서 사용자별 파일 대신 사용해야 합니다.
# Get the public key file generated previously on your client.
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub
# Generate the PowerShell command to run remotely that copies the public key file generated previously on your client to the authorized_keys file on your server.
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '''$authorizedKey''';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""
# Connect to your server and run the PowerShell command by using the $remotePowerShell variable.
ssh username@domain1@contoso.com $remotePowershell
영어 이외의 지역화된 운영 체제 버전의 경우 그에 따라 그룹 이름을 반영하도록 스크립트를 수정해야 합니다. 그룹 이름에 권한을 부여할 때 발생할 수 있는 오류를 방지하기 위해 그룹 이름 대신 SID(보안 식별자)를 사용할 수 있습니다.
Get-LocalGroup | Select-Object Name, SID
실행하여 SID를 검색할 수 있습니다. 그룹 이름 대신 SID를 사용하는 경우 앞에 별표(*)가 와야 합니다. In the following example, the Administrators group uses the SID S-1-5-32-544
:
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '''$authorizedKey''';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""*S-1-5-32-544:F"" /grant ""SYSTEM:F"""
이러한 단계에서는 Windows에서 키 기반 OpenSSH 인증을 사용하기 위해 필요한 구성을 완료합니다.
이러한 PowerShell 명령을 실행한 후 프라이빗 키가 있는 모든 클라이언트에서 sshd
호스트에 연결할 수 있습니다.