0
votes

I pass the following as my GCE startup script but it always logs in as the root user and never as the demo-user. How do I fix it?

    let startupScript = `#!/bin/bash
    su demo-user
    WHO_AM_I=$(whoami)
    echo WHO_AM_I: $WHO_AM_I &>> debug.txt
    cd..`
1

1 Answers

0
votes

I think it should work like that:

#! /bin/bash
sudo -u demo-user bash -c 'WHO_AM_I=$(whoami);
echo WHO_AM_I; $WHO_AM_I &>> debug.txt;'

use "sudo-u" to specify the user, then bash -c 'with all the commands between these particular quotes '' and separated by ;

For example: bash -c 'command1; command2;'

You can try an easier test (it worked for me), for example:

#! /bin/bash
sudo -u demo-user bash -c 'touch test.txt'

And then check with ls -l /home/demo-test/text.txt that demo-test is the owner of the new file.