1

I'm attempting to write a setup script that I can run on multiple machines to run a bunch of installers (requires sudo) but also do things like pull down a git repo (I want to run that as the normal user so they will have ownership + r/w/x permissions on them). I am using Ubuntu 14.04 and bash for my scripts (willing to switch them to zsh if necessary but would like to leave them as bash for portability)

I did some reading and found the here document syntax for running several commands as the same user.

However I am getting the Sessions still open, not unmounting error message displaying.

How can I fix this? Is this an issue? Is there a better way to drop in and our of sudo?

Here's the script I am using to test the dropping in/out of sudo and the output.

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "[FAIL] This script must be run as root"
   exit 1
fi

sudo -u myusername bash << \EOF
echo ""
echo "I SHOULD BE A REGULAR USER 0"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""
EOF

echo ""
echo "I SHOULD BE SUDO'D 1"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""


sudo -u myusername bash << \REG_USER_END
echo ""
echo "I SHOULD BE A REGULAR USER 2"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""
REG_USER_END

echo ""
echo "I SHOULD BE SUDO'D 3"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""

exit 0;

Output:

I SHOULD BE A REGULAR USER 0
uid is '1000'
euid is '1000'
user is 'myusername'
username is 'myusername'

Sessions still open, not unmounting

I SHOULD BE SUDO'D 1
uid is '0'
euid is '0'
user is 'root'
username is 'root'


I SHOULD BE A REGULAR USER 2
uid is '1000'
euid is '1000'
user is 'myusername'
username is 'myusername'

Sessions still open, not unmounting

I SHOULD BE SUDO'D 3
uid is '0'
euid is '0'
user is 'root'
username is 'root'
  • Perhaps because you are not explicitly exiting from the sudo bash shell at the end of each heredoc? FYI the actuall error seems to be from ecryptfs - see Encrypted home directory doesn't always dismount after logout.. – steeldriver Aug 6 '15 at 18:20
  • I'll try using exit but I don't think that solved the issue. And ah! Great that at least explains the issue, thanks :) – James Aug 7 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.