2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux Samba NFS

2
Last updated at Posted at 2025-09-08

はじめに

Linux 同士のファイル共有においては NFS が古くから利用されている。

一方、Linux と Windows 間のファイル共有においては Samba が利用される。

SMB

Server Message Block

Windows で使われているファイル共有プロトコル。

Windows ではエクスプローラのアドレスバーに ¥¥ の後に続けて IP アドレスを入力することができる。

CIFS

Common Internet File System

SMB を拡張したもの。

ワークグループ

複数の Windows コンピュータが構成するネットワークの単位。

Wiondws マシンは必ずいずれかのワークグループに所属する。

NT ドメイン

Windows で 1990 年代 〜 2000 年前後に使われていた古いユーザ認証、管理の仕組み。

単純で軽量だが、セキュリティや拡張性に限界があったとされる。

Active Directory

Windows 2000 Server 以降で採用されている NT ドメイン の後継ディレクトリサービス。

AD と略される。

オープン規格のディレクトリサービス LDAP(Lightweight Directory Access Protocol) をベースに作られている。

AD は階層構造を持ち、以下の単位で管理される。

  • ドメイン
    • ユーザ、グループ、コンピュータが属する
    • 例:example.com
  • ツリー
    • 複数のドメインが階層的につながったもの
    • 例:example.comsales.example.com
  • フォレスト
    • 複数のツリーをまとめた最上位の管理単位

ドメインを管理するためのサーバは ドメインコントローラ と呼ばれる。ドメインコントローラは認証を担当する。

NetBIOS

Windows コンピュータ間で利用される API の名称。

それぞれのホストは NetBIOS 名 で識別される。

NetBIOS 名を利用したローカルネットワーク内でのファイル共有は、インターネットに依存しないのが特徴。

TCP / IP 上で NetBIOS を利用することができる NetBIOS over TCP / IP というプロトコルも存在する。

WINS サーバ

Windows Internet Naming Service

NetBIOS 名と IP アドレスの名前解決を行うサーバ。

Samba

Linux のオープンソースソフトウェア。

Windows との ファイル共有、プリンタ共有、またその際の 認証アクセス制御 のために使用される。

歴史的な背景

「Windows の世界」では古くから SMB というプロトコルでファイル共有、プリンタ共有が行なわれてきた。

SMB は Microsoft が中心となって発展させてきた技術であり、Windows に標準搭載されている。

一方、「Linux の世界」で SMB は実装されておらず、異なる OS 間でのファイル共有が課題となった。

Samba はこうした課題を解消するために登場した。

Samba は Linux 上で SMB プロトコルを実装したソフトウェア群 であり、Linux が 「Windows と同じ方法で」ファイルやプリンタを共有することを可能にする。

Samba ができること

Samba は Windows が利用している SMB / CIFS プロトコルを実装している。

そのため Linux を SMB サーバとして動かした場合、Windows からネットワークドライブ(ネットワーク共有)として Linux のディレクトリにアクセスすることができる。

一方、Linux を SMB クライアントとして動かした場合、Linux から Windows の共有フォルダにアクセスすることができる。

これらはインターネットを経由することなく実現する。

他にも Linux を Active Directory のドメインコントローラとして利用したり、Winbind を使って Windows のユーザアカウントを Linux 側で扱うことなどができる。

Samba を構成するプロセス

  • smbd
    • ファイル共有、プリンタ共有時のリクエストを処理する Samba において主要なデーモン
    • 認証機能を持つ
  • nmbd
    • NetBIOS 名前解決を行うデーモン
    • ブラウズ(ネットワーク上のホスト検出)機能
  • winbindd
    • Windows ドメインとのアカウント統合管理を行うデーモン

Winbind

Samba に含まれるコンポーネント(winbindd デーモン)で、Linux を Windows の認証基盤である Active Directory や NT ドメインと統合するための仕組み。

Linux を Active Directory のドメインコントローラへ参加させ、Active Directory に登録されているユーザを Linux 上で認証できるようにすることができる。

