Sambaサーバーの構築

Sambeサーバを構築する。

何ができるか?

  • ノード間でファイル共有ができるようになる
  • Linuxを仮想環境で構築した場合、ホスト間でファイル共有ができるようになる

想定環境

  • Samba Server:192.168.11.123
    • ネットワーク:192.168.11.0/24
    • 共有ディレクトリ/samba/share
    • ユーザ:vagrant
      • システムユーザおよび、ホームディレクトリが存在することを前提とする。

Build Samba Server

Parameters

user=vagrant
pass=vagrant
share_dir=/samba/share

Install Packages and Setting

dnf -y update

# Firewall
dnf -y install firewalld
systemctl enable firewalld --now
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload

# Install Samba and Setting Conf.
dnf install -y samba samba-common
cp -pv /etc/samba/smb.conf /etc/samba/smb.conf.bak

cat <<SAMBACONF > /etc/samba/smb.conf
[global]
security = user
map to guest = Bad User
netbios name = linux
hosts allow = 192.168.11. 127.
mangled names = no
vfs objects = catia
catia:mappings = 0x22:0xa8,0x2a:0xa4,0x2f:0xf8,0x3a:0xf7,0x3c:0xab,0x3e:0xbb,0x3f:0xbf,0x5c:0xff,0x7c:0xa6
unix charset = UTF-8
dos charset = cp932
read only = no
writeable = yes
force create mode = 0644
force directory mode = 0755
passdb backend = tdbsam

[Share]
path = ${share_dir}
valid users = ${user}
SAMBACONF

pdbedit -a -u ${user}

Setting of Directories

# Setting of Directories
yum install -y policycoreutils-python-utils

mkdir -p ${share_dir}
install -m 0777 -o nobody -g nobody -d ${share_dir}
for ea in $(find $(dirname ${share_dir}))
do
  semanage fcontext -a -t samba_share_t ${ea}
done
restorecon -Rv $(dirname ${share_dir})
ls -1RZ $(dirname ${share_dir})

systemctl enable  smb.service nmb.service
systemctl restart smb.service nmb.service

Connection Test

クライアントのインストールを実施し、その後、共有フォルダへアクセスする。

Test by hand

dnf -y install samba-client
smbclient //192.168.11.123/share

Test 2

Linux上でマウントする際は、一般ユーザに権限を与える形(uid,gidを指定)する形をおすすめする。

dnf -y install cifs-utils
# as vagrant
# uid,gidはユーザに合わせる
mount -t cifs -o username=vagrant,password=vagrant,uid=1000,gid=1000 '//192.168.11.123/Share' /media ; echo $?

マウントコマンド詳細

# mount -t cifs [-o <options>] //<接続先>/<共有フォルダ名> <マウント先>
  • 接続先:IPアドレス、サーバ名
  • 共有フォルダ名:samba.confの中で、見出しの共有フォルダ名
  • マウント先:任意のディレクト
  • -o:オプション

rootでマウントすると、一般ユーザで書き込めないため、uid,gidをオプションに付加する。ゲストユーザも状況は同じである。

SELinux関連

/homeをsamba共有する方法について

ホームディレクトリをSambaで共有する場合

# コンフィグ追加(/etc/samba/smb.conf)
[homes]
valid users = %S, %D%w%S
browseable = no

SELinux設定変更

ホームディレクトリをSamba側へ共有する設定

setsebool -P samba_enable_home_dirs 1

systemctl enable  smb.service nmb.service
systemctl restart smb.service nmb.service

上のコマンドは、認証はあるものの/home全てをSamba側へ共有している。構築する状況によっては、個別にフォルダを切ること・個別にSambaにフォルダを見せるようにする必要がある(コンテンツの共有ファイル、範囲を考慮する必要がある。SELinuxの設定を変更する場合は、影響範囲を考えて、慎重に行わなければいけない。/home以下の個別設定等は、本稿では割愛する)。

Connection Test

mount -t cifs -o username=vagrant,password=vagrant,uid=1000,gid=1000 '//192.168.11.123/vagrant' /media

Shareディレクトリのラベルについて

semanageまたは、restoreconを再度打鍵すると、ファイルのSELinuxセキュリティコンテキストラベルが変更される場合がある。以下にファイルサーバとして使用できる状態を示す。タイプが、samba_share_tになっていることが、Sambaへの共有を示す。

[root@localhost ~]# ls -1RZ /samba
/samba:
unconfined_u:object_r:samba_share_t:s0 share

/samba/share:
system_u:object_r:samba_share_t:s0 share
[root@localhost ~]# 

SELinuxセキュリティコンテキスト(ls -Z)

system_u:object_r:samba_share_t:s0 <dir/file name>

Macからの接続

  • Finder -> 移動 -> サーバへ移動...(⌘+k)

接続URI

Windows 11からの接続

コマンドプロンプトにて

net use r: \\192.168.11.123\Share vagrant /user:vagrant

Environments

[root@localhost ~]# cat /etc/redhat-release 
AlmaLinux release 9.3 (Shamrock Pampas Cat)
[root@localhost ~]# 
[root@localhost ~]# smbd --version
Version 4.18.6
[root@localhost ~]#