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:
    26
    Trophy Points:
    48
    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.
    Code:
    sudo -s
    or
    Code:
    su -s
    Install the SMBFS package.
    Code:
    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.

    Code:
    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:
    Code:
    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.

    Code:
    nano /etc/fstab
    Now add the following line (based on the example above):
    Code:
    //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:
    Code:
    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.
    Code:
    exit
    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
    Code:
    nano .smblogin
    Now write the username and password in there and save the file.
    Code:
    username=myusername
    password=mypassword
    
    Now secure the file.
    Code:
    chmod 600 .smblogin
    You can now log back in as root. You had to log out there, because of file ownership.

    Code:
    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.

    Code:
    nano /etc/fstab
    Add the following line:
    Code:
    //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:
    Code:
    mount -a
    This should work, if not just reply :)
     

Share This Page