Samba는 SMB를 사용하여 이기종 간의 자원 공유를 위해 만들어진 프로그램중 하나이다. 구현도 쉽고 사용이 편리해 널리 사용되고 있으나, 이미 사용하고 있는 사람조차도 그것이 Samba라는 것을 모를 수 있다. 일례로 윈도우의 폴더 공유가 바로 Samba이다.


Samba 서버를 CentOS에서 구축할 때, 메뉴얼의 절반 이상이 SELinux를 끄라고 한다. 그 이유는 아마도 SELinux에 대한 자료가 많이 없어서 정확한 설정이 어렵기 때문일 것이다. 나도 그동안 SELinux를 딱히 신경쓰지 않았지만 이번에 새롭게 CentOS 7 서버를 구축하면서 정석대로 해보기로 했다. 본래 눈앞의 쉬운 길도 돌아가는 성격인데다, 보안만큼은 타협하고 싶지 않았기 때문이다. 거기다 SELinux는 사실 NSA가 개발한 보안 커널을 리눅스에 이식한 것이라고 한다. 미 국가안보국의 보안 커널이 구동되는 서버라니 말만 들어도 멋지지 않은가.

자업자득이라고, 그 결과로 SMB 서버를 구축하는데 이틀이란 시간이 걸리게 되었다. SELinux의 개념이 생소해서 명령의 의미를 정확히 하기가 쉽지 않았다. 그리고 사실 완성은 하루만에 했지만 다음날 다시 안되서 삽질을 하루 더 했다는 슬픈 이야기. 거짓말 조금 보태서 항목 하나 바꾸고 서비스 재가동, 그리고 확인하기를 한 300번은 한 것같다. 그리고 남은건 데이터를 충실히 암호화하고 필요하지 않은 권한을 주지 않는 정말 믿음직한 서버.


쓸데 없는 설정은 빼고, SELinux를 끄지 않은 상태에서 정석대로 구축해

현재까지 실제로 매우 원활하게 사용하고 있는 설정입니다.


최신 버전의 업데이트가 설치된 CentOS 7에서 구축하였습니다. (2018.12.24 기준)





0. Samba가 설치되어있는지 확인한다. (설치되어있지 않다면 sudo yum install samba)

[glacier@glacier ~]$ rpm -qa samba
samba-4.8.3-4.el7.x86_64


1. SMB 서버에 접근할 2개의 계정을 만들고 단일 그룹에 소속시킨다.
[glacier@glacier ~]$ sudo groupadd testgroup
[glacier@glacier ~]$ sudo useradd testuser1
[glacier@glacier ~]$ sudo useradd testuser2
[glacier@glacier ~]$ sudo passwd testuser1
testuser1 사용자의 비밀 번호 변경 중
새  암호:
새  암호 재입력:
passwd: 모든 인증 토큰이 성공적으로 업데이트 되었습니다.
[glacier@glacier ~]$ sudo passwd testuser2
testuser1 사용자의 비밀 번호 변경 중
새  암호:
새  암호 재입력:
passwd: 모든 인증 토큰이 성공적으로 업데이트 되었습니다.
[glacier@glacier ~]$ sudo usermod testuser1 -a -G testgroup
[glacier@glacier ~]$ sudo usermod testuser2 -a -G testgroup
[glacier@glacier ~]$ sudo groupmems -g testgroup -l
testuser1  testuser2


2. 1에서 생성한 계정을 SMB에 등록한다.

[glacier@glacier home]$ sudo smbpasswd -a testuser1
New SMB password:
Retype new SMB password:
[glacier@glacier home]$ sudo smbpasswd -a testuser2
New SMB password:
Retype new SMB password:
Added user testuser2.


3. SMB로 공유할 폴더를 만들고 소유자와 권한을 조정한다.

[glacier@glacier ~]$ sudo mkdir /samba
[glacier@glacier ~]$ sudo mkdir /samba/test
[glacier@glacier ~]$ sudo chown testuser1:testgroup /samba/test -R
[glacier@glacier ~]$ sudo chmod 770 /samba/test -R
[glacier@glacier ~]$ ls -al /samba/test
drwxrwx---.   2 testuser1 testgroup  4096 12월 21 18:26 test


4. SMB설정을 입력한다.

[glacier@glacier ~]$ sudo vi /etc/samba/smb.conf

#[global] 항목은 samba 전역에 적용되는 설정,
#[test] 항목은 test 에만 적용되는 설정이다.

[global]
        workgroup = WORKGROUP
        security = user

        passdb backend = tdbsam

        netbios name = glacier
        server max protocol = SMB3_11
        #서버에서 구동할 최고 신형 버전 지정
        server min protocol = SMB2_10
        #서버에서 구동할 최고 구형 버전 지정(Windows 7해당)
        smb encrypt = required
        #데이터를 반드시 암호화
        kerberos encryption types = strong
        #아래는 수정하지 않으면 적용되는 기본값으로 입력할 필요 없음.
        #guest ok = no
        #disable netbios = no
        #server role = standalone
        #encrypt passwords = yes
        #client plaintext auth = no
        #unix charset = UTF=8
        #deadtime = 0
        #max connections = 0
        #hide dot files = yes

[test]
        writeable = yes
        path = /samba/test
        hosts allow = 192.168.23.0/255.255.255.0
        valid users = @testgroup
        write list = testuser1, testuser2
        force create mode = 0770
        create mask = 0770
        force directory mode = 0770
        directory mask = 0770
        force group = testgroup
        #아래는 수정하지 않으면 적용되는 기본값으로 입력할 필요 없음.
        #available = yes
        #browseable = yes

#null passwords 옵션은 구식으로 사용 금지.


5. SMB설정을 검증한다.

