Linux Operating System

Mounting an SMB client in Linux

I have my desktop PC setup as an SMB server. As this PC is not always on, it is not feasible to put the mount on a client machine in fstab. It is necessary to mount the share by hand. Up to and including Kubuntu v14.04, I used the following command to mount the share:

sudo mount -t cifs //192.168.7.5/<sharename> /media/<mountpoint> -o userid=<smb username>

where:

  • //192.168.7.5/ is the ip address of the host (fictitious);
  • <sharename> is the name of the smb share on the host;
  • <mountpoint> is a directory on the client machine; and
  • <smb username> is the smb user name on the host machine.

With 16.04, mount points were no longer set under /media, but rather under /media/<user name>/<mount point>. I battled to get it to work in /media, so just created a mount point in a mnt folder in my home directory

sudo mount -t cifs //192.168.7.5/<sharename> ~/mnt/<mountpoint> -o userid=<smb username>,rw,gid=1000,uid=1000

As you can see, there are some extra parameters after -o in the above command. Using the command for 14.04, granted me read access, but not write access, as the mount command took root (via sudo) as the owner of the directory. gid=1000 and uid=1000 is the user on one of the machines (I haven't as yet been able to figure out whether it is on the host or client machines, In my case both are the same as I am the primary user on both machines). You can find these parameters by using the command id from the command line.

One problem I did find was that the permissions of any file modified via smb were set to rwxrwxrwx. This can be a bit dangerous if the file is going onto the web. You can change these permissions on the host with

chmod 644 filename

This will set the permissions to rw-r--r--

With 18.04, things changed again, with mount giving a parsing error if I used the same command as for 16.04. The only way I could fix it was by using quotation marks around the host address:

sudo mount.cifs "//192.168.7.5/<sharename>" ~/mnt/<mountpoint> -o user=<smb username>,rw,gid=1000,uid=1000

In this case, I used mount.cifs directly, rather than using mount -t cifs.

I put the mounts for the various mount points in scripts on the client machines so I don't have to type them out every time I want to access the shared directories.

(updated: 30 September 2019)