ウェブサイト検索

Windows でのファイル共有のために Ubuntu に Samba をインストールする方法


Samba は無料/オープンソースで、同じネットワーク上の Linux ホストや Windows ホストなどの Unix 系システム間でファイルを共有したり、印刷サービスを提供するために広く使用されているソフトウェアです。

このガイドでは、Ubuntu システムと Windows マシンの間で基本的なファイル共有を行うために Samba4 をセットアップする方法を説明します。ここでは、匿名(安全でない)安全なファイル共有の 2 つのシナリオについて説明します。

推奨読書: Windows でのファイル共有のために CentOS/RHEL 7 に Samba4 をインストールする方法

バージョン 4.0 以降、Samba は Active Directory (AD) ドメイン コントローラー (DC) として使用できることに注意してください。 Samba4 Active Directory ドメイン コントローラーをセットアップするための特別シリーズを編成しました。このシリーズは、Ubuntu、CentOS、および Windows に関する主要なトピックで構成されています。

  1. Samba4 Active Directory ドメイン コントローラーのセットアップ

Ubuntu での Samba のインストールと構成

Samba サーバーは、図に示すように、apt パッケージ マネージャー ツールを使用してデフォルトの Ubuntu リポジトリからインストールできます。

sudo apt install samba samba-common python-dnspython

samba サーバーをインストールしたら、samba サーバーを非安全な匿名安全なファイル共有として設定します。

このためには、メインの Samba 設定ファイル /etc/samba/smb.conf を編集する必要があります (さまざまな設定ディレクティブについて説明しています)。

まず、次のように元の samba 設定ファイルをバックアップします。

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

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

重要: 次に進む前に、Windows マシンが Ubuntu サーバー上で構成される同じワークグループに属していることを確認してください。

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

Windows マシンにログインし、[この PC ] または [マイ コンピュータ ] を右クリックし、 プロパティ >→ システムの詳細設定 コンピュータ名を選択してワークグループを確認します。

または、コマンド プロンプトを開いて以下のコマンドを実行して表示し、「ワークステーション ドメイン 」を探します。

>net config workstation

Windows ワークグループがわかったら、次に進み、ファイル共有用に Samba サーバーを構成します。

匿名の Samba ファイル共有

まず、ファイルを保存する共有 samba ディレクトリを作成します。

sudo mkdir -p /srv/samba/anonymous_shares

次に、ディレクトリに適切な権限を設定します。

sudo chmod -R 0775 /srv/samba/anonymous_shares
sudo chown -R nobody:nogroup /srv/samba/anonymous_shares

次に、構成ファイルを開きます。

sudo vi /etc/samba/smb.conf
OR
sudo nano /etc/samba/smb.conf

次に、以下の説明に従ってディレクティブ設定を編集または変更します。

global]
	workgroup = WORKGROUP
	netbios name = ubuntu
	security = user
[Anonymous]
	comment = Anonymous File Server Share
	path = /srv/samba/anonymous_shares
	browsable =yes
	writable = yes
	guest ok = yes
	read only = no
	force user = nobody

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

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

Press enter to see a dump of your service definitions

Global parameters
[global]
	netbios name = UBUNTU
	server string = %h server (Samba, Ubuntu)
	server role = standalone server
	map to guest = Bad User
	obey pam restrictions = Yes
	pam password change = Yes
	passwd program = /usr/bin/passwd %u
	passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
	unix password sync = Yes
	syslog = 0
	log file = /var/log/samba/log.%m
	max log size = 1000
	dns proxy = No
	usershare allow guests = Yes
	panic action = /usr/share/samba/panic-action %d
	idmap config * : backend = tdb

[printers]
	comment = All Printers
	path = /var/spool/samba
	create mask = 0700
	printable = Yes
[print$]
	comment = Printer Drivers
	path = /var/lib/samba/printers
	browseable = No
[Anonymous]
	comment = Anonymous File Server Share
	path = /srv/samba/anonymous_shares
	force user = nobody
	read only = No
	guest ok = Yes

次に、Samba サービスを再起動して、上記の変更を有効にします。

sudo systemctl restart smbd   [Systemd]
sudo service smbd restart     [Sys V]

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

Windows マシンに移動し、Windows エクスプローラーウィンドウから「ネットワーク」を開きます。 Ubuntu ホスト (この場合は TECMINT) をクリックするか、IP アドレスを使用して samba サーバーへのアクセスを試みます。

\2.168.43.168

