跳转至

在 Arch Linux 上安装 Samba

文档时效性说明

本文为早期笔记,可能存在版本过时、命令失效、链接失效、最佳实践变化等问题。请以官方最新文档为准。

原英文标题:Install Samba on Arch Linux

1. 下载软件包

bash sudo pacman -S samba

2. 编辑配置

sudo vim /etc/samba/smb.conf

Samba 配置示例:

# may wish to enable
#
# 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]

# workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH
   workgroup = MYGROUP

# server string is the equivalent of the NT Description field
   server string = Samba Server

# 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

# This option is important for security. It allows you to restrict
# connections to machines which are on your local network. The
# following example restricts access to two C class networks and
# the "loopback" interface. For more examples of the syntax see
# the smb.conf man page
;   hosts allow = 192.168.1. 192.168.2. 127.

# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
;  guest account = pcguest

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

# Put a capping on the size of the log files (in Kb).
   max log size = 50

# Specifies the Kerberos or Active Directory realm the host is part of
;   realm = MY_REALM

# Backend to store user information in. New installations should 
# use either tdbsam or ldapsam. smbpasswd is available for backwards 
# compatibility. tdbsam requires no further configuration.
;   passdb backend = tdbsam

# 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.
# Note: Consider carefully the location in the configuration file of
#       this line.  The included file is read at that point.
;   include = /usr/local/samba/lib/smb.conf.%m

# Configure Samba to use multiple interfaces
# If you have multiple network interfaces then you must list them
# here. See the man page for details.
;   interfaces = 192.168.12.2/24 192.168.13.2/24 

# Where to store roving profiles (only for Win95 and WinNT)
#        %L substitutes for this servers netbios name, %U is username
#        You must uncomment the [Profiles] share below
;   logon path = \\%L\Profiles\%U

# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
;   wins support = yes

# WINS Server - Tells the NMBD components of Samba to be a WINS Client
#   Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
;   wins server = w.x.y.z

# WINS Proxy - Tells Samba to answer name resolution queries on
# behalf of a non WINS capable client, for this to work there must be
# at least one  WINS Server on the network. The default is NO.
;   wins proxy = yes

# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# via DNS nslookups. The default is NO.
   dns proxy = no 

# These scripts are used on a domain controller or stand-alone 
# machine to add or delete corresponding unix accounts
;  add user script = /usr/sbin/useradd %u
;  add group script = /usr/sbin/groupadd %g
;  add machine script = /usr/sbin/adduser -n -g machines -c Machine -d /dev/null -s /bin/false %u
;  delete user script = /usr/sbin/userdel %u
;  delete user from group script = /usr/sbin/deluser %u %g
;  delete group script = /usr/sbin/groupdel %g

server min protocol = CORE


#============================ Share Definitions ==============================
[homes]
   comment = Home Directories
   browseable = no
   writable = yes

# Un-comment the following and create the netlogon directory for Domain Logons
; [netlogon]
;   comment = Network Logon Service
;   path = /usr/local/samba/lib/netlogon
;   guest ok = yes
;   writable = no
;   share modes = no


# Un-comment the following to provide a specific roving profile share
# the default is to use the user's home directory
;[Profiles]
;    path = /usr/local/samba/profiles
;    browseable = no
;    guest ok = yes


# NOTE: If you have a BSD-style print system there is no need to 
# specifically define each individual printer
[printers]
   comment = All Printers
   path = /usr/spool/samba
   browseable = no
# Set public = yes to allow user 'guest account' to print
   guest ok = no
   writable = no
   printable = yes

# This one is useful for people to share files
;[tmp]
;   comment = Temporary file space
;   path = /tmp
;   read only = no
;   public = yes

# A publicly accessible directory, but read only, except for people in
# the "staff" group
;[public]
;   comment = Public Stuff
;   path = /home/samba
;   public = yes
;   writable = no
;   printable = no
;   write list = @staff

# Other examples. 
#
# A private printer, usable only by fred. Spool data will be placed in fred's
# home directory. Note that fred must have write access to the spool directory,
# wherever it is.
;[fredsprn]
;   comment = Fred's Printer
;   valid users = fred
;   path = /homes/fred
;   printer = freds_printer
;   public = no
;   writable = no
;   printable = yes

# A private directory, usable only by fred. Note that fred requires write
# access to the directory.
;[fredsdir]
;   comment = Fred's Service
;   path = /usr/somewhere/private
;   valid users = fred
;   public = no
;   writable = yes
;   printable = no

# a service which has a different directory for each machine that connects
# this allows you to tailor configurations to incoming machines. You could
# also use the %U option to tailor it by user name.
# The %m gets replaced with the machine name that is connecting.
;[pchome]
;  comment = PC Directories
;  path = /usr/pc/%m
;  public = no
;  writable = yes

