Category Archives: Linux

Ubuntu wallpapers

For every version of Ubuntu there is a set of wallpaper images. Each version has its own set and doesn’t include the old ones. However, it is very simple to install all previous versions by running:

apt-cache search ubuntu-wallpapers- | awk '{ print $1; }' | xargs apt-get -y install

As of today (January 2015) this will install the wallpapers from:

  • Karmic (9.10)
  • Lucid (10.04)
  • Maverick (10.10 LTS)
  • Natty (11.04)
  • Oneiric (11.10)
  • Precise (12.04 LTS)
  • Quantal (12.10)
  • Raring (13.04)
  • Saucy (13.10)
  • Trusty (14.04 LTS)

If you want to use them for other purposes you will find the images in /usr/share/backgrounds/. I use them as wallpaper in OS X. In return I may just use the Yosemite wallpapers in Ubuntu to really confuse people.

Switch BU-353 to NMEA mode

BU-353 is a nice little USB based GPS receiver that I use for several types of project. Straight out of the wraps it defaults to outputting NMEA data at 4800 baud which is exactly what I want. However, it also supports the SIRF binary protocol and may switch to that format. This can happen if you connect it to a system that uses gpsd. That daemon supports both the NMEA and the SIRF protocol but will switch the GPS to the latter if it can.

So how do you switch it back? Maybe you can just leave it be and the supercap inside will discharge and it will revert to its default settings. The FAQ kind of hinted at that. However, I wasn’t patient enough to see if that works so I needed a quicker option.

It turns out that if you use Windows it is not too difficult. You can follow any one of several guides on the net, for instance this, straight from the horse’s mouth.

But I needed a way to do this from Linux and this is how.

First make sure you have gpsctl in your path. If it is not installed you can install it by running:

sudo apt-get install gpsd-clients

I am doing this on an Ubuntu system but it should work on most Debian derivatives.

Then connect your BU-353 and type (assuming that your GPS device turns up at /dev/ttyUSB0):

sudo stty -F /dev/ttyUSB0 4800
sudo gpsctl -n -D 4 /dev/ttyUSB0

Then it should be back on NMEA.

Run script on USB device insertion

Udev is a device manager for the Linux kernel and can be of great help to trigger activities when devices are added or removed. Once such scenario could be to run a script when the user connects a USB memory stick.

The first thing necessary to write udev rules is to have information about the device to create filters. However, I had a hard time finding information about what filters were available when I wanted to write a set of matching rules that would not cause a number of false triggers. To do this use the following command (replacing the device name to whatever device you want information about):

udevadm info -a -n /dev/sdb1

The following is an example of a command that can be used to run a script whenever a USB device is inserted.

ACTION=="add",KERNEL=="sd?1",SUBSYSTEM=="block",RUN+="/usr/bin/usb_insert %k"

The parameter %k to the script will be converted by udev to the device that caused the event. Note that the line is wrapped here but must be stated as a single line in the system.

Snappier virtual Ubuntu

The use of Unity as the default interface for later versions of Ubuntu is a welcome addition for some as it makes better use of limited screen sizes. But when Unity 2D was discontinued in Ubuntu 12.10 it caused problems for people, like myself, who keep a virtual Ubuntu installation at hand for those tasks that require a Linux system. Running Ubuntu 12.10 or later on VirtualBox will often result in terrible performance, making it virtually useless.

One solution is to stay with Ubuntu 12.04. But another solution is to fallback to the old interface. Luckily, this is very easy to do by installing the package gnome-session-fallback.

sudo apt-get install gnome-session-fallback

After having installed the package just log out. When logging in select the Gnome Classic interface from the login page. Subsequent logins will use the same interface as the previous session. It is also possible to change the default interface.

sudo /usr/lib/lightdm/lightdm-set-defaults -s gnome-classic

Create bootable Ubuntu USB stick

I don’t create bootable USB sticks that often but every time I do it I think that it’s harder than it really is and start to search the web for walkthroughs.

This post could also simply be written: Look at the Ubuntu download page.

Here is the process for OS X:

  1. Download ISO file of the operating system you want to put on the USB stick
  2. Open the terminal
  3. Convert the ISO file using the convert option of hdiutil: hdiutil convert -format UDRW -o /path/to/target.img /path/to/source.iso
  4. Run diskutil list to get the current list of devices
  5. Insert the USB stick
  6. Run diskutil list again to determine the device node assigned to your USB stick
  7. Unmount the USB stick: diskutil unmountDisk /dev/diskN
  8. Write the image to the USB stick: sudo dd if=/path/to/disk.img of=/dev/rdiskN bs=1m
  9. Eject the USB stick: diskutil eject /dev/diskN

Your own URL shortener

I use mostly bit.ly for shortening URLs but sometimes you don’t want to be dependent upon a public site, even if it is big and popular. I needed to install a custom URL shortener and this is what I did.

Firstly, my requirements were that it should be lightweight and simple. I didn’t expect that many hits on the URLs and even though I am a big fan of Ruby on Rails the idea of spinning up a Rails site just to dish out a HTTP redirect didn’t sound like a great idea. Instead I went for PHP backed by a MySQL database.

Now, searching for URL shorteners written in PHP results in a large number of hits and it can be pretty daunting to know which to pick. Shortly and Yourls were frequently present in listings of popular shorteners so I decided to setup those and benchmark them. However, the Shortly web site turned out to be unavailable so in the end I settled on just installing Yourls.

