Samba as a File Server

Level: Intermediate Module: File Sharing & Print 9 min read Lesson 36 of 66

Overview

  • What you will learn: How to install and configure Samba on Ubuntu to serve files over the SMB/CIFS protocol, enabling seamless file sharing between Linux and Windows systems. You will configure shares, manage Samba users, and connect from both Linux and Windows clients.
  • Prerequisites: Understanding of file sharing protocols (Lesson 34), basic Linux administration and networking skills
  • Estimated reading time: 18 minutes

Introduction to SMB/CIFS and Samba

The Server Message Block (SMB) protocol — also known as CIFS (Common Internet File System) — is the standard file sharing protocol in Windows environments. Samba is the open-source implementation of SMB for Unix and Linux systems, allowing them to act as file servers, print servers, and even domain controllers for Windows clients.

Samba consists of several key daemons. smbd handles file and printer sharing over SMB, listening on TCP ports 139 and 445. nmbd provides NetBIOS name resolution and browsing services on UDP ports 137 and 138. Together, they enable a Linux server to appear as a native member of a Windows network.

The primary configuration file is /etc/samba/smb.conf, which defines global settings and individual share definitions. Understanding this file is essential for deploying Samba effectively in any mixed-OS environment.

Installing and Configuring Samba

Installing Samba on Ubuntu is straightforward. After installation, the main task is editing smb.conf to define your workgroup, security settings, and share definitions.

# Install Samba packages
$ sudo apt update
$ sudo apt install samba samba-common-bin

# Verify Samba is running
$ sudo systemctl status smbd
$ sudo systemctl status nmbd

# Backup the original configuration
$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

# Edit the configuration file
$ sudo nano /etc/samba/smb.conf

The configuration file is divided into sections. The [global] section controls server-wide settings, while each additional section defines a share.

# /etc/samba/smb.conf

[global]
   workgroup = WORKGROUP
   server string = Ubuntu File Server
   security = user
   map to guest = never
   dns proxy = no
   log file = /var/log/samba/log.%m
   max log size = 1000
   logging = file

# Public read-only share
[public]
   path = /srv/samba/public
   browseable = yes
   read only = yes
   guest ok = yes

# Private share requiring authentication
[projects]
   path = /srv/samba/projects
   browseable = yes
   read only = no
   valid users = @smbgroup
   create mask = 0660
   directory mask = 0770
   force group = smbgroup

Key [global] parameters include workgroup (must match the Windows workgroup or domain name), security = user (requires username/password authentication), and map to guest which controls how failed logins are handled. For shares, valid users restricts access to specific users or groups (prefixed with @), and create mask / directory mask control default permissions on new files and directories.

Managing Samba Users and Permissions

Samba maintains its own password database separate from the system password file. Every Samba user must first exist as a Linux system user, then be added to the Samba password database with smbpasswd.

# Create the shared directories
$ sudo mkdir -p /srv/samba/public /srv/samba/projects
$ sudo chown nobody:nogroup /srv/samba/public

# Create a system group for Samba users
$ sudo groupadd smbgroup

# Create a system user and add to the Samba group
$ sudo useradd -M -s /usr/sbin/nologin sambauser1
$ sudo usermod -aG smbgroup sambauser1

# Set the Samba password (different from system password)
$ sudo smbpasswd -a sambauser1
# Enter and confirm the SMB password when prompted

# Enable the Samba user
$ sudo smbpasswd -e sambauser1

# Set ownership on the projects share
$ sudo chown -R root:smbgroup /srv/samba/projects
$ sudo chmod -R 2770 /srv/samba/projects

# List Samba users
$ sudo pdbedit -L -v

# Validate the smb.conf syntax
$ testparm

# Restart Samba to apply changes
$ sudo systemctl restart smbd nmbd

The testparm utility is invaluable — it parses smb.conf and reports any syntax errors before you restart the services. Always run it after editing the configuration. The setgid bit (2770) on the projects directory ensures new files inherit the group ownership, maintaining consistent access control.

Connecting from Clients

You can access Samba shares from both Linux and Windows clients. On Linux, the smbclient tool provides an FTP-like interface, while CIFS mounts allow mounting shares directly into the filesystem.

# From a Linux client — install client tools
$ sudo apt install smbclient cifs-utils

# List available shares on the server
$ smbclient -L //192.168.1.10 -U sambauser1

