Automatically Mounting Disk(s) as User on Login

December 7, 2010 Leave a comment

This post is meant as a step-by-step guide to modifying /etc/fstab to automatically mount one or more disks as a given user when logging in. All of the commands and instructions that follow were originally executed in Fedora 13 but should apply to most Linux distros. The motivation for this post is that my initial attempts at modifying /etc/fstab to automatically mount my drives worked in the sense that the drives were mounted but they were mounted as root and I could not perform file operations n the files/folders on the disks. Following the steps below will help you gather all the information and make all the necessary edits to automatically mount the disks as some user of the system.

Step 1 – Identify the Disks
The first step is to identify which disks(and more specifically which partitions) we want to mount. All the device files for your machines devices are kept in the /dev directory. The device files this post is concerned with will be of the type:

  • /dev/hd* – /dev/hda is the master drive and /dev/hdb is the slave drive on the primary IDE controller. /dev/hdc and /dev/hdd are the master and slave devices on the secondary IDE controller.
  • /dev/sd* – /dev/sda is the first SCSI drive. The second SCSI drive would be /dev/sdb, the third /dev/sdc, and so on.

The information above describes where the device files live but doesn’t really say which device is which. For the rest of this tutorial, I will be referring to two drives: a 200GB SCSI drive(200GB) and a 300GB SCSI drive(300GB). These are the drives that I will be automounting. To get more information on the connected drives, execute the following command(you may or may not need sudo here):

$> sudo fdisk -l

You should see information on all the connected drives and the partitions on those drives. Here is the pertinent information on the drives I am concerned with:

Disk /dev/sdb: 203.9 GB, 203928109056 bytes
255 heads, 63 sectors/track, 24792 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb5586ef8

Device Boot Start End Blocks Id System
/dev/sdb1 * 2 24792 199133707+ f W95 Ext'd (LBA)
/dev/sdb5 2 24792 199133676 7 HPFS/NTFS

Disk /dev/sdc: 300.1 GB, 300069052416 bytes
255 heads, 63 sectors/track, 36481 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2d4f45cd

 

Device Boot Start End Blocks Id System
/dev/sdc1 * 2 36481 293025600 f W95 Ext'd (LBA)
/dev/sdc5 2 36481 293025568+ 7 HPFS/NTFS

Looking at the output above I can see that I will want to mount /dev/sdb5(the logical partition on the 200GB drive) and /dev/sdc5(the logical partition on the 300GB drive). Both of these partitions are of type NTFS.

Step 2 – Identifying the User Identifier(User ID) and Group Identifier(GID) of the User to Mount as:
On my machine, I have a user named karsmic. I need to get the UID and GID for the user karsmic because I will need this when I modify /etc/fstab later. This information can be found in the /etc/passwd file. Execute:

$> cat /etc/passwd

and you should see a bunch of lines containing information on the users for your system. I have included the line for the user karsmic on my machine below.

karsmic:x:500:500:karsmic:/home/karsmic:/bin/bash

Each line in /etc/passwd is made up of seven fields where each field is separated by a colon(:). The UID and GID are the third and forth fields, both 500 in this case. These and the other five fields are discussed briefly below.

  1. The username – karsmic in the example above.
  2. Used to hold information to check the user password. An x means that the password info is stored in the shadow password file – x in the example above.
  3. The user identifier – 500 in the example above.
  4. The group identifier – 500 in the example above.
  5. Used to hold any additional information about the user(Full name, email, phone number, etc.) – karsmic in the example above.
  6. The path to the users home directory – /home/karsmic in the example above.
  7. The program or command to execute when the user logs on – /bin/bash in the example above.

So from step 2 we see that the UID and GID for the user we want to mount as are both 500.

Step 3 – Creating the Mount Points:
We need some place to mount the devices that we identified in step 1. The following commands will create two mount points, one for each of the drives from step 1 in the /mnt directory.

$> mkdir /media/200GB
$> chown karsmic.karsmic /media/200GB
$> mkdir /media/300GB
$> chown karsmic.karsmic /media/300GB

WARNING: Replace karsmic.karsmic with the user and group with the UID and GID from step 2. Note that I could have created /media/music and /media/movies if my drives held music and movie files respectively. The names do not really matter I just made them 200GB and 300GB so that the mount points would be indicative of the drive that I mounted there and to highlight what was going on in this example.

Step 4 – Modify /etc/fstab:
The final step is to take the information and output of the steps above and edit /etc/fstab with that information. Open /etc/fstab in an editor as root and you should see something similar to:

#
# /etc/fstab
# Created by anaconda on Sat Oct 16 12:29:11 2010
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_yellowtower-lv_root / ext4 defaults 1 1
UUID=8cd76efe-f3a0-41ef-b753-1087a885b9bc /boot ext4 defaults 1 2
/dev/mapper/vg_yellowtower-lv_home /home ext4 defaults 1 2
/dev/mapper/vg_yellowtower-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