Yourls supports both public and private mode of operation. In private mode a password is required to administer it. In my case I wanted to make it simple and allow everyone at the company to manage the URLs but disallow access to the management facilities from the Internet. However, Internet users still need to be able to resolve the URLs. I solved this via conditional rewrites in the Apache configuration, like so:

	Alias /s/ "/srv/www/shortener/"
	RewriteCond %{REQUEST_URI} ^/s/admin
	RewriteCond %{REMOTE_ADDR} !^192\.168\.(\d+)\.(\d+)$
	RewriteRule ^/.*$ - [F]
	RewriteCond %{REQUEST_URI} \+$
	RewriteCond %{REMOTE_ADDR} !^192\.168\.(\d+)\.(\d+)$
	RewriteRule ^/.*$ - [F]
	RewriteCond %{REQUEST_URI} ^/s/[index.php]?$
	RewriteCond %{REMOTE_ADDR} !^192\.168\.(\d+)\.(\d+)$
	RewriteRule ^/.*$ - [F]

The above is from the config file for the corporate web site, lodging the URL shortening under the /s/ subdirectory of the web site. There are three rewrite rules:

  • Preventing access to the admin pages for external clients
  • Preventing access to the link statistics for external clients
  • Preventing access to the link creating page for external clients

By default Yourls will create sequential links starting from one and going up. This makes it trivial to guess other links which is not what I wanted. Fortunately, it turned out to be very simple to create random links by following the instructions found on this page.

I am quite happy with the result. I wish the default web GUI would come with different themes but since it is only used when administering the links it doesn’t really matter. I will continue to use bit.ly but Yourls is definitely a keeper.

Convert Rails 3 project to use jQuery

Rails 3 supports unobtrusive javascript to decouple the content from the logic in a way similar to how CSS has helped web designers to decouple the content from the layout.

By default Rails 3 comes with Prototype. But more and more people are switching to jQuery. The project Rails-3-jQuery on Github simplifies the process of switching out the Prototype support to jQuery. It includes jRails that provides drop-in replacements for Prototype and Scriptaculous helper methods to make it a seamless transition.

To update a project, just issue the following command in the project root

rake rails:template 
   LOCATION=http://github.com/lleger/Rails-3-jQuery/raw/master/jquery.rb

For more information on the project, check the project page on Github.

Backup of Lazy8Web data

Lazy8Web is a free web application for basic bookkeeping tasks. I use it myself for the community where I live and I can really recommend it.

However, I needed to add a way to do backups. There is a function to export all data to XML but you must be logged on to do that and the authentication system doesn’t allow just using wget with username/password.

To automate the process I wrote the following little snippet that I run as a cron job. It logs onto the site, downloads the data and saves it to a file with the current date and time as part of the filename.

#!/usr/local/bin/ruby
#
# Author: Martin Bergek (http://www.spotwise.com)
# Dependencies: Ruby, Mechanize (http://mechanize.rubyforge.org)
#

require 'rubygems'
require 'mechanize'

# Make changes here
base_url = 'http://www.example.com'
username = 'user'
password = 'password'
log_folder = '/home/user/'
# No changes below this line

# Login
agent = Mechanize.new
page = agent.get(base_url + '/index.php?r=site/login')
form = page.forms.first
form['LoginForm[username]'] = username
form['LoginForm[password]'] = password
page = agent.submit(form, form.buttons.first)

# Get backup for all companies
xml = agent.get_file(base_url = '/index.php?r=company/exportAll')

# Save backup
t = Time.now
logfile = log_folder + t.strftime("%Y%m%d-%H%M%S.xml")
File.open(logfile, 'w') {|f| f.write(xml) }

Roll your own certificates

I host a bunch of Internet services. Some, like this site, are for public use but most are intended for personal use. And since I want to keep them personal, security is important.

Most Internet applications can be secured by SSL certificates. This includes HTTPS access for web sites, IMAPS instead of IMAP, encrypted access to subversion repositories. The list goes on.

For a number of years I have been creating my own certificates with the help of OpenSSL. Much can be said about OpenSSL but it is not exactly point-and-click. I created a few scripts to assist me but still I found myself looking through the documentation whenever I needed to create a new certificate or renew an existing one.

To solve this I have now created the attached Bash script to wrap OpenSSL to be able to set up a two-level certificate hierarchy. The result is a menu-driven text based application that will no doubt save me time in the future. Perhaps it can help you too.

I use this to create certificates to be used by Apache, Subversion, Postfix, Dovecot, Microsoft IIS, Microsoft Exchange and a few others.

There are some limitations to this. The hierarchy will be two levels deep, not more and not less. In other words you will get a root certificate authority and one or more subordinate certificate authorities. User certificates will be signed by one of the subordinate CAs. Also, while it supports subject alternative names it only does so for DNS names (I only needed that for Exchange 2007 support). For real-world certificate authorities the certificate signing requests (CRSs) are created outside the certificate authority. Since I will be handling everything myself I have set it up so that CSRs are created inside this application. For that reason it is imperative to keep access to the CA folder structure secure so that the private keys are not compromised.

To use this script just download and extract the attached file. Inside the folder which will be created there will be two files. The file ‘ca’ is the Bash script and the other is a template configuration file. Run the script from within the folder. Certificates will be available in the folder ‘all’ and backups of the certificate hierarchy will be placed in the folder ‘backup’. For more help, please comment.

The script has been verified on Ubuntu 9.04 and Mac OS X 10.5. I don’t think there are any dependencies that aren’t already met by standard OS installations.

Download: ca_v1.tar.gz

Audio feedback to shell output

The -f switch to the tail command is a great help when it comes to keeping track of what is going on in some log file. But sometimes you just can’t keep your eyes on the output to see if something is happening. At those times it would help to have some other indication that there is activity. The following small Perl script emits a beep whenever there is a line of data being output.

#!/usr/bin/perl
while(<>) 
{
	print "\007" . $_;
}

Just tack the script somewhere in your path. Then, whenever you want some audio feedback just pipe the output to the Perl script (i.e. {some command} | beeper.pl).