[glacier@glacier ~]$ testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[test]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions
^C


6. SMB를 재시작하고 부팅시 자동으로 시작하도록 설정한다.

[glacier@glacier ~]$ sudo systemctl restart smb
[glacier@glacier ~]$ sudo systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[glacier@glacier ~]$ systemctl status smb
● smb.service - Samba SMB Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
   Active: active (running) since 금 2018-12-21 18:56:12 KST; 28s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
Main PID: 66117 (smbd)
   Status: "smbd: ready to serve connections..."
   CGroup: /system.slice/smb.service
           ├─66117 /usr/sbin/smbd --foreground --no-process-group
           ├─66123 /usr/sbin/smbd --foreground --no-process-group
           ├─66124 /usr/sbin/smbd --foreground --no-process-group
           └─66125 /usr/sbin/smbd --foreground --no-process-group

12월 21 18:56:12 glacier systemd[1]: Starting Samba SMB Daemon...
12월 21 18:56:12 glacier smbd[66117]: [2018/12/21 18:56:12.253372,  0] ../lib/util/become_daemon.c:138(daemon_ready)
12월 21 18:56:12 glacier smbd[66117]:   daemon_ready: STATUS=daemon 'smbd' finished starting up and ready to serve c...ctions
12월 21 18:56:12 glacier systemd[1]: Started Samba SMB Daemon.
Hint: Some lines were ellipsized, use -l to show in full.


7. 방화벽을 Samba 서비스에 접근할 수 있도록 설정한다.

[glacier@glacier ~]$ sudo firewall-cmd --zone=public --add-service=samba --permanent
success
[glacier@glacier ~]$ sudo firewall-cmd --reload
success
[glacier@glacier ~]$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client samba
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:


8. Samba와 관련된 SE불린 항목을 조정한다.

[glacier@glacier ~]$ getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_load_libgfapi --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
tmpreaper_use_samba --> off
use_samba_home_dirs --> off
virt_use_samba --> off
[glacier@glacier ~]$ sudo setsebool -P samba_export_all_rw on


8-1. 항목별 설명을 보고 싶다면

[glacier@glacier ~]$ sudo semanage boolean -l | grep samba
samba_export_all_rw            (비활성  , 비활성 )  Allow samba to export all rw
samba_domain_controller        (비활성  , 비활성 )  Allow samba to domain controller
samba_portmapper               (비활성  , 비활성 )  Allow samba to portmapper
samba_export_all_ro            (비활성  , 비활성 )  Allow samba to export all ro
use_samba_home_dirs            (비활성  , 비활성 )  Allow use to samba home dirs
samba_create_home_dirs         (비활성  , 비활성 )  Allow samba to create home dirs
virt_use_samba                 (비활성  , 비활성 )  Allow virt to use samba
tmpreaper_use_samba            (비활성  , 비활성 )  Allow tmpreaper to use samba
samba_share_fusefs             (비활성  , 비활성 )  Allow samba to share fusefs
samba_share_nfs                (비활성  , 비활성 )  Allow samba to share nfs
samba_run_unconfined           (비활성  , 비활성 )  Allow samba to run unconfined
samba_load_libgfapi            (비활성  , 비활성 )  Allow samba to load libgfapi
sanlock_use_samba              (비활성  , 비활성 )  Allow sanlock to use samba
samba_enable_home_dirs         (비활성  , 비활성 )  Allow samba to enable home dirs


9. 3에서 만든 폴더에 "samba_share_t"컨텍스트를 추가한다.

[glacier@glacier ~]$ sudo semanage fcontext -a -t samba_share_t "/samba(/.*)?"
[glacier@glacier ~]$ sudo semanage fcontext --list | grep samba_share_t
/samba(/.*)?                                   all files          system_u:object_r:samba_share_t:s0


10. 9에서 추가한 컨텍스트가 적용되도록 컨텍스트 복구명령을 실행한다.

[glacier@glacier ~]$ sudo restorecon -R -v /samba restorecon reset /samba context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:samba_share_t:s0 restorecon reset /samba/test context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:samba_share_t:s0 [glacier@glacier ~]$ sudo ls -alZ /samba/test drwxrwx---. testuser1 testgroup unconfined_u:object_r:samba_share_t:s0 . drwxrwx---. testuser1 testgroup unconfined_u:object_r:samba_share_t:s0 ..



11. 클라이언트에서 서버에 접근해보고, 설정한 대로 구동되고 있는지 확인한다.

[glacier@glacier ~]$ sudo smbstatus

Samba version 4.8.3
PID     Username     Group        Machine                                   Protocol Version  Encryption           Signing              
----------------------------------------------------------------------------------------------------------------------------------------
89907   testuser1    testuser1    192.168.23.1 (ipv4:192.168.23.1:56591)    SMB3_11           AES-128-CCM          partial(AES-128-CMAC)

Service      pid     Machine       Connected at                     Encryption   Signing     
---------------------------------------------------------------------------------------------
test         89907   192.168.23.1  월 12월 24 14시 02분 05초 2018 KST AES-128-CCM  AES-128-CMAC

Locked files:
Pid          Uid        DenyMode   Access      R/W        Oplock           SharePath   Name   Time
--------------------------------------------------------------------------------------------------
89907        1001       DENY_NONE  0x100080    RDONLY     NONE             /samba/test   .   Mon Dec 24 14:02:05 2018




참고:

https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html

https://selinuxproject.org/page/SambaRecipes

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-security-enhanced_linux-working_with_selinux-selinux_contexts_labeling_files

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security-enhanced_linux/sect-security-enhanced_linux-selinux_contexts_labeling_files-persistent_changes_semanage_fcontext

https://www.lesstif.com/pages/viewpage.action?pageId=18219472

Posted by 비형랑#
: