Linux › Section L · Network file transfer and sharing › Topic L.2
File sharing with Samba
Copying files moves them; sharing lets several machines work on the same files in place. Samba lets a Linux machine share directories with Windows and macOS using their native protocol, SMB, and lets Linux reach shares on Windows servers too.
Why share files, and what it costs
File sharing attaches a directory on one machine to another so that it looks local, giving a team or a household one common place for documents and media. Network storage is slower than a local disk. Streaming one large file is fine, because the server can read ahead, but work that opens many small files, such as compiling a large program or editing video, waits on network latency for every access. Keep such work on local storage. Security matters as well: before sharing, decide who may connect, how they prove their identity, and whether traffic needs encryption on the networks it crosses.
Samba and SMB
Windows shares files and printers with the Server Message Block protocol, SMB, and macOS speaks it too. Samba is the free, open-source implementation of SMB for Unix-like systems. As a server it lets other machines open shares on Linux, and its client tools reach Windows shares. Two daemons do the work: smbd serves files and printers, and nmbd answers old NetBIOS name queries. Since Samba 4.11, released in 2019, the obsolete and insecure SMB1 protocol has been disabled by default; the smb.conf manual gives SMB2_02 as the default server min protocol. Install the samba package with apt or dnf. The service is smbd on Debian and Ubuntu and smb on Fedora and RHEL.
smb.conf: the global section
Samba reads /etc/samba/smb.conf, an INI-style file of bracketed sections such as [global], [homes] and [printers], each followed by name = value lines. [global] holds settings for the whole server. workgroup names the Windows workgroup, default WORKGROUP; server string is a description, default Samba followed by its version; netbios name sets the server name, which otherwise comes from the hostname. Access can be narrowed: interfaces = 192.168.1.0/24 with bind interfaces only = yes makes Samba serve only that network. The default password store is passdb backend = tdbsam, a small local database. After any edit, run testparm, which checks the file for errors and prints the settings Samba will actually use.
Defining a share
Each share is a section named after it. A share called projects might read: [projects], then path = /srv/projects, comment = Team projects, read only = no, guest ok = no and valid users = ada, bob. path is the Linux directory shared. read only defaults to yes; writable = yes, also spelled writeable, means the same as read only = no. guest ok, also called public, defaults to no; yes allows connections with no password, as the system's guest account, and should never be combined with a writable share. valid users restricts the share; when empty, any authenticated user may connect. browseable = no hides the share from browse lists, though anyone who knows its name can still connect.
Home directories and printers
A section named [homes] shares every user's home directory without listing them one by one. When ada connects to a share called ada, Samba finds no such section, treats the name as a username and builds the share from [homes] on the fly, with ada's home directory as its path. Typical settings are browseable = no, so the list does not show a homes entry, and read only = no. To keep Windows-side homes elsewhere, set a path using a substitution: %S is the current share name, which for a home share is the username, so path = /srv/winhome/%S gives each user a separate folder. A [printers] section with printing = cups and printable = yes shares the printers that CUPS manages.
Samba passwords
Windows clients authenticate with SMB credentials, which Samba cannot check against the Linux password hashes in /etc/shadow, so it keeps its own database. Every Samba user must first exist as a Linux account. As root, smbpasswd -a ada adds ada and asks for her SMB password twice; smbpasswd -x ada deletes her entry, -d disables it and -e enables it again. Plain smbpasswd, run by an ordinary user, changes that user's own entry. The Linux and Samba passwords are separate unless unix password sync = yes is set; its default is no, and turning it on confuses users who later find that changing one password changed the other. On a Windows domain, Samba can instead join the domain and use its authentication.
Worked example: sharing a team folder
Share /srv/projects with ada and bob only. 1. Create the directory and a Linux group: groupadd team, then usermod -aG team ada and the same for bob, then chgrp team /srv/projects and chmod 2770 /srv/projects, giving rwxrws---: owner and group full access, others nothing, and the setgid bit so new files belong to team. 2. Add the [projects] section from the previous card, with valid users = @team; the @ means members of that Linux group. 3. Run testparm and fix any error it reports. 4. Add Samba passwords: smbpasswd -a ada and smbpasswd -a bob. 5. Restart the service: systemctl restart smbd on Debian or Ubuntu, systemctl restart smb on Fedora. 6. Allow TCP port 445 through the firewall and test from a client; logs are in /var/log/samba.
The client side: smbclient
smbclient, from the Samba client package, talks to any SMB server from the command line. smbclient -L //fileserver -U ada asks for ada's password and lists the shares, with a type for each. Disk shares hold files, Printer shares accept print jobs, and IPC$ is used internally for remote management. To work inside a share, run smbclient //fileserver/projects -U ada. The prompt smb: \> then accepts commands much like an FTP client: ls, cd dir, get file to download, put file to upload, lcd dir to change the local directory, and !cmd to run a local command such as !ls. help lists them all and exit leaves. The Windows form with backslashes also works but needs quoting in the shell.
Worked example: mounting a Windows share
Attach the share projects on the server fileserver at /mnt/projects. 1. Install the CIFS utilities: cifs-utils on both Debian-family and Fedora-family systems. 2. Create the mount point: mkdir /mnt/projects. 3. Store the credentials in a file rather than on the command line, where they would appear in shell history and process listings. The file /root/.smbcred holds two lines, username=ada and password= followed by her password, and chmod 600 /root/.smbcred (rw-------) lets only root read it. 4. Mount, as root: mount -t cifs //fileserver/projects /mnt/projects -o credentials=/root/.smbcred,uid=ada. uid=ada makes the files appear owned by ada rather than root. 5. Check with ls /mnt/projects, and detach with umount /mnt/projects. Permanent mounts belong in /etc/fstab, as in "Mounting filesystems and /etc/fstab".
Common mistakes to avoid
First, forgetting that a Samba user needs both a Linux account and an smbpasswd entry. Second, combining guest ok = yes with a writable share. Third, editing smb.conf and skipping testparm. Fourth, expecting browseable = no to block access; it only hides the name. Fifth, putting a password in a mount command or in /etc/fstab instead of a protected credentials file. Sixth, re-enabling SMB1 for an old device instead of replacing it. Seventh, running file-heavy work, such as builds, on a network share.
Key takeaways
Samba implements SMB, the file and printer sharing protocol of Windows, on Linux; smbd serves files, and SMB1 has been off by default since Samba 4.11. Configuration lives in /etc/samba/smb.conf: [global] for server settings, one section per share with path, read only, guest ok and valid users, [homes] for home directories and [printers] for CUPS printers. Check edits with testparm. Samba keeps its own passwords, managed with smbpasswd. On the client, smbclient lists and browses shares, and mount -t cifs with a credentials file attaches a share to a directory.
Flashcards
Flashcard
Server Message Block, the Windows file and printer sharing protocol
Test your understanding
Question 1 of 5
A share section contains path = /srv/docs and nothing else. What can users who connect do?
Question 2 of 5
Linux user bob exists, but he cannot log in to any Samba share with his Linux password. What is the most likely missing step?
Question 3 of 5
Why should a mount -t cifs command use credentials=/root/.smbcred instead of password= on the command line?
Question 4 of 5
A share has browseable = no. What effect does this have?
Question 5 of 5
What is true of SMB1 on a current, default Samba server?