Windows と Linux では下記の通り「ユーザの識別方法」が異なり、Winbind はこの対応関係を管理する。

  • Windows
    • SID (Security Identifier)
  • Linux
    • UID(User Identifier)

また、etc/nsswitch.confpasswd: エントリを下記のように設定しておくことで、ユーザ情報を /etc/passwd だけでなく Winbind 経由で Active Directory からも取得できるようになる。

/etc/nsswitch.conf
passwd: files winbind

さらに PAM(Pluggable Authentication Modules)と組み合わせることで、 Active Directory のユーザが Linux にログインできるようになる。

/etc/samba/smb.conf

Samba の設定ファイル。

複数の [セクション] で構成される。

の 3 つは特別なセクション(予約済)で、それ以外は任意のセクションを記述することができる。

各セクションでは以下のフォーマットで設定値を記述する。

フォーマット
項目 = 値
/etc/samba/smb.conf
/etc/samba/smb.conf
#
# Sample configuration file for the Samba suite for Debian GNU/Linux.
#
#
# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options most of which 
# are not shown in this example
#
# Some options that are often worth tuning have been included as
# commented-out examples in this file.
#  - When such options are commented with ";", the proposed setting
#    differs from the default Samba behaviour
#  - When commented with "#", the proposed setting is the default
#    behaviour of Samba but the option is considered important
#    enough to be mentioned here
#
# NOTE: Whenever you modify this file you should run the command
# "testparm" to check that you have not made any basic syntactic 
# errors. 

#======================= Global Settings =======================

[global]

## Browsing/Identification ###

# Change this to the workgroup/NT-domain name your Samba server will part of
   workgroup = WORKGROUP

# server string is the equivalent of the NT Description field
   server string = %h server (Samba, Ubuntu)

#### Networking ####

# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
;   interfaces = 127.0.0.0/8 eth0

# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
# It is recommended that you enable this feature if your Samba machine is
# not protected by a firewall or is a firewall itself.  However, this
# option cannot handle dynamic or non-broadcast interfaces correctly.
;   bind interfaces only = yes



#### Debugging/Accounting ####

# This tells Samba to use a separate log file for each machine
# that connects
   log file = /var/log/samba/log.%m

# Cap the size of the individual log files (in KiB).
   max log size = 1000

# We want Samba to only log to /var/log/samba/log.{smbd,nmbd}.
# Append syslog@1 if you want important messages to be sent to syslog too.
   logging = file

# Do something sensible when Samba crashes: mail the admin a backtrace
   panic action = /usr/share/samba/panic-action %d


####### Authentication #######

# Server role. Defines in which mode Samba will operate. Possible
# values are "standalone server", "member server", "classic primary
# domain controller", "classic backup domain controller", "active
# directory domain controller". 
#
# Most people will want "standalone server" or "member server".
# Running as "active directory domain controller" will require first
# running "samba-tool domain provision" to wipe databases and create a
# new domain.
   server role = standalone server

   obey pam restrictions = yes

# This boolean parameter controls whether Samba attempts to sync the Unix
# password with the SMB password when the encrypted SMB password in the
# passdb is changed.
   unix password sync = yes

# For Unix password sync to work on a Debian GNU/Linux system, the following
# parameters must be set (thanks to Ian Kahan <<kahan@informatik.tu-muenchen.de> for
# sending the correct chat script for the passwd program in Debian Sarge).
   passwd program = /usr/bin/passwd %u
   passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .

# This boolean controls whether PAM will be used for password changes
# when requested by an SMB client instead of the program listed in
# 'passwd program'. The default is 'no'.
   pam password change = yes

# This option controls how unsuccessful authentication attempts are mapped
# to anonymous connections
   map to guest = bad user

########## Domains ###########

#
# The following settings only takes effect if 'server role = classic
# primary domain controller', 'server role = classic backup domain controller'
# or 'domain logons' is set 
#