# Connect interactively to a share
$ smbclient //192.168.1.10/projects -U sambauser1
smb: > ls
smb: > put localfile.txt
smb: > get remotefile.txt
smb: > exit

# Mount a Samba share using CIFS
$ sudo mkdir -p /mnt/projects
$ sudo mount -t cifs //192.168.1.10/projects /mnt/projects 
    -o username=sambauser1,uid=1000,gid=1000

# Persistent mount via /etc/fstab
//192.168.1.10/projects  /mnt/projects  cifs  credentials=/etc/samba/creds,uid=1000,gid=1000,_netdev  0  0

# Create a credentials file for security
$ sudo nano /etc/samba/creds
username=sambauser1
password=secretpass
$ sudo chmod 600 /etc/samba/creds

# From Windows — access via Explorer
# Open File Explorer and type: \192.168.1.10projects
# Or map a network drive via: net use Z: \192.168.1.10projects /user:sambauser1

# Firewall: allow Samba through UFW
$ sudo ufw allow samba

For production environments, always use a credentials file rather than putting passwords on the command line or in /etc/fstab. The credentials file should have 600 permissions (readable only by root). On Windows clients, shares appear natively in File Explorer and can be mapped as network drives for persistent access.

Key Takeaways

  • Samba implements the SMB/CIFS protocol on Linux through smbd (file/print sharing on ports 139/445) and nmbd (NetBIOS name services on ports 137/138).
  • All configuration resides in /etc/samba/smb.conf with a [global] section for server settings and named sections for individual shares; always validate with testparm before restarting.
  • Samba users must exist as Linux system users first, then be added to the Samba password database using smbpasswd -a; the two passwords are maintained independently.
  • Linux clients access shares via smbclient for interactive sessions or mount -t cifs for filesystem integration; always use a credentials file with restrictive permissions for persistent mounts.
  • Share-level permissions (valid users, read only) combine with filesystem permissions (create mask, directory mask) — the most restrictive permission always wins.

What’s Next

In the next lesson, you will learn how to promote Samba to an Active Directory Domain Controller, providing centralized authentication, group policies, and directory services for an entire network.

繁體中文

概述

  • 學習內容:如何在 Ubuntu 上安裝和設定 Samba,透過 SMB/CIFS 協定提供檔案共享服務,實現 Linux 與 Windows 系統之間的無縫檔案共享。您將設定共享資源、管理 Samba 使用者,並從 Linux 和 Windows 用戶端進行連線。
  • 先決條件:了解檔案共享協定(第 34 課)、基本 Linux 管理和網路技能
  • 預計閱讀時間:18 分鐘

SMB/CIFS 與 Samba 簡介

伺服器訊息區塊(SMB)協定——也稱為 CIFS(通用網際網路檔案系統)——是 Windows 環境中的標準檔案共享協定。Samba 是 SMB 在 Unix 和 Linux 系統上的開源實作,允許它們充當 Windows 用戶端的檔案伺服器、列印伺服器,甚至網域控制器。

Samba 由幾個關鍵的背景程式組成。smbd 透過 SMB 處理檔案和印表機共享,監聽 TCP 連接埠 139 和 445。nmbd 在 UDP 連接埠 137 和 138 上提供 NetBIOS 名稱解析和瀏覽服務。它們共同使 Linux 伺服器能夠以 Windows 網路的原生成員身分出現。

主要設定檔是 /etc/samba/smb.conf,它定義了全域設定和個別共享定義。了解此檔案對於在任何混合作業系統環境中有效部署 Samba 至關重要。

安裝和設定 Samba

在 Ubuntu 上安裝 Samba 非常簡單。安裝後,主要任務是編輯 smb.conf 來定義您的工作群組、安全設定和共享定義。

# 安裝 Samba 套件
$ sudo apt update
$ sudo apt install samba samba-common-bin

# 驗證 Samba 正在執行
$ sudo systemctl status smbd
$ sudo systemctl status nmbd

# 備份原始設定
$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

# 編輯設定檔
$ sudo nano /etc/samba/smb.conf

設定檔分為多個區段。[global] 區段控制伺服器範圍的設定,而每個額外的區段定義一個共享。

# /etc/samba/smb.conf

[global]
   workgroup = WORKGROUP
   server string = Ubuntu File Server
   security = user
   map to guest = never
   dns proxy = no
   log file = /var/log/samba/log.%m
   max log size = 1000
   logging = file

