[Linux]등록된 Host 및 IP를 제외하고 root의 ssh 로그인을 제한하는 방법

Linux 서버가 운영상태일 경우에는 가급적 root 사용자가 ssh 접속하는 것을 제한하는 것이 좋습니다. 혹시 모를 잘못된 명령어 입력으로 서버에 심각한 피해를 줄 수 있기 때문입니다. 그런데 서버를 관리하다보면 불가피하게 root 사용자로 ssh 접속을 할 때가 있습니다. 그래서 root 사용자의 ssh 접속을 최소화하기 위해 제한하는 방법을 알아보겠습니다.
서버 관리자만 root 사용자의 비밀번호를 보유하거나, 키 기반 인증을 사용하거나, root 사용자의 비밀번호를 주기적으로 변경하는 등 조치로 root 사용자의 ssh 접속을 제한할 수도 있으나, sshd 환경설정 파일(/etc/ssh/sshd_config)의 PermitRootLogin 지시자와 Match 지시자를 사용해서 등록된 Host 및 IP에서만 root 사용자의 ssh 접속을 허용할 수 있습니다.

PermitRootLogin 지시자

Specifies whether root can log in using ssh(1). The argument must be yes, prohibit-password, forced-commands-only, or no. The default is prohibit-password. If this option is set to prohibit-password (or its deprecated alias, without-password), password and keyboard-interactive authentication are disabled for root. If this option is set to forced-commands-only, root login with public key authentication will be allowed, but only if the command option has been specified (which may be useful for taking remote backups even if root login is normally not allowed). All other authentication methods are disabled for root. If this option is set to no, root is not allowed to log in.


root 사용자가 ssh를 사용하여 로그인할 수 있는지 여부를 지정합니다. 인수는 “yes”, “prohibit-password”, “forced-commands-only” 또는 “no”여야 합니다. 기본값은 “prohibit-password”입니다. 이 옵션이 “prohibit-password”(또는 더 이상 사용되지 않는 별칭, “without-password”)로 설정되면 root에 대한 비밀번호 및 키보드 대화형 인증이 비활성화됩니다. 이 옵션이 “forced-commands-only”로 설정되면 공개 키 인증을 사용한 root 로그인이 허용되지만 명령 옵션이 지정된 경우에만 허용됩니다(root 로그인이 일반적으로 허용되지 않는 경우에도 원격 백업을 수행하는 데 유용할 수 있음). 다른 모든 인증 방법은 root에 대해 비활성화됩니다. 이 옵션을 “no”로 설정하면 루트 로그인이 허용되지 않습니다.

 
Match 지시자

Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, until either another Match line or the end of the file. If a keyword appears in multiple Match blocks that are satisfied, only the first instance of the keyword is applied.
The arguments to Match are one or more criteria-pattern pairs or the single token All which matches all criteria. The available criteria are User, Group, Host, LocalAddress, LocalPort, RDomain, and Address (with RDomain representing the rdomain(4) on which the connection was received).
The match patterns may consist of single entries or comma-separated lists and may use the wildcard and negation operators described in the PATTERNS section of ssh_config(5).
The patterns in an Address criteria may additionally contain addresses to match in CIDR address/masklen format, such as 192.0.2.0/24 or 2001:db8::/32. Note that the mask length provided must be consistent with the address – it is an error to specify a mask length that is too long for the address or one with bits set in this host portion of the address. For example, 192.0.2.0/33 and 192.0.2.0/8, respectively.
Only a subset of keywords may be used on the lines following a Match keyword. Available keywords are AcceptEnv, AllowAgentForwarding, AllowGroups, AllowStreamLocalForwarding, AllowTcpForwarding, AllowUsers, AuthenticationMethods, AuthorizedKeysCommand, AuthorizedKeysCommandUser, AuthorizedKeysFile, AuthorizedPrincipalsCommand, AuthorizedPrincipalsCommandUser, AuthorizedPrincipalsFile, Banner, ChrootDirectory, ClientAliveCountMax, ClientAliveInterval, DenyGroups, DenyUsers, ForceCommand, GatewayPorts, GSSAPIAuthentication, HostbasedAcceptedKeyTypes, HostbasedAuthentication, HostbasedUsesNameFromPacketOnly, IPQoS, KbdInteractiveAuthentication, KerberosAuthentication, KerberosUseKuserok, LogLevel, MaxAuthTries, MaxSessions, PasswordAuthentication, PermitEmptyPasswords, PermitListen, PermitOpen, PermitRootLogin, PermitTTY, PermitTunnel, PermitUserRC, PubkeyAcceptedKeyTypes, PubkeyAuthentication, RekeyLimit, RevokedKeys, RDomain, SetEnv, StreamLocalBindMask, StreamLocalBindUnlink, TrustedUserCAKeys, X11DisplayOffset, X11MaxDisplays, X11Forwarding and X11UseLocalHost.


