How To: Network Trash on Ubuntu File Server (NAS) with SFTP (SSH + Fuse) and AFP (netatalk)

Purpose: create a Network Trash functionality for a Ubuntu Linux file server (NAS). Reason being: by default, files deleted from the command line on file server go away permanently. If I am connected to my file server from my Mac 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, 6.10, 10.04.

install libtrash and test it out

The version of libtrash in the repository is not the latest — I prefer to grab it from source which, in this case, is very easy:

  • sudo aptitude install build-essential
  • wget http://pages.stern.nyu.edu/~marriaga/software/libtrash/libtrash-latest.tgz
  • tar xzf libtrash-latest.tgz
  • cd libtrash-3.2/
  • make
  • sudo make install

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/local/lib/libtrash.so

After the above command has run, you can do a little test by creating a file (the touch command doesn’t seem to work, so create a real file) and then delete it. It should show up 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 you need to add the export command to the top of /etc/profile:

  • 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/local/lib/libtrash.so

[...]

You should be all set in the terminal, at this point.

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 create small script that loads the module just before the sftp-server is started and then tell openssh (in my case) to run that script (rather than starting the sftp-server directly).

I created by file at /usr/local/lib/libtrash-sftp-server and entered:

#!/bin/bash
export LD_PRELOAD=/usr/local/lib/libtrash.so
/usr/lib/openssh/sftp-server

Change permissions to make it executable:

  • sudo chmod +x /usr/local/lib/libtrash-sftp-server

Then open the configuration file for ssh (/etc/ssh/sshd_config) and make the following change near the very end of the file (comment out the original and add your own):

#Subsystem sftp /usr/lib/openssh/sftp-server
Subsystem sftp /usr/local/lib/libtrash-sftp-server

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)

I was able to use libtrash with netatalk by inserting the following line in the /etc/init.d/netatalk file (10.04 version shown here):

case "$1" in
	start)
        export LD_PRELOAD=/usr/local/lib/libtrash.so
		if [ "x$ATALK_BGROUND" = "xyes" ]; then
			echo "Starting Netatalk services in the background."
			atalk_startup >/dev/null &
		else
			echo -n "Starting Netatalk services (this will take a while): "
			atalk_startup
			echo "."
		fi
	;;

I added line 90. After, simply restart netatalk.

  • sudo /etc/init.d/netatalk restart

how to empty the trash automatically

If you look in ~/src/libtrash-x.x/cleanTrash/ you will find a couple different cleaning scripts. 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).

  • cd ~/src/libtrash-3.2/cleanTrash/
  • tar xvf strash.tar.gz
  • sudo cp strash-0.9/strash /usr/local/sbin/strash
  • sudo mkdir -p /usr/local/share/man/man8
  • sudo cp 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 week old:

# m h  dom mon dow   command
30 01 * * * /usr/sbin/strash --age 7d

Seems to work well. Enjoy.

14 Comments (newest first)

  1. Aaron says:

    Can this be used when using sshd’s internal-sftp?

    I haven’t found a good way to do it yet. It might be possible using PermitUserEnvironment, but I would rather not enable that.

    • Aaron says:

      Incase anyone else is looking to use libtrash with a SFTP-only chroot:

      This is not possible using internal-sftp directly, unless you preload libtrash into sshd itself (ie via init script).

      I needed libtrash for a particular group only, so I ended up using sftp-server like here and setting up files required in the chroot, similar to what is done here:
      http://www.howtoforge.com/chrooted-ssh-sftp-tutorial-debian-lenny

      Here’s the file structure I needed:
      libtrash.so sftp-server sh Trash

      ./dev:
      null pts tty zero

      ./dev/pts:
      1 ptmx

      ./etc:
      localtime passwd

      ./lib:
      ld-linux.so.2 libc.so.6 libdl.so.2 libnss_files.so.2 libreadline.so.6 libtinfo.so.5

  2. 10a fuse says:

    10a fuse…

    […]How To: Network Trash on Ubuntu File Server (NAS) with SFTP (SSH + Fuse) and AFP (netatalk) « damontimm.com[…]…

  3. Paul says:

    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

    • Damon says:

      Hi Paul – a couple questions: if you comment out the line you added to /etc/init.d/netatalk does 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 …

      • Paul says:

        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.

  4. Damon says:

    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 install

    Then you have to update the LD_PRELOAD call!

    $ export LD_PRELOAD=/usr/local/lib/libtrash.so

    For me, this worked.

  5. Damon says:

    Hmm, I see what you mean now — well, running the LD_PRELOAD option in the terminal before running gnome-terminal is 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, the LD_PRELOAD is added to the beginning of their start sequence.

    My guess is that you would need to look at the command gnome-terminal and 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!

  6. anonymous says:

    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!

  7. Damon says:

    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?

  8. anonymous says:

    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).

  9. Damon says:

    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.

  10. anonymous says:

    How can I use libtrash if I login via GDM or KDM?