Wednesday, March 14, 2012

Setting Up A Linux File Server Using Samba


A quick Google will show many ways to to setup a Linux file server running Samba, most of them however don't work! Some leave out important bits leaving you stuck and some will only work with one version of a specific Distro (but of course don't mention this). I struggled for ages getting Samba to work reliably and made quite a few wrong turns on the way. I was just trying to set up a simple Linux file server to store music, photos etc. but eventually found a foolproof (probably) way to do it. The following works and has been tested several times on fresh installations. This is not meant to be a high security setup, all folders are accessible to everybody for read, write and delete. If you have stroppy teenagers who want exclusive access to their own area on the server, then you can use this as a starting point. A few simple changes would achieve that level of security but it is beyond the scope of this tutorial.
You could of course setup a Windows box running file sharing but there are some good reasons not to. Windows would need constant monitoring to ensure updates are installed (requiring lots of reboots). Windows is very vulnerable to viruses so your virus software will need kept up to date. Windows has no option to setup folder security so if you find you need it in the future you'll have to start again from scratch, Windows is not as stable. I setup a Linux server in 2007 and it ran non stop for 3 years. I didn't have to reboot it even when the Windows PC's got viruses. In fact the Windows files stored on the server were infected with the virus but the Linux operating system was unaffected. Ubuntu Server can be installed on any old PC you have lying around, even a 486. There are other reasons too but enough talk, lets get started, if all goes well this should take no more than an hour or two.
NB The following Tutorial assumes you have a working network with a router providing a connection to the Internet for both your server and client PC's.

Step1

Download Ubuntu Server and create the boot CD, then download the corresponding Ubuntu Server Guide from https://help.ubuntu.com/. I  will be referring to this guide a lot so print it off or keep it open on another PC.
** It is important you use the guide written for your version of Ubuntu Server as some commands change between versions (this was one of the issues I had with other tutorials). This may also cause problems if you upgrade to a new version of Ubuntu so I suggest you use the most recent LTS (long term support) version and stick to it.
Boot off the CD and follow installation instructions. I selected Guided - Use entire Disk. I did not select LVM as I had issues retrieving data from an earlier attempt, where I screwed up the interfaces file and it wouldn't boot (yes I know you can mount LVM from a Live Disk but I can't be bothered with the hassle) this is a simple home server so I like to keep everything as simple as possible.
Give your server a name, create a user and set a sensible password. (this will also be your admin password so it should include capitals and symbols or numbers), I'll explain more about this later.
**** NB When creating the user make it something like serveradmin. Do not make this username the same as your Windows username. If you do it may cause issues later.****
Select Samba and SSH during the installation procedure by moving up and down with the arrow keys and pressing the space bar. Use the Tab key to skip to the next or back buttons. Now continue with the installation and in a few minutes you will be presented with the Ubuntu Server login screen. If at this stage you were expecting a graphical user interface with shiny buttons and a mouse then you will be disappointed. This can be installed if you really wish but that defeats the purpose of using the server edition of Ubuntu. On this version of Ubuntu you will have to get your hands dirty on the keyboard.

Step 2

Login with your user name and password.
Install updates :-
sudo apt-get update
This just ensures everything is up to date.
You should be prompted for a password. Use the one you created earlier.

Step 3

In the Ubuntu Server Guide you downloaded earlier look for the chapter on Windows Networking and follow the instructions for Samba File Server.
NB You shouldn't need to run the install command for Samba as it was installed earlier but if you have run it and are being asked which version of smb.conf you want, I suggest you keep the one already installed. This is especially true if you have already made changes to it.
** Before you start editing the smb.conf file, copy it to something like smb.conf_backup. You will probably cock things up the first time you attempt this so it is nice to be able to get back to square one. :-
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup
I use Nano for editing text files from the CLI (command line interface). I hate Vi with a passion, that may be cause I'm a bit thick, but I find Nano much easier to use. So your command to edit the smb.conf file will be :-
sudo nano /etc/samba/smb.conf
You need to be an admin user to save any changes when you're finished editing, hence the sudo in front of nano.
In Ubuntu any command which may change the server configuration needs to be carried out by the admin user. Since for security reasons you cannot login as root (the admin user) the alternative is to insert sudo before every command then provide the admin password, this keeps things nice and secure. Other Distros allow you to type su (for super user) then the password and this will give admin access until you logout.
Check the workgroup name in your Windows PC by right clicking on My Computer, select properties and Computer name. It will probably be WORKGROUP or MSHOME, depending on your version of XP. Sorry not sure how to find out in Vista or W7 but probably something similar.
Now follow the instructions carefully. Some of the commands are already in the smb.conf file but have a # symbol in front of them, removing the # symbol makes the command active.
To close Nano and save your changes press CTRL and X, type y and press enter. CTRL and O is shown as Write out which just means SAVE, if you want to save it as you go along.
NB. Pretty much everything in Linux is case sensitive, i.e. If you gave your folder names capitals when creating, them then you MUST use capitals when referring to them or moving around the file structure. Linux will see Music and music as two completely different folders.

