Mounting samba shares from a unix client
Namespaces
Page actions
Contents
Mounting samba shares from a unix client
General description
Using a share from a samba server within a unix filesystem depends on a lot of single components. You need at least the smbfs (which is no longer maintained) or the modern cifs kernel modules. Although older documentation says to use the smbfs, it has many restrictions and the cifs should be favoured. That said, I’ll describe only the cifs module in this document.
Since the cifs filesystem is included in the standard Linux kernel, it is simple to build it either as module or built in. If you build the cifs module (e.g. to get a newer version with bugfixes) and do not install it in the default location, then you may need to load it prior to mount a share or let modprobe load it via modprobe.conf in order to get the newer cifs module rather than the one shipped with your distribution.
The basic mount command is mount -t cifs // / . Each component needs its own attention.
The mount utility calls a mount helper, usually mount.cifs which calls into the kernel. The mount helper mount.cifs is the user space helper and needed to parse tcp/ip names and retrieve userid and password, and also does simple formatting of the mount options.
Server’s listen for incoming client connections via TCP/IP and thus have ip addresses, and usually tcp host names configured for them, but users often refer to the server by its «netbios name» (RFC1001 name). To mount using the cifs client, a tcp name (rather than netbios name) must be specified for the server. To resolve the to a ip address, you need either a DNS server which knows the ip address or your client needs the nss module wins. It is a shared library which must be in the path of your ldd. Usually under /usr/lib. You also have to add the wins option to hosts in your /etc/nsswitch.conf. The smbclient utility can also be used to identify the tcp name or ip address of a server (identified by its netbios name).
mountpoint must be a directory elsewhere in the filesystem which must exist.
Common mount.cifs options
Now an explaination of the options for mount -t cifs as described in man mount.cifs mount.cifs takes a lot of options, some of them are really confusing. I’ll try to mention the most important here.
- user= or username= : Thats the user which is used to authenticate against the server. If this is not given, then the environment variable USER is used. This option can also take the form «user%password» or «workgroup/user» or «workgroup/user%password» to allow the password and workgroup to be specified as part of the username.
- pass=
: specifies the CIFS password. If this option is not given then the environment variable PASSWD is used. If the password is not specified directly or indirectly via an argument to mount mount.cifs will prompt for a password, unless the guest option is specified.
- uid= : sets the uid that will own all files on the mounted filesystem. It may be specified as either a username or a numeric uid. This parameter is ignored when the target server supports the CIFS Unix extensions (which samba does per default unless you disable it).
- gid= : sets the gid that will own all files on the mounted filesystem. It may be specified as either a groupname or a numeric gid. This parameter is ignored when the target server supports the CIFS Unix extensions.
- domain= : sets the domain (workgroup) of the user
- ip= : sets the destination host or IP address.
- guest: mount the share as guest and don’t prompt for a password.
There are other options but they are not that important to be usefull beside performance and really specific things.
NetBios name resolution with WINS
All networking programs at some point need to resolve a host name to an IP-Address. Usually this is done with a call to the function gethostbyname. This call is a library function and thus is handled in user space. Glibc systems such as Linux have the feature of using more than one lookup method to fulfill such a query. This is known as nsswitch (Name Service Switch). The config file is usually found in /etc/nsswitch.conf.
If a program wants to resolve the ip address of a host named ‘wiki’ it simply calls the function gethostbyname. The lookups done are the responsibility of the underlying glibc then. Glibc reads the /etc/nsswitch.conf and loads the library mentioned in the hosts line. Thats the key point: if there is a wins entry, glibc loads the libnss_wins.so which enables the NetBios Name Lookup. You got it then, you can resolve a Windows Name to an ip address.
So its simple: install the libnss_wins.so, add to your /etc/nsswitch.conf at line hosts the wins entry and the windows name resolution works. Your network setup should be ok for this, but thats not a matter of samba.
Step by Step list to get it working
Lets assume you have one of those ARM powered NAS boxes such as Buffalo et al make under your TV set and you want to access it from a desktop Linux box somewhere in your house.
You will need the IP address of said box! Often you can find this out via the web interface of your router.
As root edit /etc/fstab using your favourite editor
e.g. sudo gedit /etc/fstab
At the end of the file add a line that looks like this:-
If you never set any users up on the NAS then it would look like this
Here is what one of my entries looks like
Now when I browse over to /mnt/oneterra I see all my files on my 1TB NAS! If I had left out the uid=myloginname I would only have read-only access, the _netdev is to tell Linux to wait for the network to be up before attempting the mount
For more try this link [1]
TODO: make a step by step list to solve most of the problems
Here’s the verbatim commands I used (SuSE 10.0 to Buffalo Link Station). Notice I knew the IP address, in my case the SMB name didn’t work and I didn’t bother making the Network Name «NetBios Name» Work as suggested above.
Mounting shares at logon time
If you want to automatically mount shares at user logon time the only way I found is doing it with the pam_mount module. It is the only way to do an authenticated mount without requiring the user to store the password in a file or entering it on a terminal.
How to Mount Samba Share in Ubuntu Linux
In this tutorial we are going to learn how to mount samba share on Ubuntu.
For this tutorial I am using Ubuntu Server 16.04, But you can use the following guide to mount Samba CIFS Share in any version of Ubuntu Linux including Ubuntu Desktop.
Install cifs-utils package in Ubuntu
In order to mount Samba share on Ubuntu we need to install the cifs-utils package.
The cifs-utils package provides the tools and utilities that need to mount samba share on Ubuntu Linux.
Mount Samba Share using the Mount Command
After installing cifs-utils package, we can use mount command to mount samba share instantly.
Once you execute the Command you will prompt for the Samba password of the samba user.
- The Server name can be the domain name or the IP Address of the Samba Server.
- It is also possible to provide the password with the mount command.
As per the above example, I used the mount command to mount samba share called documents from the Server 192.168.1.100. The mount point is /mnt and samba user is smbuser.
This time we also provided the password for the smbuser as a command option.
Add Samba Share to /etc/fstab
To mount samba share automatically when system reboot, We need to add an entry to the /etc/fstab file.
When system reboot, Ubuntu will mount the samba share specified in the /etc/fstab file.
To check the fstab file without reboot, use the mount -a command.
The mount -a command will Mount all filesystems mentioned in the fstab file.
Authenticate Samba Share using Credential File
We can use a credentials file that contains a username and password to authenticate the samba User.
First, we need to create the credentials file.
Next we need to add the username and password to the credentials file as follows.
Then we can use the credentials file to mount Samba Share on Ubuntu Server.
Mount using the mount Command
The fstab entry should be as follows.
So that is how we can mount samba share on Ubuntu Linux using the CIFS Protocol.
about anything near me
четверг, 17 марта 2016 г.
Монтирование Samba-ресурсов в Linux (Ubuntu)
Часто бывает нужно смонтировать виндовый расшаренный ресурс на линуксовый сервер с использованием конкретного пользователя и пароля (как сетевой диск в Windows).
Обычный smbclient не всегда подходит, поэтому, в таком случае нам поможет cifs.
Для начала нужно установить пакет cifs-utils:
$ sudo apt-get install cifs-utils
Теперь монтируем ресурсы командой:
$ sudo mount -t cifs //192.168.0.60/shara /mnt/smb/ -o user=IvanovAA
или чуть более расширенный вариант:
$ sudo mount -t cifs -o username=user,password=passwd,iocharset=utf8,file_mode=0777,dir_mode=0777 //network/resource /mount/point
Где «user» и «passwd» — имя пользователя и пароль для подключения к расшаренному ресурсу, а «//network/resource» и «/mount/point» — сетевой путь и точка монтирования соответственно.
Для того, что бы сетевой ресурс монтировался автоматически при включении системы, можно добавить эту запись в файл /etc/fstab в следующем виде:
//192.168.0.1/backup /backup cifs username=user,password=passwd,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
Что бы не хранить имя пользователя и пароль в открытом виде в файле fstab, можно записать эти параметры в файл и ограничить права на его чтение:
$ sudo touch /root/.smbcredentials
$ sudo chmod 600 /root/.smbcredentials
$ sudo echo «username=user» >> /root/.smbcredentials
$ sudo echo «password=passwd» >> /root/.smbcredentials
Где «user» и «passwd» конечно же ваши логин и пароль для подключения к сетевому ресурсу.
После этого в файл /etc/fstab вместо строки:
//192.168.0.1/backup /backup cifs username=user,password=passwd,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
пишем:
//192.168.0.1/backup /backup cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0