Using a chroot environment

by richard on Fri, 12/09/2011 - 08:59

A Chroot environment is usually a folder on your filesystem that contains the entire base system for an operating system.

This tutorial will assume you are installing debian squeeze into a folder /chroot/squeeze

You can create a chroot environment itself using debootstrap.

richard@vpceh:~$ debootstrap squeeze /chroot/squeeze http://ftp.uk.debian.org/debian

That sets up the base packages, and then you need to mount the extra filesystems, and create the list of mounted systems.

The /dev filesystem contains all the device nodes for your system. I find it easiest to bind-mount this from the host operating system.

richard@vpceh:~$ sudo mount -o bind /dev /chroot/squeeze/dev

The /proc filesystem contains information about current running processes. This is mounted using the following command

richard@vpceh:~$ sudo mount -t proc none /chroot/squeeze/proc

you can then chroot into the system using the command

richard@vpceh:~$ sudo chroot /chroot/squeeze /bin/bash

Finally, just read the mounted filesystems into /etc/mtab by running

root@vpceh:/# cat /proc/mounts > /etc/mtab

and then you can perform operations as if you were on your own base system :)

Using get_iplayer to download from iPlayer

by richard on Sun, 09/18/2011 - 14:24

get_iplayer is a nifty little tool that allows you to download iPlayer programmes (either radio or TV) to your computer.

Installation on Debian is very easy. Just fire up a terminal, and use apt-get.
You can, of course, use synaptic or any other package management tool that interfaces with apt, but I use apt-get

richard@eee:~$ sudo apt-get install get-iplayer

apt-get will ask you to confirm that you want to install the dependencies, which is fine.

Once it is installed, you can browse available programmes, download specific programmes, or download any programme that matches a string. Examples are below:

Browse Programmes

richard@eee:~$ get_iplayer torchwood

get_iplayer v2.78, Copyright (C) 2008-2010 Phil Lewis
This program comes with ABSOLUTELY NO WARRANTY; for details use --warranty.
This is free software, and you are welcome to redistribute it under certain
conditions; use --conditions for details.

Matches:
704: Torchwood - Miracle Day: Episode 1, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
705: Torchwood - Miracle Day: Episode 2, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
706: Torchwood - Miracle Day: Episode 3, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
707: Torchwood - Miracle Day: Episode 4, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
708: Torchwood - Miracle Day: Episode 5, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
709: Torchwood - Miracle Day: Episode 6, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
710: Torchwood - Miracle Day: Episode 7, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
711: Torchwood - Miracle Day: Episode 8, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
712: Torchwood - Miracle Day: Episode 9, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
713: Torchwood - Miracle Day: Episode 10, BBC HD, Audio Described,Drama,Guidance,Popular,SciFi & Fantasy,TV, default,audiodescribed,

INFO: 10 Matching Programmes

Download Specific Programme

richard@eee:~$ get_iplayer --get 713

get_iplayer v2.78, Copyright (C) 2008-2010 Phil Lewis
This program comes with ABSOLUTELY NO WARRANTY; for details use --warranty.
This is free software, and you are welcome to redistribute it under certain
conditions; use --conditions for details.

Matches:
713: Torchwood - Miracle Day: Episode 10, BBC HD, Audio Described,Drama,Guidance,Popular,SciFi & Fantasy,TV, default,audiodescribed,

INFO: 1 Matching Programmes

This will then go on to download the programme.

Download all programmes matching a string

richard@eee:~$ get_iplayer --get Torchwood
get_iplayer v2.78, Copyright (C) 2008-2010 Phil Lewis
This program comes with ABSOLUTELY NO WARRANTY; for details use --warranty.
This is free software, and you are welcome to redistribute it under certain
conditions; use --conditions for details.

Matches:
704: Torchwood - Miracle Day: Episode 1, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
705: Torchwood - Miracle Day: Episode 2, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
706: Torchwood - Miracle Day: Episode 3, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
707: Torchwood - Miracle Day: Episode 4, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
708: Torchwood - Miracle Day: Episode 5, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
709: Torchwood - Miracle Day: Episode 6, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
710: Torchwood - Miracle Day: Episode 7, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
711: Torchwood - Miracle Day: Episode 8, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
712: Torchwood - Miracle Day: Episode 9, BBC HD, Audio Described,Drama,Guidance,SciFi & Fantasy,TV, default,audiodescribed
713: Torchwood - Miracle Day: Episode 10, BBC HD, Audio Described,Drama,Guidance,Popular,SciFi & Fantasy,TV, default,audiodescribed,

