Hash-based Diff for Directories

Recently I was working on a project where I needed to quickly and reliably detect changes to the contents of a directory, and when a change was detected run a series of commands.

There are any number of file differential tools, the venerable diff chief among them, and I think they would certainly do the job. They would certainly do a very complete job allowing for a comparison of every line of every file and be able to show exactly what changed where. But for what I needed to do, this seemed overkill.

Ultimately what I needed to know was if something had changed, not specifically what had changed. To that end, I realized what I needed was a view of the directory, not a view of the files themselves. I needed to know if a file had been changed, added or removed. Looking at a directory listing, I could easily see that something had been changed compared to an earlier listing sample. And then it donned on me -- I could solve this with a hash.

The MD5 hash is a fairly simple and very quick to execute hashing function which takes any input it is given and generates a hash value. Most POSIX systems include an md5 command that can be run from the command line which will output the hash value as a string. By capturing the hash value of the directory and comparing it each time the script is run, it becomes fairly easy to see when something has changed.

To make this work, I just needed to pipe the contents of the my directory using ls -la into the MD5 command and save the resulting string to a file.

lang:bash ls -la | bash

The final logic for the script looked something like this. I've done this extract to remove the bulk of the script which is all of the actions being run.

``` lang:bash

!/bin/bash

hashfile="/path/to/lastrunhash.md5" postdir="/path/to/source/directory/"

lasthash=cat $hashfile thishash=ls -la $postdir | md5

echo "Last Hash: $lasthash" echo "This Hash: $thishash"

if [ "$lasthash" != "$thishash" ] then echo "Directory value has changed" echo "Do your actions here..." echo "$thishash" > "$hashfile" else echo "Match!" fi ```

Ubuntu School – which: Finding Ruby

I've been getting my feet wet with Ruby the past couple of days and decided to post this as much for my own recollection as anything else. When you're creating a .rb script file the first line (as with nearly all *nix script files) contains a reference to the executable which should be used to execute the script.

```

!/usr/bin/ruby

```

But what if you don't know precisely where that file lives? The which command will find it for you. Which is a command which provides you the fully qualified path to shell commands, and this includes script hosts like ruby.

callisto:~ kdmurray$ which ruby /usr/bin/ruby

This also works for Perl, Python and any number of other executables. Ever wonder where the pwd command lives? or nano? or which itself? Go explore! This command works on most *nix operating systems including most Linux OSes and Mac OS X.

Ubuntu School – What Groups Do I Belong To?

This is a quick one. If you need to figure out what groups your user account belongs to you can simply use the groups command from the command line:

kdmurray@titan:~$ groups kdmurray adm cdrom sudo dip plugdev sambashare lpadmin debian-transmission smbmedia smbbackups ccnet

You can also use this command to find out what groups any other users belong to. This is particularly useful when setting up new services or new network shares and you need to confirm which groups the user belongs to.

kdmurray@titan:~$ groups ccnet ccnet : users ccnet

Try to find yourself a use for the groups command this week!

Ubuntu School – GUI (xubuntu-desktop) for Ubuntu Server

Whether it's because you're not fully versed in the power of the command line, or you just want to use a tool that will speed things along like gparted, the command-line isn't always the best tool for the job. Sometimes you just need a GUI, even if it's just for a few minutes. I'm going to walk through how to do the installation on Ubuntu 11.10 with the assumption that you only want the GUI occasionally, and don't want it consuming server resources all the time.

The first step is to get the GUI tools installed. ``` sudo apt-get update

sudo apt-get upgrade

sudo apt-get install xubuntu-desktop ```

That was the simple part. At this point you now have the GUI installed, but it will start the GUI every time the system boots, loading all of that extra cruft into memory. The next steps that I followed from a post on Techinote shows the steps to disable the automatic startup of the GUI environment.

``` sudo apt-get install lightdm

sudo nano /etc/default/grub ```

find the line GRUB_CMDLINE_LINUX_DEFAULT and set it to GRUB_CMDLINE_LINUX_DEFAULT="text"

``` sudo update-grub

sudo update-rc.d -f lightdm remove

sudo shutdown -r now ```

Done!

Your system will now be at the text-based login screen (as it was before this all started). When you need to use the graphical environment, simply type startx and it will move you into an already authenticated GUI desktop environment. This will allow you to setup and configure tools with a GUI (like CrashPlan) without having to rely the UI being constantly loaded and running in the background.

Ubuntu School – Get Rolling with Webmin on Ubuntu Server 11.10

Even if some Linux purists would have you believe the command-line is the only way to go, the pragmatist in me will always take an appropriate GUI over a complicated command-line any day. You can run a lot of powerful services for your home network using one or more Ubuntu server machines. With the right tools you don't need to be a Linux expert to make that happen.

