Software & AppsOperating SystemLinux

Creating a Samba Share via Command Line on Ubuntu

Ubuntu 17

In this tutorial, we will be going through the process of creating a Samba share via the command line on Ubuntu. Samba is a powerful tool that allows Ubuntu systems to share files with Windows computers and vice versa.

Quick Answer

Creating a Samba share via the command line on Ubuntu can be done using either the net usershare command or by manually editing the smb.conf file. Both methods have their advantages and can be used based on your specific requirements.

What is Samba?

Samba is an open-source software suite that uses the SMB/CIFS protocol to provide seamless file and print services to SMB/CIFS clients. It enables interoperability between Linux/Unix servers and Windows-based clients.

Prerequisites

Before we begin, ensure that you have the following:

  • An Ubuntu system
  • Sudo or root access to your system
  • Samba installed on your system. If not, you can install it using the command sudo apt-get install samba

Creating a Samba Share

There are two methods to create a Samba share from the command line on Ubuntu: using Samba’s net usershare command or manually editing the smb.conf file.

Method 1: Using Samba’s net usershare Command

The net usershare command allows you to share directories from the command line. This method is considered less clunky and safer than manually editing the smb.conf file. Here’s an example command to create a share:

net usershare add share_name /home/user/somefolder "Share description" everyone:F guest_ok=y

In this command:

  • share_name is the name of the share that will be created.
  • /home/user/somefolder is the directory that will be shared.
  • "Share description" is a description of the share.
  • everyone:F grants full access to everyone.
  • guest_ok=y allows guest access.

To make the share permanent and persist over reboots, you can create a file for each share under /var/lib/samba/usershares/ using the following command:

net usershare info --long share_name > /var/lib/samba/usershares/share_name

This method has the advantage that the shared directories will show up as shared in file managers like Dolphin or Nautilus.

Method 2: Manually Editing the smb.conf File

The smb.conf file located at /etc/samba/smb.conf is the main configuration file for the Samba server. In this file, you can enter the share name, location, and specify who should have access to it. There are several examples in the file itself to guide you.

Here’s an example of how to add a share to the smb.conf file:

[some_share]
 path = /home/some_user/some_folder
 browseable = yes
 read only = no
 guest ok = yes

In this example:

  • [some_share] is the name of the share.
  • path is the directory that will be shared.
  • browseable set to yes means the share will be visible to the network.
  • read only set to no allows write access to the share.
  • guest ok set to yes allows guest access.

After editing the smb.conf file, you should restart the Samba server to apply the changes using the following command:

sudo service smbd restart

Conclusion

Creating a Samba share via the command line on Ubuntu can be done in two ways: using the net usershare command or manually editing the smb.conf file. Both methods have their advantages and can be used based on your specific requirements.

Remember to use sudo if necessary to execute these commands with administrative privileges. Always be careful when editing configuration files and ensure you have a backup in case something goes wrong.

For more information on Samba and its configuration, you can visit the official Samba documentation here.

How do I check if Samba is installed on my Ubuntu system?

You can check if Samba is installed on your Ubuntu system by running the command sudo apt list --installed | grep samba. If Samba is installed, you will see its package listed in the output.

How can I install Samba on Ubuntu?

You can install Samba on Ubuntu by running the command sudo apt-get install samba. This will install the Samba package and its dependencies on your system.

How do I create a Samba share using the `net usershare` command?

To create a Samba share using the net usershare command, you can use the following syntax: net usershare add [share_name] [directory_path] "[share_description]" [permissions]. Replace the placeholders with the appropriate values for your share.

How do I create a Samba share by manually editing the `smb.conf` file?

To create a Samba share by manually editing the smb.conf file, you need to open the file using a text editor with root privileges (sudo nano /etc/samba/smb.conf). Then, add a new section with the share details, following the format mentioned in the tutorial.

How do I make a Samba share permanent and persist over reboots?

To make a Samba share permanent, you can create a file for each share under /var/lib/samba/usershares/. Use the net usershare info --long [share_name] > /var/lib/samba/usershares/[share_name] command to create the file.

How do I restart the Samba server to apply changes?

You can restart the Samba server to apply changes by running the command sudo service smbd restart. This will restart the Samba service and apply any modifications made to the smb.conf file.

Where can I find more information on Samba and its configuration?

You can find more information on Samba and its configuration in the official Samba documentation, which can be accessed here. The documentation provides detailed explanations and examples for various Samba configurations.

Leave a Comment

Your email address will not be published. Required fields are marked *