# 公開唯讀共享
[public]
   path = /srv/samba/public
   browseable = yes
   read only = yes
   guest ok = yes

# 需要驗證的私有共享
[projects]
   path = /srv/samba/projects
   browseable = yes
   read only = no
   valid users = @smbgroup
   create mask = 0660
   directory mask = 0770
   force group = smbgroup

關鍵的 [global] 參數包括 workgroup(必須與 Windows 工作群組或網域名稱匹配)、security = user(需要使用者名稱/密碼驗證)和 map to guest(控制登入失敗的處理方式)。對於共享,valid users 限制對特定使用者或群組(以 @ 為前綴)的存取,而 create mask / directory mask 控制新檔案和目錄的預設權限。

管理 Samba 使用者和權限

Samba 維護自己的密碼資料庫,與系統密碼檔案分開。每個 Samba 使用者必須先以 Linux 系統使用者身分存在,然後使用 smbpasswd 加入 Samba 密碼資料庫。

# 建立共享目錄
$ sudo mkdir -p /srv/samba/public /srv/samba/projects
$ sudo chown nobody:nogroup /srv/samba/public

# 為 Samba 使用者建立系統群組
$ sudo groupadd smbgroup

# 建立系統使用者並加入 Samba 群組
$ sudo useradd -M -s /usr/sbin/nologin sambauser1
$ sudo usermod -aG smbgroup sambauser1

# 設定 Samba 密碼(與系統密碼不同)
$ sudo smbpasswd -a sambauser1

# 啟用 Samba 使用者
$ sudo smbpasswd -e sambauser1

# 設定專案共享的擁有權
$ sudo chown -R root:smbgroup /srv/samba/projects
$ sudo chmod -R 2770 /srv/samba/projects

# 列出 Samba 使用者
$ sudo pdbedit -L -v

# 驗證 smb.conf 語法
$ testparm

# 重新啟動 Samba 以套用變更
$ sudo systemctl restart smbd nmbd

testparm 工具非常有價值——它會解析 smb.conf 並在您重新啟動服務之前報告任何語法錯誤。在編輯設定後務必執行它。專案目錄上的 setgid 位元(2770)確保新檔案繼承群組擁有權,維持一致的存取控制。

從用戶端連線

您可以從 Linux 和 Windows 用戶端存取 Samba 共享。在 Linux 上,smbclient 工具提供類似 FTP 的介面,而 CIFS 掛載允許將共享直接掛載到檔案系統中。

# 從 Linux 用戶端——安裝用戶端工具
$ sudo apt install smbclient cifs-utils

# 列出伺服器上可用的共享
$ smbclient -L //192.168.1.10 -U sambauser1

# 互動式連線到共享
$ smbclient //192.168.1.10/projects -U sambauser1
smb: > ls
smb: > put localfile.txt
smb: > get remotefile.txt
smb: > exit

# 使用 CIFS 掛載 Samba 共享
$ sudo mkdir -p /mnt/projects
$ sudo mount -t cifs //192.168.1.10/projects /mnt/projects 
    -o username=sambauser1,uid=1000,gid=1000

# 透過 /etc/fstab 永久掛載
//192.168.1.10/projects  /mnt/projects  cifs  credentials=/etc/samba/creds,uid=1000,gid=1000,_netdev  0  0

# 為安全建立憑證檔案
$ sudo nano /etc/samba/creds
username=sambauser1
password=secretpass
$ sudo chmod 600 /etc/samba/creds

# 從 Windows——透過檔案總管存取
# 開啟檔案總管並輸入:\192.168.1.10projects
# 或透過以下方式映射網路磁碟機:net use Z: \192.168.1.10projects /user:sambauser1

# 防火牆:允許 Samba 通過 UFW
$ sudo ufw allow samba

在生產環境中,始終使用憑證檔案而不是在命令列或 /etc/fstab 中放置密碼。憑證檔案應具有 600 權限(僅 root 可讀)。在 Windows 用戶端上,共享在檔案總管中以原生方式顯示,並且可以映射為網路磁碟機以實現持久存取。

