alan

mostlyunixish

I find it extremely handy to be able to recall my windows by just using the keyboard.

It's pretty easy to do in Linux, just use:

wmctrl -x -R <WM_CLASS>

you can find the WM_CLASS of any window using this command:

xprop | grep WM_CLASS

then clicking on the desired Window. There may be more than one match, just choose one.

Example:

wmctrl -x -R "Gnome-terminal"

When you execute such command, the windows matching such class will be gathered on the current workspace and focused; you can assign global hotkeys in Gnome with System -> Preferences -> Keyboard -> Shortcuts. The Windows key (Super) is usually a great candidate for such global hotkeys.

#MostlyUnixish #xwindows #productivity

  • UPDATE: modern ssh-copy-id often support a -p parameter for the port. So this trick is unnecessary and won't probably work.

ssh-copy-id is a cool script which is usually distributed along openssh client nowadays; it performs an automatic install of your local public SSH key to a remote server. Its syntax is simply:

ssh-copy-id [-i [identity_file]] [user@]machine

You may have spotted there's no builtin way of telling it what's the port of the remote server; a workaround exists which leverages the way arguments are passed to the script (it's a simple bash file, take a look at it. You can find various versions around the internet).

Just do this:

ssh-copy-id '-p [port_number] [-i [identity_file]] [user@]machine'

And enjoy your newly-added SSH key!

#MostlyUnixish #ssh

If you develop enough software, most probably you'll have a test/development network at your service.

In such networks, which are usually handled through a virtualization infrastructure, machines come and go very quickly. But ssh clients are usually unhappy about that:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
The authenticity of host '192.168.0.208 (192.168.0.208)' can't be established.
RSA key fingerprint is 31:4d:8b:97:c8:57:04:85:6a:1b:72:54:46:ab:04:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

The first time you connect, you're asked if the fingerprint is correct. The second time you'll just be allowed to connect:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
root@192.168.0.208's password: 

What then if the machine at such ip address changes, because it's rebuilt or modified and the ssh host key is not retained?

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
7b:42:03:5f:5c:8d:5a:2b:6a:a4:1e:d6:0d:05:de:38.
Please contact your system administrator.
Add correct host key in /Users/alan/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/alan/.ssh/known_hosts:111
  remove with: ssh-keygen -f "/Users/alan/.ssh/known_hosts" -R 192.168.0.208
RSA host key for 192.168.0.208 has changed and you have requested strict checking.
Host key verification failed.

From the point of view of openssh this may be a serious security breach, and you should fear such message if it happens in the wild if you haven't touched a server - a MitM attack is probably going on!

What about your local network? ssh-keygen -R 192.168.0.208 will solve that, but you probably didn't need host key authentication since the beginning.

So, just add this:

Host 192.168.0.*
    CheckHostIP no
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no

at the beginning of your /etc/ssh/ssh_config file, and feel good.

Now, anytime you try connecting inside your network you'll be greeted by this:

alan@melquiades:/etc/ssh$ ssh root@192.168.0.208
Warning: Permanently added '192.168.0.208' (RSA) to the list of known hosts.
root@192.168.0.208's password: 

No confirmations whatsoever.

#MostlyUnixish #linux #ssh

Whenever you use Thunderbird on Linux you may encounter one annoying behaviour.

If you open a message which is got an attachment – you just need to open the message, not to save the attachment! – Thunderbird will place such attachment in our temporary directory, usually /tmp. You find the file there, and you think you used the Save as function to place it there, so you open it and you edit it.

Then you close Thunderbird, and poof! It clears its temporary files and deletes your modified attachment, possibily containing a day's work.

The workaround is simple: just use a launcher that changes Thunderbird TMP directory and use that to start Thunderbird!

#!/bin/bash -e
mkdir -p /tmp/thunderbird
TMP=/tmp/thunderbird /usr/bin/thunderbird "$@"

You may need to adjust the original binary path, then you can put such script in your path before your /usr/bin directory and you're done.

#MostlyUnixish #linux #thunderbird #temporary

Bash builtin disown is a not-so-well known [job control command] http://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html), and yet it's an extremely useful one.

So what? You've started a remote ssh session, launched some command which is taking very, very long to complete, and you just want to go home, or maybe you feel your connectivity may drop, and so on... and you forgot to launch screen (possibly with [byobu] https://launchpad.net/byobu)) or tmux before starting your work!

Now you're desperate and you'd just like to go home, but you fear your longstanding jobs will quit if you close your terminal.

Just background your job, launch disown and all the jobs in background will receive no notification whatsoever of your saying goodbye to them; of course you won't be able to read their output to the terminal.

#MostlyUnixish #linux #bash #ssh

Whenever I install a production system/server I tend to enable full automatic updating, because it's usually better that something breaks because of a failed update rather than leaving a production machine unpatched; and if you pick a rather stable distribution (Centos or Ubuntu LTS are good examples) chances that you'll end up with a broken system are fairly low.

Btw let's get back to the point: in such cases you may end up with a disk which was filled up with obsolete kernels. The following snippets allows you to get all outdated kernels which are not in use, so you can automagically pipe its output to an apt-get remove -y in a cron job.

That may sound trivial, but what happens if you're not careful (e.g. if you try deleting all kernels but the latest) is that you might try removing the currently running kernel – and it's pretty bad if that happens in an unattended cron job.

#!/usr/bin/env python

# print all installed kernel/headers/etc BUT the current and the latest;
# suitable to be used with apt-get remove.
# requires aptitude to be installed. Works on Ubuntu and probably Debian

from subprocess import check_output
import re

def runcmd(s):
    return check_output(s.split())

# should work for anything until kernel 3.100
kernel_version_pattern = re.compile("[23]\.\d{1,2}\.\d{1,2}-\d{1,3}")
def get_kernel_version(kernelstring):
    return kernel_version_pattern.search(kernelstring).group(0)

def get_providing_packages(pkg):
    return filter(lambda x: x != "",
        map(str.strip,
            runcmd('aptitude search ~i~P%s -F%%p' % pkg).split("\n")
            )
        )

def get_nonmatching_packages(package_list, excluding):
    return [k for k in package_list if not get_kernel_version(k) in
        excluding]

all_kernels = get_providing_packages("linux-image")
all_kernels.sort() # lexicographical

kernel_versions = map(get_kernel_version, all_kernels)
latest_version = kernel_versions[-1]
current_version = get_kernel_version(runcmd("uname -r"))

kernels_to_remove = get_nonmatching_packages(all_kernels, (current_version,
    latest_version))

impl_headers = get_providing_packages("linux-headers")

headers_to_remove = get_nonmatching_packages(impl_headers, (current_version,
    latest_version))

# This seems to be needed in Ubuntu >= 13.04
#base_headers_to_remove = [header.replace("-generic", "") for header in headers_to_remove]
#headers_to_remove += base_headers_to_remove

print " ".join(kernels_to_remove + headers_to_remove)

Suggested use in a cron job, of course, is just:

apt-get remove --purge -y `ubuntu_get_obsolete_kernel_and_headers_pkgs.py`

WARNING: recent changes to the way Ubuntu marks dependencies and provided names for kernel headers require that you uncomment a couple of lines in the script towards the end; just remove the # at the beginning of the lines 46 and 47.

UPDATE: in recent Ubuntu versions, just invoking apt-get autoremove --purge will do the trick. Such command will remove all automatically installed packages that are no longer required in the running system. It can remove other packages beyond outdated kernels, by the way.

#MostlyUnixish #linux #ubuntu #debian #update #maintenance

Some days ago I ranted about an issue I had with Mercurial and Dropbox; basically, I did put a mercurial repo in a dropbox folder, and ended up with this kind of mess

Mercurial repos inside Dropbox are not a great idea after all. But I had one problem: I sometimes forget to push changes to some files, and Dropbox comes to the rescue; at the same time I need history for such files, and I don't want problems to arise.

I found quite an easy solution:

  • Create your dropbox-aware folder, e.g. “somefolder”
  • create a .hg folder inside it, e.g. “somefolder/.hg”
  • now use your dropbox client, be it the CLI or the GUI, enable Selective Sync and remove such .hg folder from being synced on your current machine. You must take this step on all machines that you want to use with such technique.
  • disabling sync for .hg should remove it from the somefolder directory.

Now you can use hg init inside “somefolder”, and you'll obtain a non-synced hg repository.

What for? Whatever! I just create a repo on bitbucket and push/pull changes to/from here.

On other machines, after making sure you've excluded .hg from syncing, you can just use

hg clone -U YOUR_REPO_URL

to obtain such .hg directory.

This way:

  • it's impossible to get errors or conflicts like the one I previously mentioned
  • you get full storicization
  • Dropbox syncs the actual working copy, letting you have the latest version always 100% synced without manual intervention.

Enjoy!

#MostlyUnixish #dropbox #mercurial #productivity