# A publicly accessible directory, read/write to all users. Note that all files
# created in the directory by users will be owned by the default user, so
# any user with access can delete any other user's files. Obviously this
# directory must be writable by the default user. Another user could of course
# be specified, in which case all files would be owned by that user instead.
;[public]
;   path = /usr/somewhere/else/public
;   public = yes
;   only guest = yes
;   writable = yes
;   printable = no

# The following two entries demonstrate how to share a directory so that two
# users can place files there that will be owned by the specific users. In this
# setup, the directory should be writable by both users and should have the
# sticky bit set on it to prevent abuse. Obviously this could be extended to
# as many users as required.
;[myshare]
;   comment = Mary's and Fred's stuff
;   path = /usr/somewhere/shared
;   valid users = mary fred
;   public = no
;   writable = yes
;   printable = no
;   create mask = 0765

我将 workgroup 改为 WORKGROUP,并在文件末尾添加

1
2
3
4
5
6
[2tb]
    comment = 2TB storage on Samba
    path = /mnt/2tb
    public = no
    writable = yes
    printable = no

然后为 Samba 创建日志文件

1
2
3
sudo mkdir -p /usr/local/samba/var/
sudo touch /usr/local/samba/var/log.smbd
sudo chmod 666 /usr/local/samba/var/log.smbd

3. 添加 SMB 用户

1
2
3
4
5
# 如果尚不存在,为 Samba 用户创建 Linux 用户账户。将 samba_user 替换为你想要的名称:
useradd samba_user
# Samba 用户使用的密码与 Linux 用户账户的密码是分开的。使用与上一步相同的名称创建 Samba 用户账户:
pdbedit -a -u samba_user
smbpasswd samba_user

4. 重启服务

sudo systemctl restart smb

在 Windows 文件资源管理器中输入 //<your_ip>/<shared_folder_name> 访问共享文件夹。

5. 可选:安装 wsdd

yay -S wsdd
sudo systemctl enable --now wsdd

6. 原文(English)

---
tags:
  - Arch Linux
  - Linux
---

# Install Samba on Arch Linux


!!! warning "文档时效性说明"
    本文为早期笔记,可能存在版本过时、命令失效、链接失效、最佳实践变化等问题。请以官方最新文档为准。

## Download packages

 ```bash
sudo pacman -S samba
 ```

## Edit configurations