INFO: 10 Matching Programmes

Downloading a programme from a URL
If you've found the programme you want on the iPlayer website, then you can download from a specific URL. To do this:

richard@eee:~$ get_iplayer --get "http://www.bbc.co.uk/iplayer/episode/b014vy02/Doctor_Who_Series_6_The_God_Complex/"
get_iplayer v2.78, Copyright (C) 2008-2010 Phil Lewis
This program comes with ABSOLUTELY NO WARRANTY; for details use --warranty.
This is free software, and you are welcome to redistribute it under certain
conditions; use --conditions for details.

INFO: Trying pid: b014vy02 using type: tv
INFO Trying to stream pid using type tv
INFO: pid found in cache
Matches:
208: Doctor Who: Series 6 - 11. The God Complex, BBC One, Drama,Highlights,Popular,SciFi & Fantasy,TV, default,

INFO: 1 Matching Programmes

Note the quotation marks around the URL. These are required

 

It is worth noting that get_iplayer will default to downloading as an FLV file (which won't play on everything) unless it can find ffmpeg on the system. To install ffmpeg, just run
richard@eee:~$ apt-get install ffmpeg
then get_iplayer will convert it to a nice mp4 for you

SSH Keys

by richard on Sun, 09/18/2011 - 14:08

SSH and SCP are wonderful tools. I use them all the time for moving files, managing servers, and anything else I need to do on a remote system.

One thing that gets me though is the way I have to type my password every time. Enter SSH Keys...

SSH keys are a private/public keypair system for authenticating you when logging into a remote machine by SSH. The private key sits on the client, and the public key can sit on as many servers as you wish.

Setting up SSH keys is easy. On your client machine, you need to generate the keypair. Fire up a shell, and run
richard@eee:~$ ssh-keygen

There are options for ssh-keygen, such as setting the encryption type, and number of bits, but I'm happy with the defaults.

The program will prompt you for where to save the file (the default ~/.ssh/id_rsa is fine), and then for a passphrase. It isn't good form to leave an empty passphrase, as then anyone who gets your private key would be able to use it to get into any of your servers. I know, it means that you have to enter a password when you're using SSH, but at least you can have a shorter password than the 41 character alpha-numeric password your sysadmin forces you to use!

ssh-keygen will then show you what your key looks like (as a randomart picture), and write the keypair to the file you specified.

So now you have a keypair, you need to copy the public part to your server.

richard@eee:~$ scp .ssh/id_rsa.pub remoteserver:~/

This will copy the id_rsa.pub file (the public part of your key) to your remote server, into your home directory.
Then, ssh to your server, and run
richard@remoteserver:~$ cat id_rsa.pub >> .ssh/authorized_keys2

This will append your public key to the authorized keys for this account.

Note, you can have more than one key for a single account. So, if you have a laptop, and a desktop, then you might have the key that is on your laptop able to log into some servers, and the key from your desktop able to log into more servers (because your desktop is less likely to be stolen!).

Terminate the SSH session, and then try logging in with your new SSH keys. It should drop you straight in!

Redirect Visitors by IP Address

by richard on Wed, 09/14/2011 - 14:59

OK, so I was doing some work on my site, and wanting to redirect everyone but me to a separate version of the site (so I could play around as much as I wanted with the main version, and nobody would notice).

This is really easy, it turns out, using mod_rewrite in apache.

First, you need to know what IP address (or addresses) you want to exclude. This is fairly easy, just use a site like whatismyip to find out your external address.

Then comes the mod_rewrite magic.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.rf152.co.uk$ [NC]
RewriteCond %{REMOTE_ADDR} !^10\.20\.30\.40$
RewriteRule ^(.*)$ http://www2.rf152.co.uk/$1 [L,QSA]

The first line turns the rewrite engine on (you don't need to do this if it's already turned on further up)
The second line specifies the site you are directing away from (in my case, my main www site). This ensures that if, like me, the www2 is in a subsite of the www, then it doesn't create a redirection loop.
The third line is specifying the ip address to exclude. I have used 10.20.30.40, but if it was a live site, it would be an external one.
The final line is the line that actually does the rewriting. It says to redirect anything to www2.rf152.co.uk. The [L,QSA] part means that this will be the last rule applied, and it will attach the QueryString from the first request