# It specifies the location of the user's
# profile directory from the client point of view) The following
# required a [profiles] share to be setup on the samba server (see
# below)
;   logon path = \\%N\profiles\%U
# Another common choice is storing the profile in the user's home directory
# (this is Samba's default)
#   logon path = \\%N\%U\profile

# The following setting only takes effect if 'domain logons' is set
# It specifies the location of a user's home directory (from the client
# point of view)
;   logon drive = H:
#   logon home = \\%N\%U

# The following setting only takes effect if 'domain logons' is set
# It specifies the script to run during logon. The script must be stored
# in the [netlogon] share
# NOTE: Must be store in 'DOS' file format convention
;   logon script = logon.cmd

# This allows Unix users to be created on the domain controller via the SAMR
# RPC pipe.  The example command creates a user account with a disabled Unix
# password; please adapt to your needs
; add user script = /usr/sbin/useradd --create-home %u

# This allows machine accounts to be created on the domain controller via the 
# SAMR RPC pipe.  
# The following assumes a "machines" group exists on the system
; add machine script  = /usr/sbin/useradd -g machines -c "%u machine account" -d /var/lib/samba -s /bin/false %u

# This allows Unix groups to be created on the domain controller via the SAMR
# RPC pipe.  
; add group script = /usr/sbin/addgroup --force-badname %g

############ Misc ############

# Using the following line enables you to customise your configuration
# on a per machine basis. The %m gets replaced with the netbios name
# of the machine that is connecting
;   include = /home/samba/etc/smb.conf.%m

# Some defaults for winbind (make sure you're not using the ranges
# for something else.)
;   idmap config * :              backend = tdb
;   idmap config * :              range   = 3000-7999
;   idmap config YOURDOMAINHERE : backend = tdb
;   idmap config YOURDOMAINHERE : range   = 100000-999999
;   template shell = /bin/bash

# Setup usershare options to enable non-root users to share folders
# with the net usershare command.

# Maximum number of usershare. 0 means that usershare is disabled.
#   usershare max shares = 100

# Allow users who've been granted usershare privileges to create
# public shares, not just authenticated ones
   usershare allow guests = yes

#======================= Share Definitions =======================

# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username
;[homes]
;   comment = Home Directories
;   browseable = no

# By default, the home directories are exported read-only. Change the
# next parameter to 'no' if you want to be able to write to them.
;   read only = yes

# File creation mask is set to 0700 for security reasons. If you want to
# create files with group=rw permissions, set next parameter to 0775.
;   create mask = 0700

# Directory creation mask is set to 0700 for security reasons. If you want to
# create dirs. with group=rw permissions, set next parameter to 0775.
;   directory mask = 0700

# By default, \\server\username shares can be connected to by anyone
# with access to the samba server.
# Un-comment the following parameter to make sure that only "username"
# can connect to \\server\username
# This might need tweaking when using external authentication schemes
;   valid users = %S

# Un-comment the following and create the netlogon directory for Domain Logons
# (you need to configure Samba to act as a domain controller too.)
;[netlogon]
;   comment = Network Logon Service
;   path = /home/samba/netlogon
;   guest ok = yes
;   read only = yes

# Un-comment the following and create the profiles directory to store
# users profiles (see the "logon path" option above)
# (you need to configure Samba to act as a domain controller too.)
# The path below should be writable by all users so that their
# profile directory may be created the first time they log on
;[profiles]
;   comment = Users profiles
;   path = /home/samba/profiles
;   guest ok = no
;   browseable = no
;   create mask = 0600
;   directory mask = 0700

[printers]
   comment = All Printers
   browseable = no
   path = /var/tmp
   printable = yes
   guest ok = no
   read only = yes
   create mask = 0700

# Windows clients look for this share name as a source of downloadable
# printer drivers
[print$]
   comment = Printer Drivers
   path = /var/lib/samba/printers
   browseable = yes
   read only = yes
   guest ok = no
# Uncomment to allow remote administration of Windows print drivers.
# You may need to replace 'lpadmin' with the name of the group your
# admin users are members of.
# Please note that you also need to set appropriate Unix permissions
# to the drivers directory for these users to have write rights in it
;   write list = root, @lpadmin

[global]

Samba 全体の動作に関する設定を行うセクション。

変更した場合には $ systemctl による再起動が必要。

Samba を再起動する
$ systemctl restart smb nmb

基本設定

項目 説明
workgroup Samba サーバが所属する ワークグループ 名を指定する。
デフォルトは WORKGROUP
【例】workgroup = WORKGROUP
server role Samba サーバの動作モードを指定する。
- AUTO:自動判別
- STANDALONE SERVER:独立サーバ(ワークグループで動作)
- MEMBER SERVER:Active Directory ドメインに参加するメンバサーバ
- ACTIVE DIRECTORY DOMAIN CONTROLLER:Active Directory のドメインコントローラ
【例】server role = AUTO
netbios name Windows から見えるホスト名(NetBIOS 名)を指定する。
省略時は Linux のホスト名が利用される。
【例】netbios name = MYSERVER
server string サーバ名に関する説明、コメント欄。
【例】server string = Samba File Server
logon script ログオン時に実行するスクリプトファイル(Windows のバッチファイル)を指定する。
【例】logon script = logon.bat
wins support Samba サーバを WINS サーバ として動作させるかどうか(YesNo)。
【例】wins support = Yes
wins server WINS サーバの IP アドレスを指定する。
wind support = Yes の場合には使わない。
【例】wins server = 192.168.1.10

認証関連

項目 説明
encrypt passwords パスワードを暗号化(正確にはハッシュ化)するかどうか(YesNo)。
【例】encrypt passwords = Yes
smb passwd file パスワードファイルのパスを指定する。
【例】smb passwd file = /etc/samba/smbpasswd
unix password sync Samba のパスワードと Linux のユーザパスワードを同期させるかどうか(YesNo)。
Samba サーバ側でパスワード変更する場合の実行プログラムを passwd program、その際の応答内容を passwd chat で指定する。
【例】unix password sync = Yes
passwd program Samba サーバ側でパスワード変更する場合の実行プログラム。
【例】passwd program = /usr/bin/passwd %u
passwd chat Samba サーバ側でパスワード変更する場合の応答内容。
【例】passwd chat = *New*password* %n\n *Retype*new*password* %n\n *updated*successfully*
username map Windows アカウントと Linux ユーザの紐付けファイルを指定する。
【例】username map = /etc/samba/smbusers
guest ok / public Samba サーバへのゲストアカウントによるログインを許可するかどうか(YesNo)。
【例】guest ok = Yes
【例】public = Yes
security 認証を行うかを指定する。
- USER:ユーザ名とパスワードで認証する
- DOMAIN:WIndows NT ドメインコントローラに認証を委任する(Samba のユーザアカウント が必要になる)
【例】security = USER

アクセス制御関連

項目 説明
valid users アクセス可能なユーザ名。
グループを指定する場合は、グループ名の先頭に @ を付ける。
【例】valid users = alice @staff
hosts allow 接続を許可するホスト名を指定する。
ここに記載がないホストは基本的に接続が拒否される。
【例】hosts allow = 192.168.1. 127.
hosts deny 接続を拒否するホスト名を指定する。
【例】hosts deny = ALL
guest account ゲストアクセス時に使用する Linux ユーザ名。
デフォルトは nobody(匿名)。
アカウントを持たないユーザに対してアクセス許可する際に利用される。
【例】guest account = nobody
map to guest 認証失敗時の挙動を指定する。
- Never:ゲストとしてのログインを許可しない
- Bad User:存在しないユーザなら、ゲストアカウントとしてログインを許可する
- Bad Password:アカウントは存在していても、パスワード誤りならゲストアカウントとしてログインを許可する
【例】map to guest = Bad User
veto files 表示、アクセスをさせないファイル、ディレクトリを指定する。
【例】veto files = /*.mp3/ *.avi/
hide files 例えば「ファイル名が . で始まる隠しファイルを表示しない」などのように、特定のパターンのファイルを隠すための指定。
【例】hide files = /.*/ *.bak/