重點摘要

  • Samba 透過 smbd(連接埠 139/445 上的檔案/列印共享)和 nmbd(連接埠 137/138 上的 NetBIOS 名稱服務)在 Linux 上實作 SMB/CIFS 協定。
  • 所有設定位於 /etc/samba/smb.conf 中,包含伺服器設定的 [global] 區段和個別共享的命名區段;在重新啟動前務必使用 testparm 驗證。
  • Samba 使用者必須先以 Linux 系統使用者身分存在,然後使用 smbpasswd -a 加入 Samba 密碼資料庫;兩組密碼獨立維護。
  • Linux 用戶端透過 smbclient 進行互動式存取,或透過 mount -t cifs 進行檔案系統整合;永久掛載務必使用具有嚴格權限的憑證檔案。
  • 共享層級權限(valid usersread only)與檔案系統權限(create maskdirectory mask)結合——最嚴格的權限始終優先。

下一步

在下一課中,您將學習如何將 Samba 提升為 Active Directory 網域控制器,為整個網路提供集中式驗證、群組原則和目錄服務。

日本語

概要

  • 学習内容:Ubuntu に Samba をインストール・設定し、SMB/CIFS プロトコルでファイル共有サービスを提供して、Linux と Windows システム間のシームレスなファイル共有を実現する方法。共有の設定、Samba ユーザーの管理、Linux および Windows クライアントからの接続方法を学びます。
  • 前提条件:ファイル共有プロトコルの理解(レッスン34)、基本的な Linux 管理とネットワーキングスキル
  • 推定読了時間:18分

SMB/CIFS と Samba の紹介

Server Message Block(SMB)プロトコル — CIFS(Common Internet File System)とも呼ばれます — は、Windows 環境における標準的なファイル共有プロトコルです。Samba は Unix および Linux システム向けの SMB のオープンソース実装であり、Windows クライアントに対してファイルサーバー、プリントサーバー、さらにはドメインコントローラーとして機能することができます。

Samba はいくつかの主要なデーモンで構成されています。smbd は SMB を介したファイルおよびプリンター共有を処理し、TCP ポート 139 と 445 でリッスンします。nmbd は UDP ポート 137 と 138 で NetBIOS 名前解決とブラウジングサービスを提供します。これらが連携することで、Linux サーバーが Windows ネットワークのネイティブメンバーとして表示されます。

主要な設定ファイルは /etc/samba/smb.conf で、グローバル設定と個々の共有定義を定義します。このファイルの理解は、混合 OS 環境で Samba を効果的に展開するために不可欠です。

Samba のインストールと設定

Ubuntu への Samba のインストールは簡単です。インストール後の主なタスクは、smb.conf を編集してワークグループ、セキュリティ設定、共有定義を設定することです。

# Samba パッケージのインストール
$ sudo apt update
$ sudo apt install samba samba-common-bin

# Samba の稼働確認
$ sudo systemctl status smbd
$ sudo systemctl status nmbd

# 元の設定のバックアップ
$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

# 設定ファイルの編集
$ sudo nano /etc/samba/smb.conf

設定ファイルはセクションに分かれています。[global] セクションはサーバー全体の設定を制御し、追加の各セクションが共有を定義します。

# /etc/samba/smb.conf

[global]
   workgroup = WORKGROUP
   server string = Ubuntu File Server
   security = user
   map to guest = never
   dns proxy = no
   log file = /var/log/samba/log.%m
   max log size = 1000
   logging = file

# 公開読み取り専用共有
[public]
   path = /srv/samba/public
   browseable = yes
   read only = yes
   guest ok = yes

# 認証が必要なプライベート共有
[projects]
   path = /srv/samba/projects
   browseable = yes
   read only = no
   valid users = @smbgroup
   create mask = 0660
   directory mask = 0770
   force group = smbgroup

主要な [global] パラメータには、workgroup(Windows のワークグループまたはドメイン名と一致する必要があります)、security = user(ユーザー名/パスワード認証が必要)、および map to guest(ログイン失敗の処理方法を制御)が含まれます。共有では、valid users が特定のユーザーまたはグループ(@ プレフィックス付き)へのアクセスを制限し、create mask / directory mask が新しいファイルとディレクトリのデフォルト権限を制御します。

Samba ユーザーと権限の管理

Samba はシステムのパスワードファイルとは別に独自のパスワードデータベースを維持しています。すべての Samba ユーザーはまず Linux システムユーザーとして存在し、その後 smbpasswd で Samba パスワードデータベースに追加する必要があります。

