How to Access Raspberry Pi via Hostname and Create File Share with Samba

By default, your Raspberry Pi will not be availble to your network via its Hostname. You could access it via IP address, but that isn’t guaranteed to be static. You’ll also want to setup a file share so that you can code on your desktop machine and easily copy your code to your Pi…or work directly off the Pi via a network share.

You can do both (access via hostname and create a file share) with Samba.

Here’s how:

Assign a Unique Hostname

Via Raspbian Desktop

  • Go to Raspberry Pi Configuration -> System and set a unique hostname and click OK.

  • Click Yes to reboot the Pi

Via Command Line

1. Change /etc/hostname

sudo nano /etc/hostname

Change raspberrypi to your desired hostname

2. Change /etc/hosts

sudo nano /etc/hosts

Change raspberrypi to your desired hostname

3. Reboot

sudo reboot

Remote Desktop into Raspberry Pi

Setup Remote Desktop

Follow the instructions in my How to Remote Desktop into Raspberry Pi (Raspbian Stretch) with RealVNC post. This will allow you to remote into your machine from your Windows desktop.

Remote Desktop via Hostname

You can now remote into your Pi via hostname instead of IP. See this post for remote desktop instructions.

Create a File Share

This allows you to access your Raspberry Pi files from your desktop machine.

1. Run Update

sudo apt-get update

2. Install Samba

Open Terminal and run the following:

sudo apt install -y samba

3. Configure Samba

If on the device’s desktop:

sudo leafpad /etc/samba/smb.conf &

If on Terminal (SSH or on device)

sudo nano /etc/samba/smb.conf

4. Configure Network Hostname

Scroll down to the Global Settings section:

  • Set workgroup to the name of your workgroup or domain name
  • Change ‘wins support’ from ‘no’ to ‘yes’
workgroup = WORKGROUP
wins support = yes

5. Configure File Share

Scroll down to the bottom of the /etc/samba/smb.conf file and paste in the following:

only guest=no
create mask=0777
directory mask=0777

This will share your entire home directory. If you’d like to limit access to a subdirectory, just create a directory and change the ‘path’ variable.

Save smb.config

6. Set File Share Password

Open Terminal and execute the following to set the Samba password for the ‘pi’ user.

This password can be different than the regular Pi user password.

sudo smbpasswd -a pi

You’ll need to enter the password twice.

Restart the samba service.

sudo service smbd restart

7. Access Pi File Share from Windows

You can now access the file share you just created on your desktop machine

  • Open Windows Explorer, type in your Pi hostname and you will see the share

8. Map Network Drive to Share

I like to do this so I can easily access the files without having to type in the Hostname everytime.

  • Right click on ‘This PC’ in Windows Explorer and select ‘Map network drive…’

  • Enter path to Pi share and check ‘Connect using different credentials’

  • Select ‘Use a different account’ and enter ‘pi’ and ‘raspberry’ (or whatever you set your Samba user/password to). Check ‘Remember my credentials’ so you don’t get prompted everytime you access the share.

  • You will now be able to access the share via drive letter