Computing Tips

I collect a few handy tips of improving computing experience. Hope you find it helpful.

Here is an index of the topics.

Secure Network-transparent Storage with autofs and sshfs

A basic way to understanding is abstraction. We see three abstractions the operating system makes for us on the computer right before you

So far, so good.

It would be nice if we extend the file-system abstractions to include storage beyond the disks attached to your personal computer. Here is how we can do it under Linux with autofs and sshfs.

Suppose we have three computers, office, home, and laptop. Our goal is to seamlessly access files on office and home through laptop. More specifically, on laptop, when we access /media/ssh/office, we are actually accessing files on office through the Internet. Besides, we want the access to be encrypted.

For the sake of illustration, I use the Arch Linux distribution to illustrate the procedure because that is what I have on my computers. Please refer to your package manager's documentation or you can always build the package yourself---that is one of the reasons I like Open Source/Free Software. To find your reason to like or hate it, go ahead trying it; see for yourself.

First, install the packages.

pacman -S autofs fuse sshfs openssh
Your package manager (e.g., aptitude, yum, emerge, zypper, or BSD's pkgadd) should take care of rest of the dependencies.

autofs and fuse need some initial setup; check this ArchWiki entry out! In summary, you need to

  1. modprobe autofs4 fuse; also add them to the MODULES line in /etc/rc.conf.
  2. Edit /etc/autofs/auto.master. Set proper --timeout, e.g., --timeout=60 sets auto-umount timeout to 60 seconds.
  3. A suggestion is to visudo your sudoers file with the line ALL ALL=(ALL) NOPASSWD: /usr/bin/killall -USR1 automount, which allows non-privileged user to umount unused mounts with killall -USR1 automount. See automount(8) for the explanation of -USR1 signal.
  4. Add a line automount: files to /etc/nsswitch.conf.
  5. Add automount's startup option to /etc/conf.d/autofs's daemonoptions option, e.g., daemonoptions='--verbose'. Start the service with /etc/rc.d/autofs start
Add the following line to your /etc/autofs/auto.master
/media/ssh /etc/autofs/auto.ssh --timeout=60
Suppose the SSH server ports for office and home are 22 and 20022, respectively. Edit /etc/autofs/auto.ssh as ( and are the host names of office and home, respectively).
office  -fstype=fuse.sshfs,rw,allow_other,IdentityFile=/home/wei/.ssh/id_dsa,port=22 \\:/home/pw/
home  -fstype=fuse,rw,allow_other,IdentityFile=/home/wei/.ssh/id_dsa,port=20022 \\:/home/pw/

Now, we need to establish a password-less ssh connection to both office and home. Do not worry, the "password-less" does not mean you go without encryption. It only means that you do not need password (or "to unlock the key with the passphrase" in ssh jargon) to ssh to office and home. To do this, we first need to generate a key (if you have not done so yet; check you $HOME/.ssh/ for files like id_dsa or id_rsa)

ssh-keygen -t dsa
Then, we copy the key to the office and home's authorized_keys. I used to do this with some thing like
cat ~/.ssh/ | ssh -p 22 'echo - >> ~/.ssh/authorized_keys'
But we can do it more conveniently with
ssh-copy-id -i ~/.ssh/
Unfortunately, my current ssh-copy-id does not has an option for specifying port. So we need to resort to the first method for home which has a non-default SSH server port of 20022.
cat ~/.ssh/ | ssh -p 20022 'echo - >> ~/.ssh/authorized_keys'

Now, before starting the autofs daemon automount, we need to do this important but easily forgotten step. I learn its importance the hard way; do not repeat my mistake.

What we need to do is to establish an SSH connection to both office and home as root. The reason for doing this is that, in the OpenSSH implementation, ssh will warn you about "unrecognized host" if that host's signature is not in the $HOME/.ssh/known_hosts file; ssh needs your explicit permission (i.e., input "yes" after the prompt) to establish the connection. Since automount daemon runs as root, it will connect to the remote host as root with ssh. If the remote host is not in root's .ssh/known_hosts, the background ssh will waits futilely for your confirmation and the automount will hung there forever.

OK, we know what we need to do. The actual doing is very simple.

sudo ssh -p 22 -i /home/pw/.ssh/id_dsa
sudo ssh -p 20022 -i /home/pw/.ssh/id_dsa

Finally, we can start the service with

/etc/rc.d/autofs start
Now you can access office and home as /media/ssh/office and /media/ssh/home, respectively. Neat, huh? :-)>

We can restart the service with

/etc/rc.d/autofs restart
or simply ask automount to re-read its configuration with
killall -HUP automount
Read the Notes section in the manpage automount(8).

Preparing Cheatsheet with LaTeX

Cheatsheet represents a class of docuemnt which requires maximal usage of white spaces on the paper. You certainly want to cram a lot of equations on the 1-page double-sided letter-sized notes to your exam. Here is how I do it with LaTeX.

You can take a look at the cheatsheet I prepares for the mid-term exam of the pattern recognition class here (and the LaTeX source here).

The content does not concern us here (and it is actually incomplete and probably contains a bunch of errors due to limited time I have in preparing it). The point is that it contains mathematical equations (which is TeX/LaTeX's strength) and that it needs to be done compactly (which is not what we get from LaTeX by default).

Here is how I do it. Plainly, I use a few LaTeX packages (geometry, multicol, mdwlist, and a few other standard ones such as graphicx, if you want to include graphics) and tweak the TeX/LaTeX parameters a bit. Documentation to these packages can be found at your local site (e.g. /usr/local/share/texmf/doc or by a Google query <package-name> latex filetype:pdf. Read them; documentation is your friend.

Without further ado, here is the template.


\usepackage{mdwlist} % use enumerate* and itemize* instead





%% End of preamble


\begin{multicols*}{3} % * version disables multicol's auto balancing
    {\LARGE \oops{Cheatsheet How-to}\vspace{.5ex}}
    {\large John Doe\vspace{.5ex}}
  \section{What Cheatsheet}
  \section{When Cheatsheet}
  \section{Why Cheatsheet}
  \subsection{How Cheatsheet}

Name it cheatsheet.tex and put the following Makefile along with it. Then, we can produce cheatsheet.pdf simply with make.

RM=rm -f

.PHONY: all clean dist-clean

all: $(TARGET).pdf 

	ps2pdf -dPDFSETTINGS=/prepress -dMaxSubsetPct=100 -dCompatibilityLevel=1.4 \
		-dSubsetFonts=true -dEmbedAllFonts=true $< %.dvi
	dvips -Ppdf -G0 -t landscape $<

%.dvi: %.tex
	latex $*
	latex $*

dist-clean: clean
	$(RM) *.dvi *.pdf *.eps
	$(RM) *.log *.aux *.out *.bbl
Moreover, we can clean the intermediate by-product with make make clean and all but the source with make dist-clean.