본문 바로가기
Linux/TIP

sftp로 upload한 파일, 디렉토리의 permission 오류(?!) 해결방법

by aegypius 2020. 12. 17.
728x90
반응형

  CentOS 7에서 8로 교체한 후에 두 번째로 겪는 기이한(?) 현상이다. sftp나 ftp(s)로 파일이나 디렉토리를 업로드 하거나 혹은 디렉토리를 생성하는 경우 보통 파일은 755, 디렉토리는 644의 퍼미션을 갖는다. 계정의 기본 퍼미션 마스크가 022이기 때문에 파일은 (777-022)755, 디렉토리는 (666-022)644를 갖는 것인데, 유독 sftp로 업로드한 파일만 700, 디렉토리는 600의 값으로 설정된다. (아직 ftp(s)는 테스트를 못해봤다.)

  계정의 umask값은 분명 022인데 이런일이 생기는 이유가 뭘까? CentOS 7의 버전정보는 아래와 같다.

[root@localhost ssh]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@localhost ssh]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
[root@localhost ssh]# umask
0022
[root@localhost ssh]#

  그리고 문제의 CentOS 8의 버전은 아래와 같다.

[root@vultrguest ssh]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@vultrguest ssh]# ssh -V
OpenSSH_8.0p1, OpenSSL 1.1.1c FIPS  28 May 2019
[root@vultrguest ssh]# umask
0022
[root@vultrguest ssh]#

  구글링을 통해서 해결했다. (qastack.kr/server/70876/how-to-put-desired-umask-with-sftp)

Match LocalPort 포트번호
        ForceCommand internal-sftp -u 022

Match user pius
         ChrootDirectory %h
         ForceCommand internal-sftp

Match User phreun
         ChrootDirectory %h
         ForceCommand internal-sftp

  하지만 qastack.kr에서의 sftp(ssh)설정과 내가 운영중인 서버의 설정은 분명히 다르다. 그리 심플한 것은 아니지만 나조차도 조금 헷갈리기 때문에 간단하게 정리해본다.

 

(1) ssh와 sftp의 포트를 분리한 경우

  위와 같이 sftp 전용 포트를 정의한 부분에서만 -u (0)022를 추가하면 된다. sshd_config 의 내용은 아래와 같다. 158번째 줄과 같이 수정하면 된다.

    142 # override default of no subsystems
    143 Subsystem       sftp    /usr/libexec/openssh/sftp-server
    144 #Subsystem      sftp    internal-sftp
    145
    146 # Example of overriding settings on a per-user basis
    147 #Match User anoncvs
    148 #       X11Forwarding no
    149 #       AllowTcpForwarding no
    150 #       PermitTTY no
    151 #       ForceCommand cvs server
    152
    153 #Match user pius
    154 #         ChrootDirectory %h
    155 #        ForceCommand internal-sftp
    156
    157 Match LocalPort 12345
    158         ForceCommand internal-sftp -u 022
    159
    160 Match User phreun
    161          ChrootDirectory %h
    162          ForceCommand internal-sftp
    163

 

(2) ssh와 sftp가 동일한 포트를 사용중이라면

"Subsystem sftp internal-sftp -u 022"로 충분하다. 아래의 3번째 줄이다.

# override default of no subsystems
#Subsystem       sftp    /usr/libexec/openssh/sftp-server
Subsystem      sftp    internal-sftp -u 022

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

#Match user pius
#         ChrootDirectory %h
#        ForceCommand internal-sftp

Match User phreun
         ChrootDirectory %h
         ForceCommand internal-sftp

  테스트하면서 몇 번의 오작동(?)이 있었는데...위의 예에서 2번째 줄과 3번째 줄 둘 중 어느것을 사용하더라도 그 차이는 없었던 것으로 기억한다.

  이와 같이 수정하였음에도 업로드한 파일이나 디렉토리의 퍼미션이 변함없이 700 / 600 일 수도 있다. 

  ssh의 사용권한은 있으나(AllowUser에 포함되었거나 DenyUsers에 제외된) 위의 예에서와 같이 Match User로 다시 설정하지 않은 계정들(ssh와 sftp 모두 사용 가능한 계정들이다)은 sftp로 업로드한 파일이나 디렉토리가 정상적인 퍼미션을 갖지만, 그렇지 않은 계정(위의 예에서는 phreun이다)은 여전히 700 / 600의 퍼미션을 보여줄 수 있다. 이때는 아래와 같이 해당 계정마다 별도로 설정해줘야 한다. 아래 예문의 가장 마지막 줄이다.

 

# override default of no subsystems
#Subsystem       sftp    /usr/libexec/openssh/sftp-server
Subsystem      sftp    internal-sftp -u 022

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

#Match user pius
#         ChrootDirectory %h
#        ForceCommand internal-sftp -u 022

Match User phreun
         ChrootDirectory %h
         ForceCommand internal-sftp -u 022

 


728x90
반응형

댓글