Incus exec -> $HOME not set

me:2001

incus exec me-workspace --user 2001 -- sh -c 'echo $HOME'

EMPTY

incus exec me-workspace -- sh -c 'echo $HOME'

/root

incus exec me-workspace -- su --login me
me@me-workspace:~$ echo $HOME

/home/me

Why, please?

For the root user, we can guess what the home directory is (usually /root).
For a random uid provided by the user, we can’t do that as we can’t resolve the uid to a name without having to run code inside of the container or VM which is unsafe.

1 Like

I see. What would be the recommended way if I want to run commands dependent on $HOME env var or some other user var? Should I set it manually with --env? Thanks.

Try using the username instead of uid:

incus exec me-workspace -- sudo --login --user me sh -c "echo $HOME"

Or nest two commands:

incus exec me-workspace -- sudo --login --user $( incus exec me-workspace -- getent passwd 2001 | cut -d: -f1 ) sh -c "echo $HOME"

@qkiel username doesn’t work

Error: invalid argument "me" for "--user" flag: strconv.ParseUint: parsing "me": invalid syntax

Nested commands could be a way, but I need to call this from REST API /1.0/instances/{name}/exec as well. Thanks.

1 Like

incus query --request POST --wait /1.0/instances/{name}/exec --data ‘{ “command”: [“sh”, “-c”, “echo $HOME”],“environment”: {“HOME”: “/home/me”}}’

that’s the commend you want.

1 Like

Yeah, that’d be the way to go.

1 Like