The tool of choice is Webmin. This is a set of web-based tools which allow you to control virtually every piece of server-side software on you Ubuntu server. The GUI is intuitive and straight-forward, the documentation is excellent, and the project is under active development.

Because Webmin isn't in the standard repositories you will have to do a couple of quick command-line changes to configure your system to be able to find and download the apt package.

sudo nano /etc/apt/sources.list

Once the file is open, add these lines to the bottom of the file

```

#

Package Sources for Webmin

deb http://download.webmin.com/download/repository sarge contrib deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib ```

Those lines will add the necessary sources to apt for it to find the Webmin package. The Webmin package has also been digitally signed by its author. By default you will need to download the author's key so that apt can use it to verify the Webmin package at install time. Fortunately, this is really easy to do.

wget http://webmin.com/jcameron-key.asc sudo apt-key add jcameron-key.asc

Now that all the prep work is done, it's time to install Webmin.

sudo apt-get update sudo apt-get install webmin

All done! Now you can access administrative functions of your server's services from the Webmin console: https://yourservername:10000/. This URL is also shown in the last few lines of the apt install details that are output to the command-line.

Ubuntu School – Add an Existing User to a Group

Occasionally you need to grant an existing user some additional permissions to files, directories or applications. This typically means some kind of change in your permissions settings for the object in question. But because you can only have a single owner for a given object you need to be careful making these changes.

Something you can do, though, is extend the permissions on the object to a set of users by way of a group. Logically, a group is nothing more than a named collection of users who all have the same access (by way of that group) to some resource. Users in Ubuntu typically carry one primary, and one or more secondary groups (I won't get into the differences here).

By adding group permissions to your resources (ie give the 'payroll' group read/write access to the 'HR' folder) you can simply add users to and remove users from the appropriate groups and be confident that their level of access to the resources on your machine is set correctly.

To add an existing user to an existing group:

sudo usermod -a -G payroll graymond

To remove a user from a group you use the same command. The catch is, you remove a user from a group by re-adding all of their groups and simply omitting the group you wish to remove them from.

sudo usermod -nG mkirkpatrick

The system will show you a list of the user's groups.

marketing sales vanprinters torprinters

Then you simply run the usermod command as above, removing the group in question (in this case vanprinters)

usermod -G marketing,sales,torprinters mkirkpatrick

Ubuntu School – sudo Your Last Command

Sometimes we just forget that we need to specify elevated privileges on our Ubuntu machines. I do it all the time, particularly when I'm setting up a new machine.

Thankfully there's a shortcut for those of us who are forgetful. If I want to restart the box I can use a command like:

shutdown -r now

But of course that command requires elevated privileges:

shutdown: Need to be root

With the fantastic !! argument for sudo you can repeat your last terminal command:

sudo !!

Now you can quickly and efficiently re-run that last command you forgot to sudo.

Ubuntu School – DHCP Release and Renew

It's not uncommon to need to release/renew the IP address for a given machine. This is particularly true if you're doing any kind of maintenance on your network, or are troubleshooting pretty much any kind of Internet problem. I never seem to remember how to do this, so I'm including this post as much for my own benefit as anything.

What I'm talking about is the Ubuntu equivalent of these windows commands ``` ipconfig /release ipconfig /renew ``

From an Ubuntu terminal type: sudo dhclient -r sudo dhclient

Much like the Windows equivalents you can also specify these actions for a specific interface if your situation requires.

sudo dhclient eth0

Ubuntu School – Creating a New User

There are two built-in commands for creating a user from the command-line in Ubuntu: useradd and adduser. useradd is the older command which has, for the most part, been deprecated in favour of the more user-friendly adduser command. Both will allow you to create new user accounts, set up home directories and generally move in the right direction, but adduser will prompt you for information you didn't include whereas useradd will assume you didn't want those things (ie create the home directory).

sudo adduser theboss

will produce an output similar to

Adding user 'theboss' ... Adding new group 'theboss' (1001) ... Adding new user 'theboss' (1001) with group 'theboss' ... Creating home directory '/home/theboss' ... Copying files from '/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for theboss Enter the new value, or press ENTER for the default Full Name []: Joe Bossman Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] y

And there you are! Happy user-creating!

WordPress Plugin: Admin Links Widget

I've just launched the first version of a new WordPress widget, the Admin Links Widget.

This very simple widget does as the name suggests. It provides links to administrative functions from the sidebar of your WordPress installation. The catch is, only administrators will be able to see the links so the general public won't have their view cluttered up with links they can't use.

Give it a try!

  1. Download the Plugin
  2. Unzip into your WordPress plugins folder
  3. Activate the plugin
  4. Add the widget to your site! If you have any suggestions or feedback, please feel free to post below.