Friday, 20 December 2013

June

Yes I know it's December and it's also December 2013, I really don't know where the time has gone and once more things are starting to slowly come into place.

It's a Friday morning, I'm having a relax and messing around with my computer while playing some music and a tune came on that I've heard quite often but a few synapses have fired away in my brain and made a connection.

The tune in question is about the month and not a person, but the first few lines talk about June and probably the most important woman in my life.

I know you should say that your wife is the most important woman but without this woman I wouldn't be here.

As I said, June was the most important woman in my life and I never got round to meeting her, that is the saddest part.

 So here's a suggestion, say hello to people a bit more often, forget about disagreements and never let the sun go down on an argument. 

 End of my little ramble for the day and to end, here's Spock's Beard, dedicated to the most important woman I never knew.


Monday, 16 December 2013

Ubuntu Server and Torrents

I'm determined that I'm not going to install any sort of a graphical UI on my Ubuntu server install and so far the one thing that has been causing me problems is a torrent client. I actually use this for legit purposes such as sharing out Linux ISOs and don't really want to have to run up a remote desktop to check the client.

I found something called deluge, which can run in text mode and can provide a web interface so that it can be controlled from another machine, just what I wanted. So on the server I needed to do the following, open up a command prompt and enter:


sudo apt-get update

sudo apt-get install deluge-console deluged deluge-web

Now we need to configure them so they start up at boot time, this is a tint bit more complicated but not that hard, I found this information on the Deluge website.

So at the command prompt we do the following: sudo nano/etc/default/deluge-daemon We need to enter the following into this file, making sure you change the username to the user that you want to run it as.


# Configuration for /etc/init.d/deluge-daemon

# The init.d script will only run if this variable non-empty.
DELUGED_USER="" # !!!CHANGE THIS!!!!

# Should we run at startup?
RUN_AT_STARTUP="YES"

Then we need to make another file with the following command: 


sudo nano /etc/init.d/deluge-daemon 


This should contain the following:


#!/bin/sh
### BEGIN INIT INFO
# Provides: deluge-daemon
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Daemonized version of deluge and webui.
# Description: Starts the deluge daemon with the user specified in
# /etc/default/deluge-daemon.
### END INIT INFO

# Author: Adolfo R. Brandes
# Updated by: Jean-Philippe "Orax" Roemer

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Deluge Daemon"
NAME1="deluged"
NAME2="deluge"
DAEMON1=/usr/bin/deluged
DAEMON1_ARGS="-d" # Consult `man deluged` for more options
DAEMON2=/usr/bin/deluge-web
DAEMON2_ARGS="" # Consult `man deluge-web` for more options
PIDFILE1=/var/run/$NAME1.pid
PIDFILE2=/var/run/$NAME2.pid
UMASK=022 # Change this to 0 if running deluged as its own user
PKGNAME=deluge-daemon
SCRIPTNAME=/etc/init.d/$PKGNAME

# Exit if the package is not installed
[ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ]
then
log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it."
exit 0
fi

if
[ -z "$DELUGED_USER" ]
then
log_warning_msg "Not starting $PKGNAME, DELUGED_USER not set in /etc/default/$PKGNAME."
exit 0
fi

#
# Function to verify if a pid is alive
#
is_alive()
{
pid=`cat $1` > /dev/null 2>&1
kill -0 $pid > /dev/null 2>&1
return $?
}

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started

is_alive $PIDFILE1
RETVAL1="$?"

if [ $RETVAL1 != 0 ]; then
rm -f $PIDFILE1
start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile \
--exec $DAEMON1 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS
RETVAL1="$?"
else
is_alive $PIDFILE2
RETVAL2="$?"
[ "$RETVAL2" = "0" -a "$RETVAL1" = "0" ] && return 1
fi

is_alive $PIDFILE2
RETVAL2="$?"

if [ $RETVAL2 != 0 ]; then
sleep 2
rm -f $PIDFILE2
start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile \
--exec $DAEMON2 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS
RETVAL2="$?"
fi
[ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred

start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2
RETVAL2="$?"
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1
RETVAL1="$?"
[ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2

rm -f $PIDFILE1 $PIDFILE2

[ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1
}

case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME1"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

:



In the option Daemon 2 args, I have put in -p 8080 to ensure that the web interface runs on this port.
Now we need to make it executable:

sudo chmod 755 /etc/init.d/deluge-daemon

Then set it to run on startup:

sudo update-rc.d deluge-daemon defaults
And then we can start it with:
sudo invoke-rc.d deluge-daemon start
and stop it with:
sudo invoke-rc.d deluge-daemon stop
Once it's started you can type deluge-console on the server terminal and then type connect in the bar at the bottom to ensure it's running, once you have connected, type in config to see how it is setup and then quit to leave, you should now be able to access the web interface by typing http://machinename:8080

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

and

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

then

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

quit

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:

6692275b-e45d-471b-bdce-4286fb4cd4b0
ca34fdde-ccb2-4132-9d17-95530c0a7f3d
cfb39364-db4b-4f52-a3f3-a5821e4a6096

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

[Music]
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.