ログ関連

項目 説明
log file ログファイル名を指定する。
【例】log file = /var/log/samba/log.%m
max log size ログファイルの最大サイズ(KB 単位)。
0 を指定すると、制限を行わない。
【例】max log size = 5000

[homes]

ユーザごとに専用の共有を自動生成するためのセクション。

[homes] 自体の定義は 1 つだが、ユーザごとに、個別の共有が動的に作成される。

Samba はアクセスがあったユーザ名を見て、そのユーザのホームだけを共有対象とするため、ユーザからすると自分のアカウント名を共有名として自分のホームディレクトリにアクセスできる。

[printers]

項目 説明
print ok / printable プリンタの共有として扱うかどうか(YesNo)。
【例】prin ok = Yes

[任意の共有名]

ディレクトリを共有フォルダとしてクライアントに公開するためのセクション。

項目 説明
path 共有ディレクトリのパスを指定する。
【例】path = /srv/docs
browsable クライアント側から「ネットワーク一覧」として表示するかどうかを指定する(YesNo)。
共有名 の末尾を $ としても同じ効果がある。
【例】browsable = Yes
writable 書き込み可能かどうかを指定する(YesNo)。
【例】writable = Yes
read only 読み取り専用にするかどうかを指定する(YesNo)。
【例】read only = No
write list writable = Noread only = Yes が指定された際に、例外的に書き込みを許可するユーザ名を指定する(複数指定可)。
グループを指定する場合は、グループ名の先頭に @ を付ける。
【例】write list = alice @admins
force user 共有フォルダ内のファイル、フォルダの所有者を強制的に指定したユーザにする。
【例】force user = sambauser
force group 共有フォルダ内のファイル、フォルダの所有グループを強制的に指定したグループにする。
【例】force group = sambagroup

$ testparm

/etc/samba/smb.conf の構文チェックをする
$ testparm

Samba ユーザ管理

Windows クライアントが Samba サーバにアクセスするとき、ユーザ認証が行われ、このとき Samba アカウントが使用される。

Samba のユーザアカウントは Linux のアカウントとは別で管理される。

Samba のユーザ認証は、「ユーザ名」と「パスワード」を確認することによって行われる。

ユーザ情報が管理されるデータベースには以下の形式がある。

  • smbpasswd
    • テキスト形式でパスワードファイル
    • /etc/samba/smbpasswd に保存
  • tdbsam
    • バイナリ形式のデータベースファイル
    • /etc/samba/passdb.tdb に保存
  • lsapsam
    • LDAP サーバに保存

※ 参考

$ smbpasswd

アカウントを追加する
$ smbpasswd -a ユーザ名
アカウントを削除する
$ smbpasswd -x ユーザ名
アカウントを無効化する
$ smbpasswd -d ユーザ名
パスワードを変更する
$ smbpasswd ユーザ名

$ pdbedit

アカウントを追加する
$ pdbedit -a ユーザ名
アカウントを削除する
$ pdbedit -x ユーザ名
ユーザを一覧表示する
# pdbedit -L

$ smbstatus

現在の Samba の利用状況を確認するためのコマンド。

監視やトラブルシューティング時に利用される。

Samba サーバに接続しているクライアントの情報を表示
$ smbstatus

$ nmblookup

NetBIOS 名前解決を行うためのコマンド。

ホスト名から IP アドレスを名前解決する
$ nmblookup ホスト名

$ smbclient

Samba サーバや Windows の共有フォルダに接続するためのクライアントコマンド。

Samba サーバに接続する
$ smbclient //サーバ名/共有名 -U ユーザ名

接続に成功すると、以降は対話的にサブコマンドが実行できる。

NFS

Network File System

Linux で標準的に利用されるファイル共有プロトコル。

現行の Linux では NFSv4 が主流。v3 も使われているが、v4 はセキュリティや機能が大幅に改善された。