```bash
sudo vim /etc/samba/smb.conf

Sample configuration of samba:

# 1. may wish to enable
#
# 3. NOTE: Whenever you modify this file you should run the command "testparm"
# 4. to check that you have not made any basic syntactic errors. 
#
#======================= Global Settings =====================================
[global]

# 7. workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH
   workgroup = MYGROUP

# 8. server string is the equivalent of the NT Description field
   server string = Samba Server

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

# 18. This option is important for security. It allows you to restrict
# 19. connections to machines which are on your local network. The
# 20. following example restricts access to two C class networks and
# 21. the "loopback" interface. For more examples of the syntax see
# 22. the smb.conf man page
;   hosts allow = 192.168.1. 192.168.2. 127.

# 23. Uncomment this if you want a guest account, you must add this to /etc/passwd
# 24. otherwise the user "nobody" is used
;  guest account = pcguest

# 25. this tells Samba to use a separate log file for each machine
# 26. that connects
   log file = /usr/local/samba/var/log.%m

# 27. Put a capping on the size of the log files (in Kb).
   max log size = 50

# 28. Specifies the Kerberos or Active Directory realm the host is part of
;   realm = MY_REALM

# 29. Backend to store user information in. New installations should 
# 30. use either tdbsam or ldapsam. smbpasswd is available for backwards 
# 31. compatibility. tdbsam requires no further configuration.
;   passdb backend = tdbsam

# 32. Using the following line enables you to customise your configuration
# 33. on a per machine basis. The %m gets replaced with the netbios name
# 34. of the machine that is connecting.
# 35. Note: Consider carefully the location in the configuration file of
# 36.       this line.  The included file is read at that point.
;   include = /usr/local/samba/lib/smb.conf.%m

# 37. Configure Samba to use multiple interfaces
# 38. If you have multiple network interfaces then you must list them
# 39. here. See the man page for details.
;   interfaces = 192.168.12.2/24 192.168.13.2/24 

# 40. Where to store roving profiles (only for Win95 and WinNT)
# 41.        %L substitutes for this servers netbios name, %U is username
# 42.        You must uncomment the [Profiles] share below
;   logon path = \\%L\Profiles\%U

# 43. Windows Internet Name Serving Support Section:
# 44. WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
;   wins support = yes

# 45. WINS Server - Tells the NMBD components of Samba to be a WINS Client
#   Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
;   wins server = w.x.y.z

# 47. WINS Proxy - Tells Samba to answer name resolution queries on
# 48. behalf of a non WINS capable client, for this to work there must be
# 49. at least one  WINS Server on the network. The default is NO.
;   wins proxy = yes

# 50. DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# 51. via DNS nslookups. The default is NO.
   dns proxy = no 

# 52. These scripts are used on a domain controller or stand-alone 
# 53. machine to add or delete corresponding unix accounts
;  add user script = /usr/sbin/useradd %u
;  add group script = /usr/sbin/groupadd %g
;  add machine script = /usr/sbin/adduser -n -g machines -c Machine -d /dev/null -s /bin/false %u
;  delete user script = /usr/sbin/userdel %u
;  delete user from group script = /usr/sbin/deluser %u %g
;  delete group script = /usr/sbin/groupdel %g

server min protocol = CORE


#============================ Share Definitions ==============================
[homes]
   comment = Home Directories
   browseable = no
   writable = yes

# 55. Un-comment the following and create the netlogon directory for Domain Logons
; [netlogon]
;   comment = Network Logon Service
;   path = /usr/local/samba/lib/netlogon
;   guest ok = yes
;   writable = no
;   share modes = no


# 56. Un-comment the following to provide a specific roving profile share
# 57. the default is to use the user's home directory
;[Profiles]
;    path = /usr/local/samba/profiles
;    browseable = no
;    guest ok = yes


# 58. NOTE: If you have a BSD-style print system there is no need to 
# 59. specifically define each individual printer
[printers]
   comment = All Printers
   path = /usr/spool/samba
   browseable = no
# 60. Set public = yes to allow user 'guest account' to print
   guest ok = no
   writable = no
   printable = yes

# 61. This one is useful for people to share files
;[tmp]
;   comment = Temporary file space
;   path = /tmp
;   read only = no
;   public = yes

# 62. A publicly accessible directory, but read only, except for people in
# 63. the "staff" group
;[public]
;   comment = Public Stuff
;   path = /home/samba
;   public = yes
;   writable = no
;   printable = no
;   write list = @staff

# 64. Other examples. 
#
# 66. A private printer, usable only by fred. Spool data will be placed in fred's
# 67. home directory. Note that fred must have write access to the spool directory,
# 68. wherever it is.
;[fredsprn]
;   comment = Fred's Printer
;   valid users = fred
;   path = /homes/fred
;   printer = freds_printer
;   public = no
;   writable = no
;   printable = yes

# 69. A private directory, usable only by fred. Note that fred requires write
# 70. access to the directory.
;[fredsdir]
;   comment = Fred's Service
;   path = /usr/somewhere/private
;   valid users = fred
;   public = no
;   writable = yes
;   printable = no

# 71. a service which has a different directory for each machine that connects
# 72. this allows you to tailor configurations to incoming machines. You could
# 73. also use the %U option to tailor it by user name.
# 74. The %m gets replaced with the machine name that is connecting.
;[pchome]
;  comment = PC Directories
;  path = /usr/pc/%m
;  public = no
;  writable = yes

# 75. A publicly accessible directory, read/write to all users. Note that all files
# 76. created in the directory by users will be owned by the default user, so
# 77. any user with access can delete any other user's files. Obviously this
# 78. directory must be writable by the default user. Another user could of course
# 79. be specified, in which case all files would be owned by that user instead.
;[public]
;   path = /usr/somewhere/else/public
;   public = yes
;   only guest = yes
;   writable = yes
;   printable = no

# 80. The following two entries demonstrate how to share a directory so that two
# 81. users can place files there that will be owned by the specific users. In this
# 82. setup, the directory should be writable by both users and should have the
# 83. sticky bit set on it to prevent abuse. Obviously this could be extended to
# 84. as many users as required.
;[myshare]
;   comment = Mary's and Fred's stuff
;   path = /usr/somewhere/shared
;   valid users = mary fred
;   public = no
;   writable = yes
;   printable = no
;   create mask = 0765

I changed workgroup to WORKGROUP, and added

1
2
3
4
5
6
[2tb]
    comment = 2TB storage on Samba
    path = /mnt/2tb
    public = no
    writable = yes
    printable = no

at the end of file.

then create the log file for samba

1
2
3
sudo mkdir -p /usr/local/samba/var/
sudo touch /usr/local/samba/var/log.smbd
sudo chmod 666 /usr/local/samba/var/log.smbd

Add smb user

1
2
3
4
5
# 85. If it doesn't exist yet, create a Linux user account for the Samba user. Substitute samba_user with your preferred name:
useradd samba_user
# 86. Samba users use a password separate from that of the Linux user accounts. Create the Samba user account with the same name as in the previous command:
pdbedit -a -u samba_user
smbpasswd samba_user

Restart services

sudo systemctl restart smb

And access shared folder by typing //<your_ip>/<shared_folder_name> in Windows file explorer.

Optional: Install wsdd

yay -S wsdd
sudo systemctl enable --now wsdd
```