Linux - Using PAM_EXEC to mount userspaces
PAM_EXEC - What is it?
On a Linux box, users are authenticated via a PAM module. These modules check passwords, and do tasks before the shell is presented.PAM_EXEC will allow you to run a script when the user logs on/out or changes passwords.
My Mission
The main aim was to mount a NFS space under the users $HOME directory when they login. This allows for shared storage to be used and no-matter what server the user logs in to, their NFS shares are present.My script
A file named /usr/local/bin/mounthome.sh with the below contents. Set the file permissions to execute using chmod +x /usr/local/bin/mounthome.shChange the mount server and volumes to suit your own server needs. The below will create a directory called nfs and mount the NFS server under it
#!/bin/bash
#
# "User: $PAM_USER"
# "Remote Host: $PAM_RHOST"
# "Service: $PAM_SERVICE"
# "TTY: $PAM_TTY"
if [ "$PAM_TYPE" == "open_session" ]
then
mkdir /home/$PAM_USER/nfs
mount -t nfs -o user server.local:/volume1/$PAM_USER /home/$PAM_USER/nfs
chmod 777 /home/$PAM_USER/nfs
chown $PAM_USER:$PAM_USER /home/$PAM_USER/nfs
fi
if [ "$PAM_TYPE" == "close_session" ]
then
umount /home/$PAM_USER/nfs
fi
exit 0
Executing it using PAM
Modify/etc/pam.d/common-session
file accordingly:session optional pam_exec.so /usr/local/bin/mounthome.sh