How To: Network Trash on Ubuntu File Server (NAS) with SFTP (SSH + Fuse) and AFP (netatalk)
Purpose: create a Network Trash System for a Ubuntu Linux file server (NAS). Reason being: by default, files deleted on file server go away permanently (as they do when you run rm from the command line). On my Mac (OS 10.4) and on Ubuntu (7.04) if I connect either via AFP (through netatalk) or SSH (SFTP through Fuse) and delete a file, that file is gone forever! This is a problem, because often I find I want them back. Enter: libtrash!
Tested on: Ubuntu 6.06.1 and 6.10.
install libtrash and test it out
First step is to install libtrash from aptitude:
$ sudo aptitude update && sudo aptitude install libtrash
Unlike a lot of software on linux, installing it isn’t quite enough to get it running. I think this is because it is a lib package and not an actual program — it seems to be meant to be used by other programs in the background and not directly interact with the user (in the way that I want it to).
To test it out briefly, run the following to start the libtrash engine in your terminal prompt:
$ export LD_PRELOAD=/usr/lib/libtrash/libtrash.so.2.4
You’ll notice the .2.4 at the end of the first line — that may change, I would imagine, if libtrash is upgraded. I recommend when you type in this line to hit the TAB key at or around .so to have it automatically fill in the rest. (And remember what it is, because you will need to use it later.) After the above command has run, you can do a little test (the touch command doesn’t seem to work, so create a real file). Once you’ve created the file, delete it, and it should be in your ~/Trash folder.
If it isn’t working at this point, check out the documentation at: /usr/share/doc/libtrash/
To make sure libtrash is running every time you login via the terminal (command prompt) be sure to edit in the following line:
$ sudo nano /etc/profile
#/etc/profile: system-wide .profile file for the Bourne shell (sh(1))# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).export LD_PRELOAD=/usr/lib/libtrash/libtrash.so.2.4[ ... ]
Now you are all set in the terminal.
There are a few more steps that need to be taken in order to begin utilizing Trash folders for the users on your system. For each method of connecting/utilizing the files on the server (through the command line, through sshFS, or AFP), the LD_PRELOAD option needs to be called for libtrash. Also, I recommend you use one of the Trash cleaning scripts (outlined below).
using libtrash with SFTP (sshFS)
I like to mount my home server remotely using sshFS (which utilizes FUSE). sshFS can be used on the Mac through the MacFUSE project or on Ubuntu through Places/Go to Server Menu on 7.04). To initialize libtrash, we need to modify which script is called when the file system is first mounted to ensure that libtrash is started prior to mounting.
NOTE: this assumes you are using openssh-server.
Create a script in nano with the following text in the following location:
$ sudo nano /usr/lib/openssh/libtrash-sftp-server
In nano, enter the following code and then save the file:
#!/bin/bashexport LD_PRELOAD=/usr/lib/libtrash/libtrash.so.2.4/usr/lib/openssh/sftp-server
Change permissions to make it executable:
sudo chmod +x /usr/lib/openssh/libtrash-sftp-server
Then open the configuration file for ssh and make the following change near the very end of the file (comment out the original and add your own):
$ sudo nano /etc/ssh/sshd_config
[ ... ]#Subsystem sftp /usr/lib/openssh/sftp-serverSubsystem sftp /usr/lib/openssh/libtrash-sftp-serverUsePAM yes
Restart your ssh server and then, when you connect via sshFS (in Ubuntu go to Places < Connect to Server and choose a SFTP (SSH) server; on the Mac, you will need MacFuse and SSHFS) you'll be using your ~/Trash folder.
$ sudo /etc/init.d/ssh restart
using libtrash with AFP (netatalk)
We need to edit one file in a really strange place in order for this to work. You probably could make a backup copy, if you are afraid of messing something up.
$ sudo nano /etc/init.d/netatalk
In the middle of this file, you should find this patch of code; insert the highlighted text where it lies:
case "$1" instart)export LD_PRELOAD=/usr/lib/libtrash/libtrash.so.2.4if test "x${ATALK_BGROUND}" = "xyes"; thenecho "Starting AppleTalk services in the background."atalk_startup >/dev/null &elseecho -n "Starting AppleTalk services (this will take a while): "atalk_startupecho "."fi;;
After, simply restart netatalk. You may always want to look at how to securely authenticate your user's login with netatalk as well.
$ sudo /etc/init.d/netatalk restart
how to empty the trash automatically
If you look in /usr/share/doc/libtrash you will find a lot of interesting information as well as some examples. In the examples directory there are scripts for cleaning your trash can (/usr/share/doc/libtrash/examples/cleanTrash/). I like strash the best. If you do this, you can extract the script, install it to a usable path, and setup the man file (so you can read about its cleverness).
$ mkdir -p ~/src$ cd ~/src$ cp /usr/share/doc/libtrash/examples/cleanTrash/strash-0.9.tar.gz ~/src/strash.tar.gz$ tar -xvf strash.tar.gz$ sudo cp ~/src/strash-0.9/strash /usr/sbin/strash$ sudo cp ~/src/strash-0.9/strash.8 /usr/share/man/man8/strash.8
You can read the man page for strash now, which will show you all the nifty options. I added a line to my root crontab (by running: sudo crontab -e) that deletes all files over one month old and will remove files (oldest first) when the total size of the ~/Trash folder is greater than 5GB:
# m h dom mon dow command30 01 * * * /usr/sbin/strash --age 1M --size 5G
Seems to work well. Enjoy.

