For Guest posts or some help on specific problems please leave comments or Email me from Contact Us page!.

Sunday, July 13, 2014

Quick & easy Samba Install & configuration

The time from which linux has been introduced there has been a conflict between the Windows users and Linux users as they are directly not supporting file sharing within themselves.
For this purpose we are using the Samba file and printer sharing, it integrates with CIFS tools to mount the windows partition.
So lets start with installing samba and configure it.

What you will need:
A system with Linux OS
A system with Windows OS
An Internet connection
A router so that both of the systems gets connected to each  other.

Note: This post can be followed in virtual systems too such as VMware or Virtual box.

Skill: Novice
Difficulty: Medium
Time: 20 minutes max or depending on your internet connection

Lets begin (& I hope you are familiar with few Linux commands)

1) For people with fresh installation they can move on to the second step
Lets consider you have been confused with samba4 and you have ruined your samba installation and its configuration file
Open a terminal and type

$ sudo apt-get purge samba
  it will ask you for your account password enter it and after analyzing it will ask you for uninstalling the samba, press 'y' (without quotes) and press Enter it will remove it.

2) Now you can Install the samba application but before that make sure you perform a simple command to update your header files from the repositories that is

$ sudo apt-get update
this will update your headers now perform the next command

$ sudo apt-get install samba
  this will install samba in your OS
you might see this screen if you earlier had a samba installation
samba install
just select "install the package maintainer's version" this will make a new copy of smb.conf

$ sudo apt-get install system-config-samba 
   this is a GUI utility to configure Samba.

$ sudo apt-get install cifs-utils
well this is required to mount the windows shares so ensure it is installed!!!

Sharing folder and Mounting Windows shared Folder

Now open a terminal and type
First command to make the directory (in case you don't know the meaning of the command)
$ mkdir /your/directory/for/mounting/window/share
$ sudo system-config-samba

you can also search it in your system menu by typing 'samba'
a new GUI window will open like this
System-config-samba GUI for samba configuration
  • Click on "Add share"
  • Select the path to the directory which you created/which you want to share, it will take the "Share name" automatically
  • If you want you can give a short description too, now check mark the "writable" and "visible" check boxes
  • In the "Access" tab for a small duration click on "Allow access for everyone"
  • Now click "ok"
  • Now open "Preferences" on the top click "Samba users"
  • Click on "Add user" In unix username select the user account of linux system
  • In "Windows username " enter the username of the windows account from which you want to mount the shared folder.
  • Give samba password and click on ok
  • open the access tab for the directory which you added earlier and select this user which you created just now [this will ensure that only valid users can access these folders ]

(In the above steps you have made a legal user permission for access of the files we will need them in windows while mounting the system. )

Now just go to the directory which you shared right click>properties>permissions tab at the bottom "others" change it to "Read & write" click "ok" you are good to go now

Accessing this shared folder from Windows system

  • press Win+R on your keyboard
  • in the address bar type "\\address.of.linux.system" (without quotes)

[this can be searched by opening a terminal in linux and typing

$ ifconfig
it will be generally 'eth0' see the "inet addr" something like this remember it
$ iwconfig
if you are using a wifi adapter
it is generally 'wlan0' see the "inet addr" something like this remember it

In windows when you will enter this address it will ask you some credentials these are the credentials which you made in "System-config-samba" enter those and if you entered them correctly you can access your Linux share easily

Mounting Windows shared folder in Linux 
In windows 

  • you need to first share a folder
  • Select a folder right click>properties>sharing in advanced sharing mark the check box "share this folder"
  • In permissions click "full control" apply it and close now you can see "shared" tag on it.

In linux

  • Open a terminal make a directory like we made before with mkdir  command
  • Run another command this will mount the Windows share to your specified directory

$ sudo mount -t cifs //PCname/shared_folder_name /"directory"/ -o username=windows_username,password=win_password,domain=CRAY-PC,[uid=username_id ]

for example

  • suppose the name of the windows PC is HCL and its shared folder name is myshared its username is bank_user and password is passwd123
  • in linux the username is mylinux and the folder in which the windows folder will be mounted is win-share-folder

  $ sudo mount -t cifs //HCL/myshared /mylinux/win-share-folder/ -o username=bank_user,password=passwd123

if that windows system is under some domain name you can add domain to the command.
the UID thing gives access to you to modify or add files to the remote shared file
For knowing your UID you can simply run a command
$ id -u "username" 

for example for mylinux [note if there is only one user account on linux then it probably might show UID as 1000]

$ id -u mylinux

Now you can see that the windows share has been mounted.

What if you want to unmount this folder,
simply run this command
$ sudo umount /path/to/your/directory
here for example its win-share-folder
$ sudo umount /mylinux/win-share-folder/ 

This will unmount the folder!!!

Now that you have learned to mount and unmount the folders you must be tired of writing these long commands
so lets make a simple shell script for them

Creating a shell script for mounting and unmounting the folders

  • Open a terminal and type 

$ sudo gedit mounting_srcipt
$ sudo leafpad mounting_script 

[well most of the distributions contains these two file editing tools, one can use the default Vi editor also, well here i'm mostly considering GUI so only telling about GUI editors, so if neither of two is installed please install them with "apt-get install" command]
  • it will open an empty file
  • now write
command to be executed 

for example
sudo mount -t cifs //HCL/myshared /mylinux/win-share-folder/ -o username=bank_user,password=passwd123

and save it
now from the same terminal type
$ chmod +x mounting_script

this way you will give executing permissions to this script now to run this script just type in terminal
$ ./mounting_script

Same you have to do with the unmount script do the same!!!

So guys i hope you might have successfully mounted and unmounted the shared folders plus learned a bit of shell scripting.
NOTE: If you are experiencing "denied access or permissions denied" error just check are you using the "sudo" keyword before the command its like ordering your subordinate with a higher designation. Sudo makes it possible for you when you normally can't.
Still you have any doubts or experience any problems please leave comments and i promise i will definitely help you. 

No comments:

Post a Comment