Browsing all articles tagged with samba
May
16

Howto: Mount your Android SD card under linux via wifi

This guide will get you from having an Android phone with a Samba sharing app installed (such as the one I currently use: https://market.android.com/details?id=com.funkyfresh.samba) and an Ubuntu (or any generic Linux) machine with Samba installed to being able to mount your SD card via WiFi. We will do this by: setting the computer to use netbios names for IP address resolution, testing the mount with mount.cifs, and then adding a permanent entry to /etc/fstab.

My setup: HTC Inspire 4g, rooted running Cyanomodgen 7; Samba Filesharing on Android app (from marketplace, free, version 110130-market); Ubuntu 11.04 (with samba package installed)

Prerequisites

First, you need a Samba server app for your phone (I’m using https://market.android.com/details?id=com.funkyfresh.samba, which seems to work well enough), then you need to go into your Samba app on the Android and set a netbios name for your phone (e.g. I’m using “bcoop-android”, so replace that with whatever name you set). For security reasons, you also want to set a user (e.g. “bcoop”) and a password required to access the share. If you have the option, you need to give the shared directory a name (on my app the name is fixed as “sdcard”).  You’ll also need to make sure that your computer has a samba client installed (this can be installed in Ubuntu by installing the smbclient package).

Using Netbios for IP address resolution

Secondly, we need to tell the computer to try to perform DNS lookups using netbios names if all else fails. You can tell if you need this step by running the command “ping bcoop-android” (making sure your phone is connected to the same network as the desktop via wireless and that the Samba app on the phone is running). If you receive a “unknown host” error, then the desktop is not able to look up names via netbios, which is a simple fix. Run the command

sudo gedit /etc/nsswitch.conf

and look for the line that starts with hosts:. At the end of this line, add wins. You should end up with the line looking something like:
hosts: files [...] wins

This will tell your machine to use Netbios name lookup if all else fails. You want to make sure to add wins at the end of the string of methods so that it does not check this before other methods. Save the file and close gedit.

Update: After an hour or so, my connection started timing out and I couldn’t remount the share.  I was confused about what was going on, but noticed that when I tried to ping the netbios name I suddenly got a response from a 209.XXX.XXX.XXX IP address, and not from my phone.  Long story short, it turns out that my lovely ISP (Comcast) has a policy of hijacking domain names that don’t exist so that they can redirect browsers to a search page with ads.  A side effect of this is that all DNS resolution requests are answered, regardless of whether they exist or not.  This causes the computer to assume that the request has been answered and not to look at the wins netbios name for a possible IP address.  The solution to this was to put wins just before the dns entry in the hosts line.

You should now be able to run the ping command and have the computer try to ping an IP address (it doesn’t matter if you receive a response or not, we’re just checking to see that the computer can translate the netbios name to the ip address of the phone).

Note: If you’re not able to get this to work, you can still move on, but just use the phone’s IP address instead of netbios name for the server.  It will be necessary to either continually change the IP address to the phone’s IP, tell your router to assign the phone the same IP address always, or to use some other method to ensure that the phone’s IP address remains correct.

Testing things out: Temporarily mounting the phone

To test things out, we need to create a directory to use as a mount point.  Run the command sudo mkdir /media/android to do this (using a different directory if you’d prefer). Now, we want to manually mount the phone in this directory. There are a couple different ways to do this, depending on how you want the file permissions to work.  I’ll list the different commands you can use, and you can see the below section for further discussion about which might be best.  You will need to modify this command with some specifics for your setup, see the section immediately afterwards.

To not allow any other users to access your files (the recommended method)

sudo mount -t cifs //bcoop-android/sdcard/ /media/android  -o user=bcoop,uid=bcoop,gid=bcoop,nounix,file_mode=0770,dir_mode=0770

To avoid using ‘nounix’, but allow others to read (but not write) your files

sudo mount -t cifs //bcoop-android/sdcard/ /media/android  -o user=bcoop,uid=nobody,gid=bcoop

To disable permission checking entirely (anyone can read/write your files)

sudo mount -t cifs //bcoop-android/sdcard/ /media/android  -o user=bcoop,noperm

For all methods

You’ll need to replace some parts of this command with your setup information:

  • //bcoop-android/sdcard should be your phone’s netbios name (or IP address) followed by the share name:  //NETBIOS_NAME/SHARE_NAME
  • /media/android should be your mount point directory
  • user=bcoop should be the user name that you set up on the phone for the Samba share:  user=PHONE_SAMBA_USER
  • uid=bcoop,gid=bcoop should be your computer user’s name and group (these are likely the same on a typical setup): uid=COMPUTER_USER,gid=COMPUTER_GROUP
  • uid=nobody should be the name of a fake user on your computer

After running the command, you’ll need to enter your sudo password, then your password for the phone’s samba share.  If all goes well, you should see no error messages then be able to run

ls /media/android

and see the contents of your phone. In that case, you’re ready to set the share up permanently. If you don’t mind running the mount command every time, you can just stop here.

Notes regarding file permissions

(This section can safely be skipped if you’re not interested in knowing any background about how things work behind the scene…)

When I was trying this out, the thing that took the most amount of time to figure out is the file permissions used on the phone.  When mounting a SMB (Samba) share, there are a few options when it comes to file permissions: accept the uid/gid (user and group owner id) from the phone, force the uid/gid to map to specific users/groups on the computer, or ignore the permissions reported by the phone entirely.

The most convenient option is to ignore the permissions entirely, but it is also the least secure: it would allow any program or user on your computer to have full access to the files on the phone when it is mounted.  The typical approach is to map the user and group from the phone to be equivalent to your computer user.  However, I noticed something odd about the way the permissions are reported on my setup.  I’m not positive if this is just some eccentricity of my specific setup, but the permissions reported by my phone have the user set with no read/write/execute permission, the group set with full read/write/execute permission, and everyone else set with just read/execute permission.  (For comparison, the typical setup is user with full permission, group with either full or read/execute permission, and everyone else with either read/execute permission or no permissions at all.)  So, if one maps the uid/gid to the computer user’s uid/gid then the result is that the current user will have no permissions at all.  One solution is to map the gid to the computer user’s gid, but to map the uid to some fake/unused user (I used ‘nobody’, which is a standard and safe bet).  This results in you having full access to the phone and other users being able to read but not modify the contents, and has the advantage of retaining the maximum amount of functionality (i.e. it doesn’t disable some behind-the-scenes filesystem functionality).  An alternative solution is to disable ‘CIFS Unix Extensions’ and manually set the file/directory permissions as well as the uid/gid.  This has the advantage of allowing you to explicitly remove read permission from other users if desired, but has the possible disadvantage of disabling something that is required (though I have no idea if that is likely to happen or even really possible; please leave a comment if you know something about this that I don’t).

Setting things up permanently

To permanently save these settings, we need to create a credentials file to safely hold your samba share’s username and password and we also need to add the mount information to /etc/fstab so that the system is aware of the settings.  To safely store your credentials, we want to create a file that only your user can read which holds your username and password.  To do that, run

gedit ~/.android_credentials

and add the following to this file:

username=YOUR_USERNAME
password=YOUR_PASSWORD

Save the file, close gedit, and run the command

chmod 0600 ~/.android_credentials

to make sure that only you can read that file.

Now, to save the information into fstab, run

sudo gedit /etc/fstab

and add the following line to the file:

//bcoop-android/sdcard/	/media/android	cifs	credentials=/home/bcoop/.android_credentials,uid=bcoop,gid=bcoop,nounix,file_mode=0770,dir_mode=0770,user,noauto	0	0

(Note that you may need to change the options if you’d like something other than the recommended method from above, and you’ll need to replace credentials=/home/bcoop/.android_credentials with the correct path to your credentials file. Also note that the trailing slash on //bcoop-android/sdcard/ is very important. If you forget this trailing slash then you cannot unmount the share as a regular user.)

 

If all goes well you should now be able to run

mount /media/android

and access your phone’s SD card contents in /media/android. Remember that you need to unmount this by running

umount /media/android

when done. Enjoy!

 

%d bloggers like this: