ウェブサイト検索

Windows でファイル共有を行うために CentOS 7 に Samba4 をインストールする方法


前回の記事では、Ubuntu システムと Windows マシンの間で基本的なファイル共有を行うために、Ubuntu に Samba4 をインストールする方法を説明しました。ここでは、匿名 (安全でない) および安全なファイル共有の構成について説明しました。

ここでは、他の Linux システムと Windows マシンの間で基本的なファイル共有を行うために、CentOS 7 に Samba4 をインストールして構成する方法 (RHEL 7 でも動作します) を説明します。

重要: バージョン 4.0 以降、Samba は Active Directory (AD) ドメイン コントローラー (DC) として実行できます。 Samba4 Active Directory ドメイン コントローラーのセットアップに関する特別シリーズを一読することをお勧めします。このシリーズには、Ubuntu、CentOS、Windows に関する重要なトピックが含まれています。

CentOS 7 に Samba4 をインストールする

1. まず、示されているように、yum パッケージ マネージャー ツールを使用して、デフォルトの CentOS リポジトリから Samba4 と必要なパッケージをインストールします。

yum install samba samba-client samba-common

2. samba パッケージをインストールした後、次のコマンドを使用して、samba サービスがシステム ファイアウォールを通過できるようにします。

firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload

Windows マシンのワークグループ設定を確認する

3. samba の設定に進む前に、Windows マシンが CentOS サーバー上で設定される同じワークグループに属していることを確認してください。