# 共有ディレクトリの作成
$ sudo mkdir -p /srv/samba/public /srv/samba/projects
$ sudo chown nobody:nogroup /srv/samba/public

# Samba ユーザー用のシステムグループを作成
$ sudo groupadd smbgroup

# システムユーザーを作成して Samba グループに追加
$ sudo useradd -M -s /usr/sbin/nologin sambauser1
$ sudo usermod -aG smbgroup sambauser1

# Samba パスワードを設定(システムパスワードとは異なる)
$ sudo smbpasswd -a sambauser1

# Samba ユーザーを有効化
$ sudo smbpasswd -e sambauser1

# プロジェクト共有の所有権を設定
$ sudo chown -R root:smbgroup /srv/samba/projects
$ sudo chmod -R 2770 /srv/samba/projects

# Samba ユーザーの一覧表示
$ sudo pdbedit -L -v

# smb.conf の構文を検証
$ testparm

# Samba を再起動して変更を適用
$ sudo systemctl restart smbd nmbd

testparm ユーティリティは非常に便利です — smb.conf を解析し、サービスを再起動する前に構文エラーを報告します。設定を編集した後は必ず実行してください。プロジェクトディレクトリの setgid ビット(2770)により、新しいファイルがグループ所有権を継承し、一貫したアクセス制御を維持します。

クライアントからの接続

Linux と Windows の両方のクライアントから Samba 共有にアクセスできます。Linux では smbclient ツールが FTP のようなインターフェースを提供し、CIFS マウントにより共有をファイルシステムに直接マウントできます。

# Linux クライアントから — クライアントツールのインストール
$ sudo apt install smbclient cifs-utils

# サーバー上の利用可能な共有を一覧表示
$ smbclient -L //192.168.1.10 -U sambauser1

# 共有にインタラクティブに接続
$ smbclient //192.168.1.10/projects -U sambauser1
smb: > ls
smb: > put localfile.txt
smb: > get remotefile.txt
smb: > exit

# CIFS を使用して Samba 共有をマウント
$ sudo mkdir -p /mnt/projects
$ sudo mount -t cifs //192.168.1.10/projects /mnt/projects 
    -o username=sambauser1,uid=1000,gid=1000

# /etc/fstab による永続マウント
//192.168.1.10/projects  /mnt/projects  cifs  credentials=/etc/samba/creds,uid=1000,gid=1000,_netdev  0  0

# セキュリティのための認証情報ファイルの作成
$ sudo nano /etc/samba/creds
username=sambauser1
password=secretpass
$ sudo chmod 600 /etc/samba/creds

# Windows から — エクスプローラーでアクセス
# エクスプローラーを開き次を入力:\192.168.1.10projects
# またはネットワークドライブのマッピング:net use Z: \192.168.1.10projects /user:sambauser1

# ファイアウォール:UFW で Samba を許可
$ sudo ufw allow samba

本番環境では、コマンドラインや /etc/fstab にパスワードを記載する代わりに、常に認証情報ファイルを使用してください。認証情報ファイルは 600 権限(root のみ読み取り可能)にする必要があります。Windows クライアントでは、共有はエクスプローラーにネイティブに表示され、永続的なアクセスのためにネットワークドライブとしてマッピングできます。

重要ポイント

  • Samba は smbd(ポート 139/445 でのファイル/プリント共有)と nmbd(ポート 137/138 での NetBIOS ネームサービス)を通じて Linux 上で SMB/CIFS プロトコルを実装します。
  • すべての設定は /etc/samba/smb.conf に格納され、サーバー設定用の [global] セクションと個々の共有用の名前付きセクションがあります。再起動前に必ず testparm で検証してください。
  • Samba ユーザーはまず Linux システムユーザーとして存在し、smbpasswd -a で Samba パスワードデータベースに追加する必要があります。2つのパスワードは独立して管理されます。
  • Linux クライアントは smbclient でインタラクティブセッションを、mount -t cifs でファイルシステム統合を行います。永続マウントには必ず制限付き権限の認証情報ファイルを使用してください。
  • 共有レベルの権限(valid usersread only)とファイルシステム権限(create maskdirectory mask)が組み合わされ、最も制限的な権限が常に優先されます。

次のステップ

次のレッスンでは、Samba を Active Directory ドメインコントローラーに昇格させ、ネットワーク全体に集中認証、グループポリシー、ディレクトリサービスを提供する方法を学びます。

You Missed