I have installed netatalk on ubuntu 9.10 and works fine
I have installed libtrash and and added LD_PRELOAD into /etc/profile and it works
I have added LD_PRELOAD into /etc/init.d/netatalk and netatalk do not restart
I get a segmentation fault
I read syslog this ‘cnid_metad: error binding to socket for localhost: Address already in use’
Can you help me?
Thanks
Hi Paul – a couple questions: if you comment out the line you added to
/etc/init.d/netatalkdoes netatlk restart? Also, did you install libtrash from the repositories or from the source? The last time I tried this (8.06) I had to install from the source (see the comment above yours). Am not sure, off the top of my head, what’s going on yet …Hi Damon,
thank you for the quick reply.
The answers are:
a) yes, without LD_PRELOAD all works fine
b) I installed from repository, the libtrash-latest.tgz do not compile, after make I get a lot of errors.
errata corrige
the latest version works.
Thank you
In case folks were wondering – this wasn’t working for me in 8.06 … so, I got the source and used that instead:
$ wget http://pages.stern.nyu.edu/~marriaga/software/libtrash/libtrash-latest.tgz$ tar xzf libtrash-latest.tgz$ cd libtrash-3.2/$ make$ sudo make installThen you have to update the LD_PRELOAD call!
$ export LD_PRELOAD=/usr/local/lib/libtrash.soFor me, this worked.
Hmm, I see what you mean now — well, running the
LD_PRELOADoption in the terminal before runninggnome-terminalis probably not going to work. When it is run in the terminal it applies to the terminal. If you look at the examples above (for SSH and AFP), both require that modifications be made to how the programs are started internally (that is, theLD_PRELOADis added to the beginning of their start sequence.My guess is that you would need to look at the command
gnome-terminaland see what it depends on to start and access the files. But, to be honest, not sure where to look or if it will even work in the end. I would guess it can, but I’m just not sure. Sorry!I’m using nautilus (or thunar, or konqueror…) to browse a local folder. (But using libtrash with graphical file manager while browsing network folders would be good idea!) Of course all of these applications have a “Trash” but all of them use different implementations. In addition, when you’re drunk or high, you can “Shift + Del” your files!
Are you using Nautilus to browse a networked folder or a local folder? If you are using a local folder, perhaps you already have a “Trash” feature that is interfering (I know my installation does all its own trash keeping); if you are browsing a network folder, how are you connecting to the server? Nautilus is just the file browser … what is the actual connection: AFP, SSH, NFS, SAMBA, etc?
Don’t worry, my mistake ;)
I’ve mean using libtrash in a desktop environment. It looks like some graphical programs (eg. nautilus) ignore LD_PRELOAD even if they’ve been started from shell (gnome-terminal).
I’m not sure I know … or understand the question fully: do you mean login “remotely” to your server via GDM or KDM ? I have never done that — sorry. I suspect there is a way, however, I don’t know what it is. Let me know if you find one.
How can I use libtrash if I login via GDM or KDM?