Friday, 13 December 2013

My little File Server

I've sat patiently and waited with Windows 7 running on my little Asus EE Box for Bitcasa to come out with a Linux client and then they announced it but with a whopping prices increase from $99 a year to $1,198 a year. So it was time to say stuff them and come up with another plan. I wanted Linux back on my server, it was faster, the Windows sharing was actually easier that Windows and it was so much more versatile, but which flavour of Linux was my next problem.

I could go for Debian, and Ubuntu based distro or PCLInuxOS, the latter was always my favourite distro and is incredibly reliable but there seems to have been some problems with some of the apps that I like to use such as Stellarium but on a server this really isn't a problem. The other problem however, is a bit more serious. Once the OS has been installed, unless I put a manual entry into the hosts table of all the other machines I can't ping or connect to the PCLinuxOS machine using the host name but only by the IP address. This problem doesn't happen in other Linux variants but seems peculiar to Mandriva derivatives.
So I've decided to go with Ubuntu Server on the Asus as I only need to install the services I want to, not use a graphical desktop and make better use of it's resources.

And so on with the show.

I'm not going to go through each step of the installation because if you can't work the easy bits out this is not for you.
On the hostname section, type in what you want your machine to be know as.
And for home networks it's unlikely there's a proxy setup.
I'm not going for an encrypted file system and on the partitioning section I'm selecting "Guided, use entire disk".
I'm not going for automatic updates as I want to choose when I install then and when I reboot the machine,
On the software selection page I'm going to install "Basic Ubuntu Server", "Open SSH Server" and "Samba File Server". The rest can come later.
Once the ssh server is running I can connect up to the server with a terminal and do the rest of the stuff from command line.
A few more questions and it's time to reboot and see if SSH works so I can carry on with the configuration.
Once you can logon through ssh, it's time to make sure there are no updates needed by typing:

sudo apt-get update


sudo apt-get dist-upgrade

If there are, get them installed and reboot before continuing.

As I'm using Linux on my desktop too, I want to connect to shares with NFS, if you are using a Windows machine then you don't need to do the next bit.

sudo apt-get install nfs-kernel-server

I also want to install an ftp server so I can easily sync my laptop with the server, I'll come back to that in another article, but to install the ftp server we use the command:

sudo apt-get install vsftpd

Next, it's time to add some storage to the server in the form of an external 3Tb USB drive. Plug the drive and type in:

dmesg |tail

This will tell you the device where the drive is located, in my case it's /dev/sdc

I now need to create a mount point for the drive, I'm going to mount it at /mnt/data so I need to create that mount point by typing:

sudo mkdir /mnt/data


sudo chmod 0777 /mnt/data

This is to change the permissions, I'm leaving it fully readable and writeable by any user on the server which is OK for me.

Now, it's time to partition the drive, I'm doing this with a 3TB drive, it will obviously be different for different drives, at the command line type the following:

sudo parted /dev/sdc "depends on where your drive appeared in the dmesg |tail option"

I then have to remove the existing partition with:

rm 1

Then take the following steps to create a new partition:

unit TB  "sets the drive numbers to Terra Bytes"

mkpart primary 0.00 TB 3.00 TB


That's the partition made, next we have to format it with:

sudo mkfs.ext4 /dev/sdc1

Now, by default 5% of the drive is reserved by the OS which is about 150Gb and is far too much, so we need to drop this a bit with the command.

sudo tune2fs -m 1 /dev/sdc1

And finally we need to mount the drive, rather than do this manually, we are going to edit the fstab file to do it at boot, first we need to find the uuid of the drive with the following:

ls /dev/disks/by-uuid

On my machine I get the following:


Open up the fstab file with:

sudo nano /etc/fstab

Two of the above IDs will be used in the file, using the one that isn't add the line

UUID=ca34fdde-ccb2-4132-9d17-95530c0a7f3d /mnt/data     ext4    errors=remount-ro 0     1

with the UUID depending one the ones shown on your machine.

Save the file with CTRL and X and then type:

sudo mount -a to mount the drive

We do pretty much the same with the backup drive but mount it at /mnt/backup

I'm going to setup an nfs share with the data drive next so that I can mount it on my Linux desktop machine, we do this by editing the /etc/exports file with:

sudo nano /etc/exports

and adding the line:

/mnt/data *(rw,nohide,insecure,no_subtree_check,async)

This will share it will all machines on your network.

We then need to run the following command:

sudo exportfs -rav

followed by:

sudo service nfs-kernel server start

just to make sure that nfs is active

That's NFS taken care of, now let's setup the Windows shares.

More file editing this time, so back to the command prompt and type:

sudo nano /etc/samba/smb.conf

At the top of the file, we need to change the workgroup name to whatever you want to call it, the line should read:

workgroup = workgroupname

Underneath this we need to add:

netbios name = servername

Then right down at the end of the file we need to add ours Windows shares

Let's say we have a folder called Music on the 3Tb drive we added earlier, on the server the path would be /mnt/data/Music

At the end of the smb.conf file we need to add

path = /mnt/data/Music
browesable = yes
public = yes
writeable = yes

You don't have to do it quite like that, the browseable option means it will show as a share on the network, if you set that to no, there will still be a share there but you will need to enter the path into explorer to open it.

Public means anyone that has access to the network can read it, you can make this more secure if you want by specifying users.

And writeable means you can either get read write access or just read access.

Follow the same pattern for the rest of the shares and remember the directory and path must be the same as they are on the file system and all paths are case sensitive. Which means that /mnt/data/Music and /mnt/Data/Music are different.

Restart the server with sudo service smb restart and that's it a really basic file server up and running, all we need to do now is setup a script to back up the data drive.

So type sudo nano /bin/mybackup to start creating the file and then enter the following into it.

rsync -urvx --progress -h --force --delete /mnt/data/ /mnt/backup/

Save the file and then type sudo chmod +x /mnt/mybackup to make the file executable.

Just type mybackup to run it

That's it, it's not as bad as it looks, just take your time and go through each bit step by step, there's plenty of help available on the Interwebs.

Next time I'll be setting up the ftp server.

And before anyone starts, I've really simplified it here, I could do a lot more to tighten up security but for me this setup will do.