Step 4

This bit may or may not be necessary, as far as I can see it depends on the version of Ubuntu Server and whether or not you installed Samba as part of the server installation. To connect from either a Linux or Windows PC you need to make one more small change. Look for the area in smb.conf marked Authentication, and just below the bit where you set security to user, change:-
encrypt passwords = false 
to
encrypt passwords = true
Save it, restart Samba by the method suggested for your version of Ubuntu Server (it seems to change between versions) or just reboot the server and you should done.

Step 5

Now to the bit nobody else mentions in their tutorials and I had to figure out for myself. How do I connect to the bugger?!!! I tried browsing "Network Connections" but that didn't work, possibly because at this point we haven't created Samba users to match our Windows users but anyhow we don't need to. Here is how I did it.
Note you experts out there may criticize my way of doing this but it's simple and it works so don't knock it.
If not sure of your server IP address just type ifconfig at the command prompt. This will give you a full breakdown of your network details.
From a Win XP Pro machine (sorry not sure it this is the same for other versions of Windows but it should be similar).
Open My Computer.
Click on Tools and Map network Drive.
In the window labeled Folder, enter the IP address of your server preceded by \\ and followed by \sharename (where sharename is the name of the share you created earlier).
e.g. :- \\192.168.1.4\Music or \\192.168.0.5\Photos
Press Finish and if everything has worked you should now see the network folder. You can create a shortcut to it for easy access in the future but Windows should remember the network drive name you just created.
To connect from a Linux PC:- (I am using Ubuntu 10.10, sorry not sure about other Distros but other versions of Ubuntu should be similar)
Click on Places and select Connect to Server.
Select Windows Share and type in the server IP address in the window labeled Server.
Enter the share name you created, put a tick in create bookmark (this just makes Ubuntu remember the path after logout or reboot) and click on connect. Do not enter any username or password, it shouldn't be necessary if you followed the guide earlier.
And that's it. Yippee you now have a working file server.

Step 5

During installation you installed SSH and having SSH running on your server in default mode can be a security risk, so we want to sort that. We need to change the default port to a random number of your own choosing. To do this follow the instructions in the Ubuntu Server Guide under the chapter Remote Administration. The default port is 22 which many people don't bother changing so this is the one used by hackers and hacking software to try and gain access to your server. There are a few other options there but I haven't bothered with them.

Step 6

Unless you chose to give your server a static IP address during installation it will have a DHCP address assigned to it by your router. This would be fine so long as you never reboot your router or shutdown the server. Sooner or later though it will pick up a different IP address and you will be left disconnected, so we will give it a static one. Check the current IP address by typing
ifconfig
at the command prompt. You only want to change the last part so for example if it is 192.168.1.5 then change it to 192.168.1.101. You can set the last part to be any number between 2 and 254, mine is 101.
In the Ubuntu Server Guide open the chapter Networking – Network Configuration - IP addressing and select Static IP Address Assignment.
sudo nano /etc/network/interfaces
It should look something like
auto eth0
iface eth0 inet static
  address 192.168.1.101
  netmask 255.255.255.0 
  gateway 192.168.1.1
The gateway is normally the IP address of your router which is usually 192.168.1.1 or 192.168.0.1 but may be something completely different. If you ran
ipconfig
on your Windows PC it should have shown you the gateway address. If in doubt check your manual or Google the model of your router.
You might also want to check the IP address of your Windows PC, it should be similar to the server but it may previously have been set to a static address.
Run
cmd
from the Start button (or Start and Run in XP). Then type
ipconfig
at the command prompt. This should show your IP address which will be something like 192.168.1.5 or 192.168.0.3. Whatever it is your PC and server should be in the same range of IP addresses (this just means the first three parts should be the same).

Step 7

Now that you've a working server you'll want to tuck it away somewhere, minus monitor, k/b and mouse of course (if your system board can boot without a k/b that is, it should be an option in the BIOS to ignore errors on boot). During the initial installation of Ubuntu Server you selected to install SSH. This is a protocol (program) which allows you to administer your server remotely from any PC (or even smart phone). If you didn't don't worry, just type:-
sudo apt-get install openssh-server
The easiest way to connect from another PC is to install Putty. A quick Google of Putty will have you up and running in a few seconds. When you launch Putty you will see it asking for a Host Name (or IP address). Just type in your server IP address and new port number, ensure SSH is selected, type a name into Saved sessions and click save, then click open. Say Yes to the security question and your connected. Now you can administer the server from your PC/laptop or even smart phone, how cool is that!
So there you have it. You've installed Ubuntu Server and configured it to let your Windows or Linux box connect to it. There are many more things your server can do for you, it can act as a Proxy server screening your network from the Internet and caching frequently used web sites, or as an email server, or a print server etc.
I'm no expert on Linux and I am sure there are other ways to achieve all this. Some of them may even be better but This method is tried and tested several times and if you follow the steps as shown it should work. If you have any suggestions for improving this tutorial then please let us all know .

No comments:

Post a Comment