2

I have this code in which I have to access a large number of dirs, some of them are network shares, looking for specific files.

In windows, I just do "net use //server/share /user:username password" and voilá! I can can go to the dir as if it was in my local machine.

How can I do the same in linux? I know that I can mount the share to a specific folder in my machine but that would imply managing a lot of dirs and I would really like to avoid that. Just like when I do it in windows.

Thanks in advance!

1 Answer 1

1

Install smbfs:

sudo apt-get install smbfs

OR alternatively you can use cifs-utils:

sudo apt-get install cifs-utils

Either will work, they are two different programs that implement windows shares. I've used smbfs successfully with the following steps, haven't tested it on cifs.

If you only want a temporary mount, for smbfs the command would be (all one line):

mount -t smbfs //server/share /mountdirectory -o username=share_user_name,password=share_password

Replace smbfs with cifs in this command if you've elected to use cifs-utils.

If you want these shares to mount automatically on boot:

Create a credentials file somewhere secure, name it .smbcredentials, and give it these contents:

username=share_username
password=share_password

To secure this file:

sudo chown root .smbcredentials
sudo chmod 600 .smbcredentials

Add this line to /etc/fstab (all on one line):

//server/share /mountdirectory smbfs credentials=/path/to/.smbcredentials,uid=USERSID,gid=USERSGROUPID 0 0

OR if using CIFS (again, all on one line):

//server/share /mnt/mountdirectory cifs iocharset=utf8,credentials=/home/whoever/.smbcredentials,dir_mode=0775,uid=USERID,gid=USERGROUPID 0 0

Here USERID is the id of the user that you want to have ownership of the mounted folder, and USERGROUPID is that users group id. Those can be found in /etc/passwd and /etc/group.

If you added one of those lines to your /etc/fstab file, you can now mount the share:

sudo mount -a

You should now see those shares mounted under the ownership of the local user and group you indicated in USERID and USERGROUP.

3
  • Hey. Thanks for the detailed answer. Anyway i was asking for a way to do it without having to create a /mountdirectory .. Your solution means I'd have to create, manage and eventually delete several directores. What i want is to access a network folder and read its files without the need to create those auxiliar directories. Is that possível?
    – dev.pt
    Jan 24, 2014 at 12:51
  • Yes, the answer instructs how to do so. See the command located under the heading "If you only want a temporary mount", that doesn't require the extra folder.
    – Dojo
    Jan 25, 2014 at 8:28
  • Hey, i have done this, but in my case i have to always to a manual sudo mount -a after each reboot. I think its a permissions thing.... @Dojo Jan 25, 2017 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .