Mounting an SMB share in Linux

Discussion in 'Linux, BSD and Other OS's' started by RHochstenbach, Apr 8, 2011.

  1. RHochstenbach

    RHochstenbach Administrator

    Likes Received:
    Trophy Points:
    If you have a Windows system in your network with shares or an NAS with an SMB Server installed, you can mount it using Linux using CIFS. I'm going to demonstrate how to do this using Debian and Ubuntu, but similar steps should also work on other distributions.

    For the ease of use, make sure you are root using either sudo or su.
    sudo -s
    su -s
    Install the SMBFS package.
    apt-get install smbfs
    Create a mount point. If you want to have your drive mounted as 'John', then create a folder for that location. It's mostly mounted in /mnt or /volumes.

    cd /mnt
    mkdir john
    SMB share without login credentials
    If the SMB share does not require logins, follow these steps:

    Test if mounting works without an error message:
    mount -t cifs //system_name_or_IP_address//sharename /mnt/john -o guest,nounix,rw,noserverino,iocharset=utf8,file_mode=0777,dir_mode=0777
    You can use either the NETBIOS name, DNS name or IP address of the SMB share. The sharename is the actual shared name. Using the elements nounix and noserverino, this should prevent the CIFS input/output error message.

    You should now be able to open the share. In this case the folder /mnt/john

    If it works, you can now set this SMB share to mount each time the system boots. You need to edit your fstab file for that.

    nano /etc/fstab
    Now add the following line (based on the example above):
    //system_name_or_IP_address//sharename /mnt/john cifs guest,nounix,rw,noserverino,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
    Save the file and test it for errors using the following command:
    mount -a
    SMB share with login credentials
    If the SMB share requires login credentials, follow these steps:

    You need to enter the username and password on the command line, but for the sake of safety you can use a text file to store these credentials.

    Log out of the root account to go back to your own user account.
    Go to your home directory or whatever location you would prefer to store this file. In this example I'll store it in /home/john. Create the text file .smblogin
    nano .smblogin
    Now write the username and password in there and save the file.
    Now secure the file.
    chmod 600 .smblogin
    You can now log back in as root. You had to log out there, because of file ownership.

    mount -t cifs //system_name_or_IP_address//sharename /mnt/john -o credentials=/home/john/.smblogin,nounix,rw,noserverino,iocharset=utf8,file_mode=0777,dir_mode=0777
    Of course you need to set the path of credentials to the actual path you're using.

    If it works without an error message, you can edit your fstab to have it mount your share at boot.

    nano /etc/fstab
    Add the following line:
    //system_name_or_IP_address//sharename /mnt/john cifs credentials=/home/john/.smblogin,nounix,rw,noserverino,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
    And again, set the correct path here.

    Test it with this command:
    mount -a
    This should work, if not just reply :)

Share This Page