Setup a Samba share on Linux via command line

Last updated on

smb

A quick and dirty guide on how to easily set up a Samba share on Linux that can be accessed from Windows PCs on the same network.

Sections

  1. Installing Samba
  2. Configuring Samba
  3. Accessing the Samba share from Windows
  4. Improve transfer speeds for Samba
  5. References

Installing Samba

Samba usually comes installed with most Linux distributions. If you do need to install it, use the following commands (which will also auto-install dependencies). On Ubuntu and other Debian-based distributions:

sudo apt install -y samba

On Arch Linux and Manjaro distributions, you need to use the following command instead:

yes | sudo pacman -S samba

Configuring Samba

After installation, you should have a default Samba configuration file in /etc/samba/ directory called smb.conf. First, make a backup copy of smb.conf (just in case), then open it in a text editor:

sudo cp /etc/samba/smb.conf /etc/samba/smd.conf.backup
sudo nano /etc/samba/smb.conf

There’s a whole lot of text in here, but it’s mostly informative/explanatory comments (that you should read). Feel free to delete all the comments and only keep the configuration options, I usually do. Here’s my configuration:

[global]
  workgroup = WORKGROUP
  server string = Samba %v
  security = user

[public]
  comment = Samba Share
  path = /path/to/share
  browseable = yes
  writeable = yes
  read only = no
  force user = ariel

The above config will allow a specific user (in this case ariel, which we’ll setup shortly) to access the share after a login prompt. First, let’s explain the options in the smb.conf file briefly:

  • Any settings under [global] apply to all shares, unless you set it differently under the specific share’s settings.
  • workgroup = is important, you’ll need to specify a Workgroup to access the share from Windows. The default is most likely WORKGROUP unless you changed it on your Windows PC. Just make sure it’s the same for all the machines you want accessing the share.
  • security = user is the default security mode for Samba and the one most compatible with Windows. You don’t really have to specify this since it’s the default, but I like to anyway.
  • [public] will be the name of the share, obviously use whatever name you’d like.
  • path = will contain the direct path to the directory you want to share.
  • browseable = yes allows unrestricted browsing all directories and files within the share.
  • writeable = yes allows write access (create/delete files) within the share.
  • read only = no is supposedly the same as writeable = yes but I use it anyway for good measure.

When you are done with the smb.conf file, save it and quit the editor. Now let’s check that the configuration is valid with the following command:

testparm

You’ll get some output here that’s self-explanatory, one of the lines should say “Loaded services file is OK” meaning your config is good.

Next we’ll need to add a user to Samba, as you’ll need to login from Windows with a username and password. Let’s assume we’re adding the user ariel to Samba so their login is required to access the share from a Windows PC. (If you want to create a new user account on Linux first, use sudo adduser followed by the username you want to create.)

Use the following command to add the user to Samba, and when prompted create a password, this will be used for login.

sudo smbpasswd -a ariel

Next we need to set the owner and group to the Samba user (if it’s not already) to ensure there’s no issues accessing it.

sudo chown -R ariel /path/to/share
sudo chgrp -R ariel /path/to/share

Finally, start the services needed and enable them to auto-run at boot. On Ubuntu/Debian, use these commands:

sudo systemctl start smbd nmbd
sudo systemctl enable smbd nmbd

On Arch/Manjaro, use these commands instead (notice neither service has the trailing d):

sudo systemctl start smb nmb
sudo systemctl enable smb nmb

Now you should be able to connect to the shared directory from other computers on your network!

Accessing the Samba share from Windows

On Windows, go to Start Menu > Run and type the following (replacing with your Linux machine’s actual IP) and hit Enter:

Windows Run

Or you can connect by hostname rather than IP.

Windows Run

You should now have the shared folder open in your Windows PC! For ease of access, pin it to Quick Access or map it as a Network Drive.

However, there MAY be an additional issue, as Windows 10 Home (but not Professional) apparently does not have the Local Security Policy settings (secpol.msc) that is required to interface with Samba. I can’t confirm this myself since I use Windows 10 Professional, but if you have issues and Windows complains about secpol.msc, go here for detailed instructions on how to fix this issue.

Improve transfer speeds for Samba

After transferring files back and forth between Windows and Linux via the Samba share, you may notice it’s extremely slow! After some googling I found some additional configuration options a company blog that claimed to improve network performance, and in my experience it works.

Add the following code (feel free to remove all the comments) to your smb.conf file under [global].

[global]
   strict allocate = Yes
   allocation roundup size = 4096
   read raw = Yes
   server signing = No
   write raw = Yes
   strict locking = No
   socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
   min receivefile size = 16384
   use sendfile = Yes
   aio read size = 16384
   aio write size = 16384

For an explanation of what these options do, check the original blog post linked above, the original code includes detailed comments for each option.