We will be adding information to the end of the file. Each line is made up of six columns. These columns are described in detail below.

  1. The device or partition to mount.
  2. The mount point(in the example output above, devpts is mounted to /dev/pts).
  3. The file system type(ext3, ext4, ntfs, etc.).
  4. The mount options. This is the most important part of this post. We will also use the uid and gid options here.
    • auto/noauto – auto will force the device to be mounted automatically at bootup while noauto requires you to mount the device manually. auto is the default.
    • user/nouser – nouser means only root can mount the device. user allows any normal user mount the device. nouser is the default.
    • exec/noexec – exec allows the execution of binaries on the partition. noexec prevents the execution of binaries from that partition. exec is the default
    • ro – mount the partition as read only.
    • rw – mount the partition as read-write. This is the default.
    • sync/async – sync means I/O is done synchronously. async means I/O is done asynchronously.
    • defaults – You can use this keyword in the 4th column to use all the default values for the mount options, namely: auto, nouser, exec, rw, async.
  5. The dump option. Essentially determines if dump should backup the drive or not. Setting this to 0 means dump will ignore the filesystem. We will be setting this to 0.
  6. The fsck option. fsck checks this value to determine if and when the filesystem should be checked. If this is 0, fsck will never check the file system. We will be setting this to 0.

Based on the information we gathered in the steps above, adding the following two lines to the end of  /etc/fstab will mount both our partitions as ntfs filesystems automatically at bootup as the user we specified in step 2 to the mount points we created in step 3.


/dev/sdb5 /media/200GB ntfs user,uid=500,gid=500 0 0
/dev/sdc5 /media/300GB ntfs user,uid=500,gid=500 0 0

With the exception of user instead of nouser, all the default mount options are used. We also include the additional mount options uid= and gid= so that the owner and group are set correctly on the contents of the filesystems we mount.

Please post any issues with the above tutorial as well as any questions in the comments.

Resetting the root password in OpenWrt

May 5, 2010 6 comments

I recently fired up one of my wireless routers(a Linksys WRT54GL already running OpenWrt) that I had not used in a while and needed to make some changes to the configuration. Unfortunately, I had forgotten the password so I could not SSH in or use the web interface. Since this seems like a common problem I decided to create this post covering resetting the root password in OpenWrt. The procedure to reset the root password is listed as a step by step process below.

  • Step 1 – Disconnect power to the router and connect your computer to one of the switch ports on the router. NOTE: For the rest of this tutorial I will assume that the router is 192.168.1.1 but this may change based on your configuration. Replace 192.168.1.1 with the IP of the router if yours has a different IP.
  • Step 2 – Give your computer a static IP. In my case I used an IP of 192.168.1.100 and a subnet mask of 255.255.255.0. You can do this with:
    sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

    You do not need to set the gateway or DNS information.

  • Step 3 – Boot the router in failsafe mode by doing the following:
    1. Connect power to the router.
    2. Wait for the DMZ light(this might be a different light on other brands of routers) to come on and then hit the “Reset” button.
    3. If the above operation worked, the DMZ and power lights will both be blinking indicating that the router is in failsafe mode.
  • Step 4 – Now that the router is in failsafe mode, connect to the router using:
    telnet 192.168.1.1

    If you are successfully connected, you should see the following screen:

    BusyBox v1.11.2 (2009-12-02 06:19:32 UTC) built-in shell (ash)
    Enter 'help' for a list of built-in commands.
    
      _______                     ________        __
     |       |.-----.-----.-----.|  |  |  |.----.|  |_
     |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
     |_______||   __|_____|__|__||________||__|  |____|
              |__| W I R E L E S S   F R E E D O M
     KAMIKAZE (8.09.2, r18961) -------------------------
      * 10 oz Vodka       Shake well with ice and strain
      * 10 oz Triple sec  mixture into 10 shot glasses.
      * 10 oz lime juice  Salute!
     ---------------------------------------------------
    root@OpenWrt:~#
  • Step 5 – You are now connected to the router but you cannot yet change the root password because the JFFS is still in read only(RO) mode. To change the JFFS to read-write(RW) mode use the command
    mount_root

    and the JFFS will be remounted in RW mode.

  • Step 6 – Now you can change the root password using by issuing the command:
    passwd

    and entering your new root password twice. After entering the password, reboot the router and you should be able to log in to the router using

    ssh root@192.168.1.1

    and entering the password you just set in the telnet session.

Hopefully this post includes all the information you will need to reset the root password on your OpenWrt installation. Please let me know if there are any issues with the instructions and ask any questions you might have in the comments.