Windows マシンのワークグループ設定を表示するには、次の 2 つの方法があります。

  • [この PC ] または [マイ コンピュータ ] を右クリック → [プロパティ] → [システムの詳細設定] → [コンピュータ名

  • または、cmd プロンプトを開いて次のコマンドを実行し、次に示すように出力内で「ワークステーション ドメイン」を探します。
>net config workstation

CentOS 7 での Samba4 の構成

4. メインの samba 設定ファイルは /etc/samba/smb.conf です。元のファイルには、ガイドとなるさまざまな設定ディレクティブを説明する事前設定が付属しています。

ただし、samba を設定する前に、次のようにデフォルトのファイルのバックアップを取ることをお勧めします。

cp /etc/samba/smb.conf /etc/samba/smb.conf.orig

次に、以下で説明するように、匿名サービスと安全なファイル共有サービス用に samba の設定に進みます。

Samba4 匿名ファイル共有

5. まず、サーバー上にファイルを保存する共有ディレクトリを作成し、そのディレクトリに適切なアクセス許可を設定します。

mkdir -p /srv/samba/anonymous
chmod -R 0775 /srv/samba/anonymous
chown -R nobody:nobody /srv/samba/anonymous

また、samba 共有ディレクトリの SELinux セキュリティ コンテキストを次のように変更する必要があります。

chcon -t samba_share_t /srv/samba/anonymous

6. 次に、Samba 構成ファイルを開いて編集します。ここで、対応するディレクティブを使用して以下のセクションを変更/追加できます。

vi /etc/samba/smb.conf
[global]
	workgroup = WORKGROUP
	netbios name = centos
	security = user
[Anonymous]
	comment = Anonymous File Server Share
	path = /srv/samba/anonymous
	browsable =yes
	writable = yes
	guest ok = yes
	read only = no
	force user = nobody

7. 次に、以下のコマンドを実行して、現在の samba 設定を確認します。

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

Press enter to see a dump of your service definitions

Global parameters
[global]
	netbios name = centos
	printcap name = cups
	security = USER
	idmap config * : backend = tdb
	cups options = raw
[homes]
	comment = Home Directories
	browseable = No
	inherit acls = Yes
	read only = No
	valid users = %S %D%w%S
[printers]
	comment = All Printers
	path = /var/tmp
	browseable = No
	printable = Yes
	create mask = 0600
[print$]
	comment = Printer Drivers
	path = /var/lib/samba/drivers
	create mask = 0664
	directory mask = 0775
	write list = root
[Anonymous]
 	comment = Anonymous File Server Share
	path = /srv/samba/anonymous
	force user = nobody
	guest ok = Yes
	read only = No

8. 最後に、Samba サービスを起動して有効にし、次回の起動時に自動的に起動し、上記の変更を適用して有効にします。

systemctl enable smb.service
systemctl enable nmb.service
systemctl start smb.service
systemctl start nmb.service

匿名の Samba ファイル共有のテスト

9. Windows マシンで、Windows エクスプローラ ウィンドウから [ネットワーク ] を開き、CentOS ホストをクリックするか、次の操作を試します。 IP アドレスを使用してサーバーにアクセスします (IP アドレスを取得するには ifconfig コマンドを使用します)。

e.g. \2.168.43.168.

10. 次に、匿名 ディレクトリを開き、そこに他のユーザーと共有するファイルを追加してみます。

Samba4 の安全なファイル共有のセットアップ

11. まず、samba システム グループを作成し、次にそのグループにユーザーを追加し、各ユーザーのパスワードを設定します。

groupadd smbgrp
usermod tecmint -aG smbgrp
smbpasswd -a tecmint

12. 次に、共有ファイルを保存する安全なディレクトリを作成し、samba の SELinux セキュリティ コンテキストを使用してディレクトリに適切なアクセス許可を設定します。

mkdir -p /srv/samba/secure
chmod -R 0770 /srv/samba/secure
chown -R root:smbgrp /srv/samba/secure
chcon -t samba_share_t /srv/samba/secure

13. 次に、設定ファイルを編集用に開き、対応するディレクティブを含む以下のセクションを変更/追加します。

vi /etc/samba/smb.conf
[Secure]
	comment = Secure File Server Share
	path =  /srv/samba/secure
	valid users = @smbgrp
	guest ok = no
	writable = yes
	browsable = yes

14. 再度、次のコマンドを実行して samba 構成設定を確認します。

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

Press enter to see a dump of your service definitions

Global parameters
[global]
	netbios name = centos
	printcap name = cups
	security = USER
	idmap config * : backend = tdb
	cups options = raw
[homes]
	comment = Home Directories
	browseable = No
	inherit acls = Yes
	read only = No
	valid users = %S %D%w%S
[printers]
	comment = All Printers
	path = /var/tmp
	browseable = No
	printable = Yes
	create mask = 0600
[print$]
	comment = Printer Drivers
	path = /var/lib/samba/drivers
	create mask = 0664
	directory mask = 0775
	write list = root
[Anonymous]
 	comment = Anonymous File Server Share
	path = /srv/samba/anonymous
	force user = nobody
	guest ok = Yes
	read only = No
[Secure]
	comment = Secure File Server Share
	path = /srv/samba/secure
	read only = No
	valid users = @smbgrp

15. Samba サービスを再起動して、変更を適用します。

systemctl restart smb.service
systemctl restart nmb.service

安全な Samba ファイル共有のテスト

16. Windows マシンに移動し、Windows エクスプローラー ウィンドウから [ネットワーク] を開き、CentOS ホストをクリックするか、アクセスを試みます。サーバーはその IP アドレスを使用します。

e.g. \2.168.43.168.

CentOS サーバーにログインするためにユーザー名とパスワードを入力するように求められます。認証情報を入力したら、[OK] をクリックします。

17. ログインに成功すると、すべての samba 共有ディレクトリが表示されます。ファイルを安全ディレクトリにドロップすることで、ネットワーク上の他の許可されたユーザーと安全に共有できるようになりました。

ネットワーク上での Samba ファイル共有に関する役立つ記事も参照してください。

  1. Linux でローカルおよびネットワーク (Samba および NFS) ファイルシステムをマウント/アンマウントする方法
  2. ACL (アクセス制御リスト) の使用と Samba/NFS 共有のマウント
  3. Linux システムの SambaCry 脆弱性 (CVE-2017-7494) を修正する方法

このガイドでは、CentOS と他の Linux システムおよび Windows マシン間で匿名で安全なファイル共有を行うために Samba4 をセットアップする方法を説明しました。ご意見がございましたら、以下のコメントセクションからお知らせください。