Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing a charm in which I need to change user to install some packages. When I start juju-deploy all this running as root... So, I try to change my user and is not possible. If anyone has any idea how to solve this, agradeceria ... I tried many things in the install script made ​​in bash but I have not accomplished anything.

I'm try this... This is a part of my install script on hooks directory:

mkdir -p $dir
useradd -d "$dir" -s "$bash" -g "$group" --create-home  $user
echo "$user:$pass" | chpasswd
cp /etc/skel/.bash_logout /etc/skel/.bashrc /etc/skel/.profile $dir
su - "$user"

It is assumed that this part should change the user and continue running the script with the user created and not as root. But that is not happening

share|improve this question
    
Hey, --create-home will automatically copy /etc/skel templates, so the line where you do this is not necessary. –  roadmr Aug 25 at 18:31

3 Answers 3

Your install script could be failing for multiple reasons:

  • If the group does not previously exist the useradd command will throw an error:
    useradd: group 'coolgroup' does not exist
    • The solution to this problem is to make the group before calling useradd.
  • If the parent directory of the user home ($dir in your example) does not exist the useradd command will raise an error:
    useradd: cannot create directory /non/default/home/dir
    • The solution to this is to create the parent directory if it does not already exist.
  • If the home directory already exists useradd will not copy the skeleton files:
    useradd: warning: the home directory already exists. Not copying any file from skel directory into it.
    • The solution to this problem is to create the parent directory (using dirname). This may be why your original script copies the skel files over to the user directory.

You can see these errors in the juju log file for the charm unit. It is highly recommended to run bash scripts with set -e so the script stops at the first error. I also like to use set -x to show the commands being run in the log file.

I wrote a quick charm to test this problem out:

$ mkdir /tmp/trusty
$ cd /tmp/trusty
$ charm create user -t bash
$ vi user/hooks/install
$ juju deploy --repository=../../ local:trusty/user

This creates a skeleton "user" charm, I then edited user/hooks/install with the following:

#!/bin/bash

set -ex 

dir=/non/default/home/dir
bash=/bin/bash
user=newuser
group=coolgroup
pass="JujuIsMagic!"

# The group must be created before trying to add the user.
groupadd "$group"
#  The parent directory must be created if this directory is not standard. 
mkdir -p `dirname $dir`
useradd -d "$dir" -s "$bash" -g "$group" -m $user
echo "$user:$pass" | chpasswd

su - "$user" --command 'whoami'
su - "$user" --command 'ls -al $dir'

The output of the install script for the user charm is:

$ tail -F /var/log/juju-mbruzek-local/unit-user-0.log
2014-08-25 14:49:47 INFO install + dir=/non/default/home/dir
2014-08-25 14:49:47 INFO install + bash=/bin/bash
2014-08-25 14:49:47 INFO install + user=newuser
2014-08-25 14:49:47 INFO install + group=coolgroup
2014-08-25 14:49:47 INFO install + pass='JujuIsMagic!'
2014-08-25 14:49:47 INFO install + groupadd coolgroup
2014-08-25 14:49:47 INFO install ++ dirname /non/default/home/dir
2014-08-25 14:49:47 INFO install + mkdir -p /non/default/home
2014-08-25 14:49:47 INFO install + useradd -d /non/default/home/dir -s /bin/bash -g coolgroup -m newuser
2014-08-25 14:49:47 INFO install + echo 'newuser:JujuIsMagic!'
2014-08-25 14:49:47 INFO install + chpasswd
2014-08-25 14:49:47 INFO install + su - newuser --command whoami
2014-08-25 14:49:47 INFO install newuser
2014-08-25 14:49:47 INFO install + su - newuser --command 'ls -al $dir'
2014-08-25 14:49:47 INFO install total 20
2014-08-25 14:49:47 INFO install drwxr-xr-x 2 newuser coolgroup 4096 Aug 25 09:49 .
2014-08-25 14:49:47 INFO install drwxr-xr-x 3 root    root      4096 Aug 25 09:49 ..
2014-08-25 14:49:47 INFO install -rw-r--r-- 1 newuser coolgroup  220 Apr  8 20:03 .bash_logout
2014-08-25 14:49:47 INFO install -rw-r--r-- 1 newuser coolgroup 3637 Apr  8 20:03 .bashrc
2014-08-25 14:49:47 INFO install -rw-r--r-- 1 newuser coolgroup  675 Apr  8 20:03 .profile

You can see the skeleton files are copied over from the useradd command. You can run individual commands as the other use with su or sudo from the hook files.

share|improve this answer
    
Nice comprehensive answer! –  lazyPower Aug 25 at 20:16

The use of 'su - "$user"' in your script will end up starting an interactive shell for the specified user.

You could use the '--command' option to 'su' to specify a command to run. Then you would encapsulate the commands you want run as that user in a script which could be created as a bash here document.

Your script would look like this...

mkdir -p $dir
useradd -d "$dir" -s "$bash" -g "$group" --create-home  $user
echo "$user:$pass" | chpasswd
cp /etc/skel/.bash_logout /etc/skel/.bashrc /etc/skel/.profile $dir
# Create a temporary file for the user script
TMPFILE=$(mktemp)
chmod u+x ${TMPFILE}
chown ${user}:${group} ${TMPFILE}

## Create a bash here document (http://tldp.org/LDP/abs/html/here-docs.html)
## with a script to execute as the user.
cat > ${TMPFILE} << EOF
#!/bin/bash
## Run `whoami` to prove that this is run by the correct user
whoami | tee ~/whoami.out
EOF 

su - "$user" --comand ${TMPFILE}
share|improve this answer

Since your script runs as root, you can also use sudo to change to the other user on a per-command basis. This may be a bit more cumbersome than rcj's suggestion, but it should work. Another possible advantage is that you still deal with only one script, as opposed to two/several with the other solution.

mkdir -p $dir
useradd -d "$dir" -s "$bash" -g "$group" --create-home  $user
echo "$user:$pass" | chpasswd
cp /etc/skel/.bash_logout /etc/skel/.bashrc /etc/skel/.profile $dir
# Starting here, preface user's commands with sudo -u user
sudo -u user some-command-run-as-user.sh
echo "some file you need written as user | sudo -u user tee filename.txt
sudo -u user another-command-run-as-user.sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.