Featured image of post Installing SMB Service on Ubuntu 22.04

Installing SMB Service on Ubuntu 22.04

This article introduces how to install and configure SMB service on Ubuntu 22.04, including installing SMB service, configuring smb.conf file, and starting the service.

Introduction

SMB (Server Message Block) is a network file sharing protocol widely used for sharing files and printers between different operating systems. Installing and configuring SMB service on Ubuntu 22.04 makes it convenient to share files between Linux and Windows systems. This article will provide detailed instructions on how to install, configure, and use SMB service on Ubuntu 22.04.

Installing SMB Service

Update System Packages

First, ensure system packages are up to date:

1
2
sudo apt update
sudo apt upgrade -y

Install Samba

Samba is the open-source software package that implements the SMB protocol on Linux systems:

1
sudo apt install samba samba-common-bin -y

Verify Installation

Check the Samba service status:

1
2
sudo systemctl status smbd
sudo systemctl status nmbd

Configuring SMB Service

Backup Default Configuration File

Before modifying the configuration file, backup the original file:

1
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup

Edit Configuration File

Use a text editor to edit the SMB configuration file:

1
sudo nano /etc/samba/smb.conf

Basic Configuration Example

Add the following content to the configuration file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[global]
   workgroup = WORKGROUP
   server string = Ubuntu SMB Server
   log file = /var/log/samba/log.%m
   max log size = 1000
   logging = file
   panic action = /usr/share/samba/panic-action %d
   server role = standalone server
   obey pam restrictions = yes
   unix password sync = yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
   pam password change = yes
   map to guest = bad password
   usershare allow guests = yes

[shared]
   comment = Shared Folder
   path = /srv/samba/shared
   browseable = yes
   read only = no
   guest ok = yes
   create mask = 0755
   directory mask = 0755

Create Shared Directory

Create the directory for sharing:

1
2
3
sudo mkdir -p /srv/samba/shared
sudo chown nobody:nogroup /srv/samba/shared
sudo chmod 777 /srv/samba/shared

Verify Configuration File

Use the testparm command to verify configuration file syntax:

1
sudo testparm

User Management

Create SMB User

If user authentication is needed, first create a system user:

1
sudo adduser smbuser

Then add the user to the SMB database:

1
sudo smbpasswd -a smbuser

Enable/Disable User

Enable SMB user:

1
sudo smbpasswd -e smbuser

Disable SMB user:

1
sudo smbpasswd -d smbuser

Delete User

Delete SMB user:

1
sudo smbpasswd -x smbuser

Starting and Managing Services

Start Services

Start SMB services:

1
2
sudo systemctl start smbd
sudo systemctl start nmbd

Enable Auto-start

Set services to start automatically on boot:

1
2
sudo systemctl enable smbd
sudo systemctl enable nmbd

Restart Services

Restart SMB services:

1
2
sudo systemctl restart smbd
sudo systemctl restart nmbd

Check Service Status

Check service running status:

1
2
sudo systemctl status smbd
sudo systemctl status nmbd

Firewall Configuration

If firewall is enabled, open SMB related ports:

1
sudo ufw allow 'Samba'

Or manually open ports:

1
2
3
4
sudo ufw allow 139/tcp
sudo ufw allow 445/tcp
sudo ufw allow 137/udp
sudo ufw allow 138/udp

Testing Connection

Local Testing

Test SMB shares locally:

1
smbclient -L localhost

Mount Share

Mount SMB share on Linux client:

1
2
sudo mkdir /mnt/smbshare
sudo mount -t cifs //SERVER_IP/shared /mnt/smbshare -o username=smbuser

Windows Client

In Windows File Explorer address bar, enter:

1
\\SERVER_IP\shared

Advanced Configuration

Home Directory Sharing

Configure home directory sharing for each user:

1
2
3
4
5
6
7
[homes]
   comment = User Home Directories
   browseable = no
   read only = no
   create mask = 0700
   directory mask = 0700
   valid users = %S

Read-only Share

Configure read-only share:

1
2
3
4
5
6
[readonly]
   comment = Read-only Share
   path = /srv/samba/readonly
   browseable = yes
   read only = yes
   guest ok = yes

Security Settings

Configuration for enhanced security:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[global]
   # Disable SMB1 protocol
   server min protocol = SMB2
   
   # Enable encryption
   server signing = mandatory
   
   # Restrict access by IP
   hosts allow = 192.168.1.0/24 127.0.0.1
   hosts deny = ALL

Common Issues

Permission Issues

If encountering permission issues, check the following settings:

  • File system permissions
  • SELinux settings (if enabled)
  • SMB user permissions

Connection Issues

If unable to connect, check:

  • Firewall settings
  • Network connectivity
  • Service status

Performance Optimization

Configuration for optimizing SMB performance:

1
2
3
4
5
6
7
[global]
   socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
   read raw = yes
   write raw = yes
   max xmit = 65535
   dead time = 15
   getwd cache = yes

Monitoring and Logging

View Logs

SMB log file locations:

1
2
sudo tail -f /var/log/samba/log.smbd
sudo tail -f /var/log/samba/log.nmbd

Connection Status

View current connections:

1
sudo smbstatus

Share List

List all shares:

1
sudo smbclient -L localhost

Summary

This article provides a complete guide to install and configure SMB service on Ubuntu 22.04. SMB service offers a convenient solution for cross-platform file sharing, suitable for both home networks and enterprise environments.

Key points:

  1. Regularly backup configuration files
  2. Set appropriate user permissions
  3. Configure proper firewall rules
  4. Monitor service status and logs
  5. Adjust performance parameters as needed

A properly configured SMB service will provide stable and reliable file sharing functionality for network environments.

Facing the sea with spring blossoms.
Built with Hugo
Theme Stack designed by Jimmy