ネットワークの先にある NFS サーバの公開ディレクトリを、NFS クライアントがマウントすることによって、あたかもそれがローカルのファイルシステムであるかのように扱うことができる。

RPC によって実現される。

NFS サーバ側の エクスポート/etc/exports によって行い、クライアントからの マウント$ mount によって行う。

リモートのファイルシステムを NFS としてマウントする
$ mount -t nfs ホスト名:共有ディレクトリ マウントポイント
$ mount -t nfs IPアドレス:共有ディレクトリ マウントポイント

RPC

Remote Procedure Call

ネットワーク上にあるホストの機能を、別のホストから利用できるようにする仕組み。

NFS の場合、ファイル操作(openreadwriteclose など)が RPC の手続き として定義されている。

NFS サーバ側では、ポート番号と RPC の手続きが rpcbind によって、紐づけられている。

rpcbind

NFS をはじめとする RPC を土台として稼働するサービスが動作するために必要なデーモン。

クライアントから「NFS サービスはどのポートで待ち受けているか?」と言った具合の問い合わせを受け、「対応するポート番号」を返す役割を持つ。

以前は portmap と呼ばれていた。

/etc/exports

NFS サーバが特定のディレクトリを共有ディレクトリとして公開することを エクスポート と言う。

エクスポートは NFS サーバの /etc/exports ファイルによって、「どのクライアントに対して」「どのディレクトリを」「どんな権限で」公開するかが設定できる。

フォーマット
共有ディレクトリ クライアント1(オプション,オプション,...) クライアント2(オプション,...)
例:特定のホストにのみ読み書きを許可する
/export/data 192.168.1.10(rw,sync)
例:サブネット全体に対して読み取り専用で公開する
/export/data 192.168.1.0/24(ro)
例:全てのホストにディレクトリを公開し、全てのユーザを匿名ユーザとして扱う(公開用サーバなど)
/export/data *(rw,all_squash,anonuid=1000,anongid=1000)
オプション 説明
ro 読み取り専用
rw 読み書き可能
sync 書き込みを同期的に処理する(書き込みをディスク反映してから応答)
async 書き込みは非同期的に処理する(高速だが障害時にデータ消失の可能性あり)
no_root_squash クライアント側の root を NFS サーバ側でもそのまま root として扱う
root_squash クライアント側の root を NFS サーバ側では nobody に変換する
all_squash すべてのユーザを匿名ユーザとして扱う
anonuid=UID 匿名ユーザの UID を指定する
anongid=GID 匿名ユーザの GID を指定する

$ rpcinfo

動作している RPC サービスの一覧を表示する
$ rpcinfo -p
$ rpcinfo -p
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
項目 説明
program RPC プログラム番号
vers RPC のバージョン
proto プロトコル
port TCP / UDP ポート番号
service プログラム

$ exportfs

NFS サーバ側 で共有ディレクトリの管理を行うコマンド。

エクスポートの状況を一覧表示する
$ exportfs -v
/etc/exports の変更を反映させる
$ exportfs -ar

-r:設定ファイルを再読み込み
-a:全てのエクスポートを適用

共有を解除する(アンエクスポート)
$ exportfs -u

$ showmount

NFS クライアント側 で NFS サーバがどのディレクトリをエクスポートしているかや、どのクライアントがマウントしているかを表示するコマンド。

エクスポートしているディレクトリを一覧表示する(export)
# showmount -e サーバのホスト名
どのクライアントがマウントしているかを確認する(all)
$ showmount -a サーバホスト名
そのサーバにアクセスしているクライアント一覧を表示する(directory)
$ showmount -d サーバホスト名

$ nfsstat

NFS の統計情報を表示するコマンド。

クライアント、サーバの両方で使用できる

クライアントがサーバに送った NFS リクエストの統計情報を表示する(client)
$ nfsstat -c
サーバがクライアントから受け取った NFS リクエストの統計情報を表示する(server)
$ nfsstat -s
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?