How to mount a shared folder under linux

Hello, how use syscall.Mount() with… credentials?
regards
Phuoc

Are you trying to mount a Samba (or Microsoft) CIFS share?

Yes, it is a CIFS share with credentials.
It works by terminal via an ssh command:
sudo mount -t cifs //servername/TEST /mnt/TEST -o username=test,password=xxx,vers=3.0

But I would like to use:
syscall.Mount(…)

I try with the help of this link (in C++ code) but I did not succeed.

Any Idea?

First, I’ve never actually done this with Go, so take that into consideration.

Generally, the easy way to solve it is to set things up so that you do not need to provide credentials in the mount command. (Aside from being inconvenient, it is insecure.) Put the username and password into a file, and make sure you have appropriate permissions set on the file.

The file’s contents are simply like this:

username=username
password=pw

Where username is the user name of a user that has access to the share, and pw is the SMB password for that user.

Then add an entry to your /etc/fstab on the client to mount the CIFS share. It will look something like this:

//172.16.21.1/share1 /share1 cifs credentials=/home/jay/.smb_auth,uid=1000,gid=1000,iocharset=utf8 0 0

This is similar to how I mount my LAN’s CIFS shares on Linux systems. The file server has the IP address 172.16.21.1, and the CIFS share called “share1” is mounted as /share1 on the client. The username and password are in /home/jay/.smb_auth. I also set the UID and GID of the mounted files here.

The general format is:

//<server_ip_addr>/<cifs_share_name> <local_mount_point> cifs credentials=<pw_file>,uid=<UID>,gid=<GID>,iocharset=<charset> 0 0

From there, just using the command

sudo mount /share1

is sufficient to mount the share. Actually, you may need to add noauto to your mount options if you don’t want the client to automatically mount the share when the system boots. Or you can mount it automatically at boot time, and (assuming things go well) already have the share mounted before your Go program starts.

Things don’t always go well when mounting CIFS shares during boot time, so in your Go program, you may want to check that the share is available, and mount it if necessary.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.