# Samba

File sharing, copying and sync

# Map Network drive

First, Need to make sure you have **Samba** and **smbclient** and, **Cifs-utils**. Install them.

##### create a mount point (if you haven't already)  


`sudo mkdir /directr/to/moun/to`

##### Mount the volume

sudo mount -t cifs //\[addressofshare\]/\[folderofshare\] -o usernam=ofshare /\[localdirectorytomountto\]   
Exaple:

```bash
sudo mount -t cifs //192.168.3.456/smbshare -o username=bob /mnt/remote
```

Then, when asked for password, type in password of the share folder that is hosting the share you are trying to access, not your local machine.

Make it permanent by going into the fstab

```
vim /etc/fstab
```

Line 2 is what you need to enter it (change it to your own of course).

```
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
//192.168.3.456/smbshare /mnt/remote cifs username=bob,passowrd=eatit 0 0

```

Test to see if it mount worked

```
umount /storage/location
```

```
mount -a
```

#### [Troubleshooing](https://wiki.danicus.net/books/samba/page/troubleshooting-samba-cifs-share-permissions "Troubleshooting Samba / Cifs share Permissions")

# Mount drives

If disk is already formatted, You can mount it. You may need to create a file system for it though.

If you do, Replace *`XY`* accordingly, but double check that you are specifying the correct partition

```bash
mkfs.ext4 /dev/sdXY
```

Do that to all the drives/partitions you need to.

Now you should be able to go to "Disk manager" in the GUI or, mount the drive/s manually.

## To mount in Terminal:

##### create a mount point (if you haven't already)  


```bash
sudo mkdir /directry/to/mount/to
```

Now mount it:

```
mount /dev/sbxy /new/mounted/location
```

Check your file type before you continue

```bash
lsblk -f 
```

If you want it permanent, you will need to add it to the fstab.

##### Mounting from fstab

```bash
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# /this uses the UUID of the drive and mounts it do the directory you want it to go to
UUID=57sdfgsdf4jk-f2nh-49u8-9596-719a26547f94 /mount/dir  ext4    errors=remount-ro 0 0

# /this uses the drive location and mounts it to the directory 
/dev/sdb /mount/dir  ext4    errors=remount-ro 0 0
```

Type "lsblk" is your drive mounted?

Type "sudo mount -a" How about now?   
Try "lsblk" again.

# Samba Share Setup

Make sure you have a directory set up that you are going to be sharing. If it is going to be a new one, create that directory where you need it, on the local machine that the data will live. /home/the/share/file/location or something like that.

If it already exists, just keep that in mind for when its needed in the config file.

##### Now, make that directory, unless you already have one in mind. 

```bash
mkdir /home/the/share/file/location
```

##### Go to the config file:

I'm using "vim". Use whatever you want.

```bash
sudo vim /etc/samba/smb.conf
```

##### Inside the config file, add the following:

```bash
[sambashare_file_name]
	comment = Samba on Ubuntu
    path = /home/the/share/file/location
    read only = no
    browsable = yes    
```

##### Restart Samba:

```bash
sudo service smbd restart
```

or

```bash
sudo systemctl restart smbd
```

##### You prob have a firewall so, fix those settings now. 

For UFW:

```bash
sudo ufw allow samaba
```

# Samba Users

Since Samba doesn't use the system account password, we need to set up a Samba password for our user account

```bash
sudo smbpasswd -a username
```

#### Note:  


The user name must belong to a system account or else it won't work.   
The password is the part that will change to its own Samba password.

# Troubleshooting Samba / Cifs share Permissions

#### Cifs share doesn't preserve permission configuration in smb.conf (755 instead of 777)

There are two options that can help 'save the day': dir\_mode and file\_mode. (placeholders in green italics)   
Setting both these to 0777 gives full permissions that the host smb server allows for the user in question.

```c++
sudo mount -t cifs -o rw,username=yourusername,dir_mode=0777,file_mode=0777 //share/directory ~/mnt/local/directory
```

Or if it s in your fstab file:

```
//192.168.1.234/Sharename /mnt/local/directory cifs username=yourusername,dir_mode=0777,file_mode=0777,password=yourpassword 0   0
```

Sources:   
[https://www.linuxquestions.org/questions/linux-newbie-8/permission-denied-on-file-operations-on-mounted-cifs-share-4175661393/](https://www.linuxquestions.org/questions/linux-newbie-8/permission-denied-on-file-operations-on-mounted-cifs-share-4175661393/)

[https://superuser.com/questions/784362/cifs-share-doesnt-preserve-permission-configuration-in-smb-conf-775-instead-of](https://superuser.com/questions/784362/cifs-share-doesnt-preserve-permission-configuration-in-smb-conf-775-instead-of)