: ifconfig コマンドを使用して、Ubuntu サーバーの IP アドレスを取得します。

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

安全な Samba ファイル共有

Samba 共有をパスワードで保護するには、 グループ「smbgrp」 を作成し、各ユーザーにパスワードを設定する必要があります。この例では、ユーザーとして aaronkilik を使用し、パスワードとして「tecmint」を使用します。

sudo addgroup smbgrp
sudo usermod aaronkilik -aG smbgrp
sudo smbpasswd -a aaronkilik

: Samba セキュリティ モード: security=user では、クライアントは共有に接続するためにユーザー名とパスワードを入力する必要があります。

Samba ユーザー アカウントはシステム アカウントとは別のものですが、オプションでシステム ユーザーとパスワードを samba ユーザー データベースと同期するために使用されるlibpam-winbind パッケージをインストールできます。

sudo apt install libpam-winbind

次に、共有ファイルを保存する安全なディレクトリを作成します。

sudo mkdir -p /srv/samba/secure_shares

次に、ディレクトリに適切な権限を設定します。

sudo chmod -R 0770 /srv/samba/secure_shares
sudo chown -R root:smbgrp /srv/samba/secure_shares

次に、構成ファイルを開きます。

sudo vi /etc/samba/smb.conf
OR
sudo nano /etc/samba/smb.conf

次に、以下の説明に従ってディレクティブ設定を編集または変更します。

[Secure]
	comment = Secure File Server Share
	path =  /srv/samba/secure_shares
	valid users = @smbgrp
	guest ok = no
	writable = yes
	browsable = yes

前と同様に、このコマンドを実行して現在の samba 設定を確認します。

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

Press enter to see a dump of your service definitions

Global parameters
[global]
	netbios name = UBUNTU
	server string = %h server (Samba, Ubuntu)
	server role = standalone server
	map to guest = Bad User
	obey pam restrictions = Yes
	pam password change = Yes
	passwd program = /usr/bin/passwd %u
	passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
	unix password sync = Yes
	syslog = 0
	log file = /var/log/samba/log.%m
	max log size = 1000
	dns proxy = No
	usershare allow guests = Yes
	panic action = /usr/share/samba/panic-action %d
	idmap config * : backend = tdb
[printers]
	comment = All Printers
	path = /var/spool/samba
	create mask = 0700
	printable = Yes
[print$]
	comment = Printer Drivers
	path = /var/lib/samba/printers
	browseable = No
[Anonymous]
	comment = Anonymous File Server Share
	path = /srv/samba/anonymous_shares
	force user = nobody
	read only = No
	guest ok = Yes
[Secure]
	comment = Secure File Server Share
	path = /srv/samba/secure_shares
	valid users = @smbgrp
	read only = No

上記の構成が完了したら、Samba サービスを再起動して変更を適用します。

sudo systemctl restart smbd   [Systemd]
sudo service smbd restart     [Sys V]

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

前と同様に、Windows マシンでWindows エクスプローラーウィンドウから「ネットワーク」を開きます。 Ubuntu ホスト (この場合は TECMINT) をクリックします。次のエラーが表示される場合がありますが、続行しない場合は次の手順に進みます。

IP アドレスを使用してサーバーにアクセスしてみます。 \\192.168.43.168 はこのようになります。次に、ユーザー aaronkilik の資格情報 (ユーザー名とパスワード) を入力し、OK をクリックします。

すべての共有ディレクトリが表示されるので、[安全] をクリックして開きます。

一部のファイルをこのディレクトリにドロップすると、ネットワーク上の他の許可されたユーザーと安全に共有できます。

Ubuntu の UFW ファイアウォールで Samba を有効にする

システム上で UFW ファイアウォールが有効またはアクティブになっている場合は、Samba がファイアウォールを通過できるようにルールを追加する必要があります。

これをテストするために、192.168.43.0 ネットワーク スキームを使用しました。ネットワーク アドレスを指定して以下のコマンドを実行します。

sudo ufw allow proto udp to any port 137 from 192.168.43.0/24
sudo ufw allow proto udp to any port 138 from 192.168.43.0/24
sudo ufw allow proto tcp to any port 139 from 192.168.43.0/24
sudo ufw allow proto tcp to any port 445 from 192.168.43.0/24

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

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

それだけです!このガイドでは、Ubuntu と Windows マシンの間で匿名で安全なファイル共有を行うために Samba4 をセットアップする方法を説明しました。以下のフィードバック フォームを使用して、ご意見をお聞かせください。