Mounting remote filesystems with smbfs
Posted by Steve on Fri 17 Jun 2005 at 11:43
Previously we've covered mounting remote filesystems using OpenSSH which works very well for most Unix servers. But when it comes to remotely mounting filesystems from Windows servers, or desktops, there's only one choice. The Samba file system module, smbfs.
Much like mounting remote filesystems with OpenSSH this involves the use of a kernel module, but unlike shfs there's no need to build this from source yourself.
The Debian kernel packages all come with the smbfs module built already, although if you compile your own kernel from source you might need to enable it yourself.
Samba is a very well known project in the Linux world, it allows you to do a lot of different things but most noticably:
- Communicate with a Microsoft Windows fileserver, domain server, or printer server.
- Impersonate a Microsoft Windows fileserver, domain server, or printer server.
Both the K desktop environment (KDE), and the GNOME desktop allow you to explore remote Samba shares, or Microsoft windows shares. However there are times when you cannot use these - for example if you wished to backup a remote machine from a shell script.
For these jobs the Samba File System kernel module, and tools, are a much better fit.
To mount a remote system directly upon your current system you'll need:
- The smbfs kernel module
- The smbfs package.
- A mountpoint - this is just an empty directory.
- The Name/IP address of the host to connect to.
- Login credentials to the remote share - both username and password.
- Local root privileges to do the mount, and load the kernel module.
If you don't already have the smbfs package installed you'll need to install it:
root@mystery:~# apt-get install smbfs
When this is installed you'll also get the samba-common package. If you're not already running a Samba server the installation of this package will ask a couple of questions about your workgroup name, etc. You may safely ignore those questions and just accept the defaults.
Once you have the smbfs package installed you can begin testing things.
As a small example lets assume I wish to mount the "C:" drive of remote machine "recurring.my.flat" to the local machine mystery.my.flat I would begin by loading the kernel module:
root@mystery:~# modprobe smbfs
You can cause this module to be loaded automatically as the machine boots by running:
root@mystery:~# echo 'smbfs' >> /etc/modules
Once the module is loaded I'll create a new directory to be the mount-point:
root@mystery:~# mkdir -p /mnt/recurring
This directory will be where all the remote files will be visible. To actually perform the mounting we have to run:
root@mystery:~# mount -t smbfs -o username=Administrator //recurring/c$ /mnt/recurring
If this command is successful you'll be prompted for the password to make the connection. To avoid this you can also specify the password for the connection on the command line as this example shows:
root@mystery:~# mount -t smbfs -o username=Administrator,password=Password //recurring/c$ /mnt/recurring
An invalid password, or lack of credentials will give you a response similar to this:
22077: session setup failed: ERRDOS - ERRnoaccess (Access denied.) SMB connection failed
If you receive an error message which is unrelated to authentication you should check the end of the "dmesg" output, by running:
root@mystery:~# dmesg | tail
If the output includes the following text then you're missing the smbfs package:
smbfs: mount_data version 1919251317 is not supported
Once you've successfully mounted the remote system you can explore from the comfort of your Debian machine:
root@mystery:~# cd /mnt/recurring/ root@mystery:/mnt/recurring# ls arcldr.exe Images System Volume Information arcsetup.exe IO.SYS six.avi AUTOEXEC.BAT MSDOS.SYS Untitled.zip boot.ini MyWorks Untitled.upd cat.zip NTDETECT.COM Utilities CONFIG.SYS ntldr Videos Documents and Settings pagefile.sys WINNT putty.exe Program Files WUTemp
You can also mount the filesystem via /etc/fstab - but note this will include your password in plain text to any local user upon the system:
//recurring/c$ /mnt/recurring smbfs defaults,user,noauto,username=Administrator,password=Password 0 0
To protect your passwords you can instead save them in a file, and pass that along instead - for example you could create the file /etc/recurring.smbpass which contains the username and password to use for the connection:
root@mystery:~# cat >/etc/recurring.smbpass <<EOF username=Administrator password=password EOF
By changing the permissions this can then be read only by the root user:
root@mystery:~# chmod 600 /etc/recurring.smbpass root@mystery:~# chown root.root /etc/recurring.smbpass
With the login details safely secured you can now mount the remote system using them :
root@mystery:/mnt# mount -t smbfs //recurring/c$ /mnt/recurring -o credentials=/etc/recurring.smbpass
Similarly you can adjust the mount line in /etc/fstab to read:
//recurring/c$ /mnt/recurring smbfs defaults,credentials=/etc/recurring.smbpass 0 0
This will cause the mount to be loaded whenever your machine boots - or if you run as root:
root@mystery:~# mount /mnt/recurring