今更ながらSambaをセットアップする
以前の記事でRaspberryPi-OSにsambaをインストールしましたが、今回はCentOS stream8に入れてみます。
RaspberryPi-OSと違う点
大きく違うのは、firewalldの設定方法とselinuxの対応です。
dnfでインストール
色々設定するのでrootでやります。まずはインストール。
[root@hogeServer ~]# dnf -y install samba
smb.confの設定
[root@hogeServer ~]# cd /etc/samba [root@hogeServer ~]# rename -v conf conf.org smb.conf [root@hogeServer ~]# cp -R smb.conf.sample smb.conf [root@hogeServer ~]# vim smb.conf
smb.confの設定は使い方次第ですが、今回は下記のような設定を行います。
- workgroup名は"WORKGROUP"
- LAN内とVPNアクセスのみ許可
- ゲストログイン禁止
- プリンタ共有無し
- homeディレクトリの共有無し
以下、Sampleファイルから設定変更した分のみ抜粋
~説明等~
#
#======================= Global Settings =====================================
[global]
# ----------------------- Network-Related Options -------------------------
#
~説明等~
#
unix charset = UTF8 #追加:samba側の文字コード設定
dos charset = CP932 #W追加:indowsの文字化け防止
workgroup = WORKGROUP #変更
server string = Samba Server Version %v
netbios name = hogeServer #変更:任意に設定
; interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24
hosts allow = 192.168.***.0/24 10.8.*.0/24 #任意設定(LANとVPNのアクセス許可)
# --------------------------- Logging Options -----------------------------
#
~説明等~
#
# log files split per-machine:
log file = /var/log/samba/log.%m
# maximum size of 50KB per log file, then rotate:
max log size = 50
# ----------------------- Standalone Server Options ------------------------
#
~説明等~
#
security = user
passdb backend = tdbsam
map to guest = Never #追加:ゲストログイン禁止
# ----------------------- Domain Members Options ------------------------
#
# security = must be set to domain or ads.
#
~説明等~
; security = domain
; passdb backend = tdbsam
; realm = MY_REALM
; password server = <NT-Server-Name>
# ----------------------- Domain Controller Options ------------------------
#
# security = must be set to user for domain controllers.
~説明等~
#
; security = user
; passdb backend = tdbsam
; domain master = yes
; domain logons = yes
# the following login script name is determined by the machine name
# (%m):
; logon script = %m.bat
# the following login script name is determined by the UNIX user used:
; logon script = %u.bat
; logon path = \\%L\Profiles\%u
# use an empty path to disable profile support:
; logon path =
# various scripts can be used on a domain controller or a stand-alone
# machine to add or delete corresponding UNIX accounts:
; add user script = /usr/sbin/useradd "%u" -n -g users
; add group script = /usr/sbin/groupadd "%g"
; add machine script = /usr/sbin/useradd -n -c "Workstation (%u)" -M -d /nohome -s /bin/false "%u"
; delete user script = /usr/sbin/userdel "%u"
; delete user from group script = /usr/sbin/userdel "%u" "%g"
; delete group script = /usr/sbin/groupdel "%g"
# ----------------------- Browser Control Options ----------------------------
#
~説明等~
#
; local master = no
; os level = 33
; preferred master = yes
#----------------------------- Name Resolution -------------------------------
#
~説明等~
; wins support = yes
; wins server = w.x.y.z
; wins proxy = yes
; dns proxy = yes
# --------------------------- Printing Options -----------------------------
#
~説明等~
load printers = yes
cups options = raw
; printcap name = /etc/printcap
# obtain a list of printers automatically on UNIX System V systems:
; printcap name = lpstat
; printing = cups
# --------------------------- File System Options ---------------------------
#
~説明等~
; map archive = no
; map hidden = no
; map read only = no
; map system = no
; store dos attributes = yes
#============================ Share Definitions ==============================
#↓[home],[printers]の設定は無効化している
#[homes]
#comment = Home Directories
#browseable = no
#writable = yes
; valid users = %S
; valid users = MYDOMAIN\%S
#[printers]
#comment = All Printers
#path = /var/spool/samba
#browseable = no
#guest ok = no
#writable = no
#printable = yes
#↓[hoge_USBflash]を新規作成
[hoge_USBflash]
path = /mnt/usb_flash/share
writable = yes
guest ok = no
guest only = no
create mode = 777
directory mode = 777
share modes = yes
# Un-comment the following and create the netlogon directory for Domain Logons:
; [netlogon]
; comment = Network Logon Service
; path = /var/lib/samba/netlogon
; guest ok = yes
; writable = no
; share modes = no
# Un-comment the following to provide a specific roaming profile share.
# The default is to use the user's home directory:
; [Profiles]
; path = /var/lib/samba/profiles
; browseable = no
; guest ok = yes
# A publicly accessible directory that is read only, except for users in the
# "staff" group (which have write permissions):
; [public]
; comment = Public Stuff
; path = /home/samba
; public = yes
; writable = no
; printable = no
; write list = +staff
[root@GitLabServer samba]# systemctl start smbd.service
Failed to start smbd.service: Unit smbd.service not found.
以上で設定ファイルの更新完了です。
ユーザー登録
sambaシステムで使用するユーザーを登録します。クライアントの資格情報もこの設定情報でログインします。
[root@GitLabServer samba]# pdbedit -a hogehoge new password: retype new password: Unix username: hogehoge NT username: Account Flags: [U ] User SID: ******************************************** Primary Group SID: ******************************************** Full Name: Home Directory: \\GITLABSERVER\hogehoge HomeDir Drive: Logon Script: Profile Path: \\GITLABSERVER\hogehoge\profile Domain: HOGESERVER Account desc: Workstations: Munged dial: Logon time: 0 Logoff time: 木, 07 2月 2036 00:06:39 JST Kickoff time: 木, 07 2月 2036 00:06:39 JST Password last set: 日, 10 7月 2022 18:18:53 JST Password can change: 日, 10 7月 2022 18:18:53 JST Password must change: never Last bad password : 0 Bad password count : 0 Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
共有フォルダのセットアップ
共有フォルダはUSBメモリやSSD内に作成するため、マウントする前提で作成します。
[root@hogeServer ~]# mkdir /mnt/usb_flash [root@hogeServer ~]# mkdir /mnt/usb_flash/share [root@hogeServer ~]# chmod -R 777 /mnt/usb_flash
ユーザーによる編集制限は行わない共有ディレクトリとなります。
sambaの起動
[root@hogeServer samba]# systemctl start smb.service [root@hogeServer samba]# systemctl enable smb.service
ここまででsamba自体の設定は完了しましたが、接続確認は後述のfirewalld設定の変更を行ってから行います。
firewalldの設定と接続確認
firewalldはサービスを登録するだけでOKです。設定後、リロードして動作確認を行います。
firewall設定の変更
[root@hogeServer samba]# firewall-cmd --add-service=samba --zone=public --permanent success [root@hogeServer samba]# firewall-cmd --reload success
これでWindowsからアクセスしてみると、アクセス拒否されます。原因はSELinuxなので、一時的に機能停止して、接続確認を行います。
SELinuxの一時停止
今の状態はコンテキスト設定がsambaに対応していないため、sambaによるアクセスは拒否されます。そのため、接続確認のために一時的に停止します。
[root@hogeServer samba]# getenforce Enforcing [root@hogeServer samba]# setenforce 0 [root@hogeServer samba]# getenforce Permissive
この後、WindowsPCからアクセスして接続出来ればSELinux以外の設定は完了です。再度、SELinuxを有効化しておきます。
[root@hogeServer samba]# setenforce 1 [root@hogeServer samba]# getenforce Enforcing
残るはSELinuxの対策のみです。
SELinux環境下のsamba設定
SELinuxへの対応は環境により変わります。通常は使用するディレクトリのコンテキスト設定を変更する対策となりますが、外部ストレージをマウントして使用する場合はマウント時のオプションとしてコンテキストを設定します。
どのようなラベルを付ける?
RedHatのサイトを参考にします。"samba_share_t"のラベルが必要なようです。
コンテキストを変更する方法(マウントしない場合)
外部ストレージをマウントしない場合はこの方法でいけます。ただし、マウントするとコンテキストが上書きされてしまいます。
[root@hogeServer hoge]# semanage fcontext -a -t samba_share_t /mnt/usb_flash/share [root@hogeServer hoge]# restorecon -v /mnt/usb_flash/share
コンテキストを指定してマウントする場合
コンテキスト指定はmountに-oコマンドを追加して行います。コンテキスト全体は元々付与されていたコンテキストを参考にして"system_u:object_r:samba_share_t:s0"とします。
[root@hogeServer ~]# mount /dev/sdb1 /mnt/usb_flash/share -o context="system_u:object_r:samba_share_t:s0"
"/dev/sdb1"の部分は環境により変わります。また、NTFSシステムのマウントは-tコマンドも必要です。以上の設定で再度アクセス確認を行って、問題無ければ完了です。



