Raspberry Pi Remote File Access Using Samba

This article covers how to transfer and access remote files on a Raspberry Pi using Samba.

Step 1. Get to the command line

Remote login to the Raspberry Pi over ssh or open up a terminal window via the desktop.

Step 2. Get the latest updates

Get the latest updates by running this command:

$ sudo apt-get update

Step 3. Install Samba

Run this command to install Samba:

$ sudo apt-get install samba samba-common-bin

You may get prompted with a dialog box that asks:

Modify smb.conf to use WINS settings from DHCP?

Select Yes.

Check the service status

Check the status of the service to confirm that it was installed and is working:

$ sudo /etc/init.d/smbd status

Look for lines like this to confirm that things are working:

Active: active (running) since (DATE); 3min 21s ago
Status: "smbd: ready to serve connections..."

Step 4. Configure Samba for read and write access

To configure Samba for read and write access, open up its configuration file using the nano text editor:

$ sudo nano /etc/samba/smb.conf 

Find the line that says read only and set it equal to no.

Step 5. Create a Samba password

Create a Samba password for remote access as user pi:

$ sudo smbpasswd -a pi

Step 6. Restart the Samba server

Restart the Samba server using this command:

$ sudo /etc/init.d/smbd restart

Check the status to confirm that there wasn’t an issue with the config file:

$ sudo /etc/init.d/smbd status

Step 7. Create a test file

Create a test file on your Raspberry Pi:

$ echo "This is a test." >> ~/test.txt

Confirm that the file was created:

$ cat ~/test.txt

Once a remote connection is setup, you can look for this file to verify that you have access.

Step 8. Connect to the Pi via Finder (Mac)

First I’m going to show you how to connect to the remote Pi on a Mac using Finder.

Later I will show you how to connect using the command line.

Mac Instructions

  • Launch Finder
  • Select from the main menu Go / Connect to Server
  • Enter this address (substituting pi4 for for your pi hostname):
    smb://pi4.local
    
  • Click Connect
  • When prompted, remember to enter your Pi username (i.e. pi) and the Samba share password
  • For volume selection, you may have only one option (pi)
  • Click OK

The new volume should now appear in Finder where you can see the test file.

You should now be able to access and transfer files to and from the Pi from your Mac.

You can do that either through Finder or from the command line.

Step 9. Access the volume from the command line

In this step I’m going to show you some of the ways that you can access the remote volume using the command line.

List the volumes

When you connect using Finder, by default it adds the mount point to the /Volumes folder.

You can see the new mount point using the ls command:

$ ls /Volumes/
Macintosh HD	pi

In the example above, the pi volume is listed.

List all mount points

You can list all of the mount points on your Mac using the df command:

$ df

The mount point for the Pi will probably be the last (newest) entry in the result:

Filesystem          512-blocks      Used Available Capacity iused      ifree %iused  Mounted on
...
//pi@pi4.local/pi  122233368   7864672 114368696     7% 3932334   57184348    6%   /Volumes/pi

List the files on the remote volume

To list the files on the remote (pi) volume, use this command:

$ ls /Volumes/pi

You should see the test file that you created on the Pi:

test.txt

Read a file on the remote volume

Verify that you can read the test file contents using the cat command:

$ cat /Volumes/pi/test.txt

Create a file on the remote volume

Verify that you can create a new file on the remote volume and write to it:

$ echo "This is another test." >> /Volumes/pi/test2.txt
$ cat /Volumes/pi/test2.txt

The result should be the line that you just echoed to the new file.

Copy a file from the remote volume

Verify that you can copy files from the remote volume using the cp command:

$ cp /Volumes/pi/test.txt ~/mytest.txt
$ cat ~/mytest.txt

Unmount the remote volume folder

To disconnect from a remote volume, use the umount command.

umount /Volumes/pi

Connect via the command line

To connect via the command line you need to do the following:

  • Verify that no other folder is currently mounted to the remote Pi (or you will get an error)
  • Make a new folder
  • Mount the remote Pi to the folder using the mount_smbfs command
  • List the contents of the new folder to verify that you have access

Here is an example of mounting a remote Pi to a folder called mypi on a Mac.

Substitute YOUR-HOSTNAME in the command below with your Pi hostname.

Substitute YOUR-PASSWORD with the Samba password defined in a previous step:

$ mkdir ~/mypi
$ mount_smbfs //pi:YOUR-PASSWORD@YOUR-HOSTNAME.local/pi ~/mypi
$ ls -ls mypi

To test it, create another file using echo and dump its contents to the console using cat:

$ echo "Yet another test." >> ~/mypi/test3.txt
$ cat ~/mypi/test3.txt

Confirm on the pi that all three test files have been created:

ls -ls ~

Troubleshooting

Unable to connect

If you can’t connect, make sure that your laptop and pi are on the same network.

File exists error

What does it mean if mount_smbfs returns a File exists error like this?

mount_smbfs: mount error: /Users/mitch/pi2: File exists

What this confusing error actually means is that the remote folder has already been mounted.

You can’t mount the same folder in two locations.

No such file or directory error

What does it mean if mount_smbfs returns a No such file or directory error like this?

mount_smbfs: could not find mount point /Users/mitch/pi2: No such file or directory

That means that you need to create the folder (in this case /Users/mitch/pi2) before you can mount the remote Pi to it.

Conclusion

In this article you learned how to:

  • Install Samba on a Raspberry Pi
  • Mount a Raspberry Pi remote volume and folder
  • Access files on a remote Pi using Finder
  • Access files on a remote Pi using the command line

References

  • samba.org [1]



About the Author

Mitch Allen works for a robotics company in New England.