조건부 블록을 도입합니다. Match 줄의 모든 기준이 충족되면 다음 줄의 키워드는 다른 Match 줄이나 파일 끝까지 구성 파일의 global section에 설정된 키워드를 재정의합니다. 충족된 여러 Match Block에 키워드가 나타나는 경우 해당 키워드의 첫 번째 인스턴스만 적용됩니다.
Match에 대한 인수는 하나 이상의 기준-패턴 쌍 또는 모든 기준과 일치하는 단일 토큰 All입니다. 사용 가능한 기준은 User, Group, Host, LocalAddress, LocalPort, RDomain 및 Address(RDomain은 연결이 수신된 rdomain(4)를 나타냄)입니다.
Match Pattern은 단일 항목 또는 쉼표로 구분된 목록으로 구성될 수 있으며 ssh_config(5)의 PATTERNS 섹션에 설명된 와일드카드 및 부정 연산자를 사용할 수 있습니다.
주소 기준의 패턴에는 192.0.2.0/24 또는 2001:db8::/32와 같은 CIDR 주소/masklen 형식으로 일치시킬 주소가 추가로 포함될 수 있습니다. 제공된 마스크 길이는 주소와 일치해야 합니다. 주소에 비해 너무 긴 마스크 길이를 지정하거나 주소의 이 호스트 부분에 비트가 설정된 마스크 길이를 지정하는 것은 오류입니다. 예를 들어 각각 192.0.2.0/33 및 192.0.2.0/8입니다.
Match 키워드 다음 줄에는 키워드의 하위 집합만 사용할 수 있습니다. 사용 가능한 키워드는 AcceptEnv, AllowAgentForwarding, AllowGroups, AllowStreamLocalForwarding, AllowTcpForwarding, AllowUsers, AuthenticationMethods, AuthorizedKeysCommand, AuthorizedKeysCommandUser, AuthorizedKeysFile, AuthorizedPrincipalsCommand, AuthorizedPrincipalsCommandUser, AuthorizedPrincipalsFile, Banner, ChrootDirectory, ClientAliveCountMax, ClientAliveInterval, DenyGroups, DenyUsers, ForceCommand, GatewayPorts, GSSAPIAuthentication, HostbasedAcceptedKeyTypes, HostbasedAuthentication , HostbasedUsesNameFromPacketOnly, IPQoS, KbdInteractiveAuthentication, KerberosAuthentication, KerberosUseKuserok, LogLevel, MaxAuthTries, MaxSessions, PasswordAuthentication, PermitEmptyPasswords, PermitListen, PermitOpen, PermitRootLogin, PermitTTY, PermitTunnel, PermitUserRC, PubkeyAcceptedKeyTypes, PubkeyAuthentication, RekeyLimit, RevokedKeys , RDomain, SetEnv, StreamLocalBindMask, StreamLocalBindUnlink, TrustedUserCAKeys , X11DisplayOffset, X11MaxDisplays, X11Forwarding 및 X11UseLocalHost.

 
root 사용자의 ssh 접속을 지정된 호스트 또는 IP address에서만 키 인증 방식으로 허용하고자 할 때는 아래와 같이 설정하면 됩니다. “,”로 분리하여 여러 개의 호스트 및 IP address를 지정할 수 있습니다.

# vi /etc/ssh/sshd_config
PermitRootLogin no #At the end Match Host hostname[, hostname] PermitRootLogin prohibit-password Match Address ip_address[, ip_address] PermitRootLogin prohibit-password
# systemctl restart sshd

함께 읽어보면 좋은 Contents

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다