1

I'm trying to run some automated scripts from root that will start running other scripts as another user in a "tmux" session.

Example:

File 1 - "start_test_script.sh" - in "root" home directory:

#!/bin/bash
cd /home/user/; sudo -u user ./test_script.sh
cd ~

File 2 - "test_script.sh" - in "user" home directory:

#!/bin/bash
tmux new -d -s test
tmux send -t test './test_script_02.sh' ENTER

Explanation:

"File 1" will be executed as "root", that executes "File 2" as "user".

"File 2" will start a detached "tmux" session with name "test", then it will try to run a 3rd file in that session.

Usually that should work fine, I tried it locally on a Virtualbox and had no issues.

I started a VPS server and tried that but it isn't working as it should. There are no errors in root but two things happen.

I get an error in the created "tmux" session in "user":

/root/.tmux.conf: /root/.tmux.conf: Permission denied

And, the 3rd script "./test_script_02.sh" doesn't run. The "tmux" session just hangs there after being created, I cannot even write anything in the session unless I press "CTRL-C" When I press "CTRL-C", I get the same error below. It looks exactly the same.

When I try to open a "tmux" session from "user" itself by running the "tmux" command from it's shell, tmux session runs but I get an error in the top of the session and it looks like this inside the session:

-bash: /root/.bash_profile: Permission denied
user@server:/home/user$ 

Thanks in advanced.


EDIT 01:

OK, so apparently what I've done on the VPS is not exactly the same as on my local machine.

On the local machine, when you install the server, you create a user beside root that is added to the "sudo" group. While on VPS, they drop you on the "root" shell directly.

So I've been trying to run as "root" instead of any other user with elevated privileges.

What I've done now is listing the permissions on /dev/pts/

root@server:/dev/pts# ls -l
total 0
crw------- 1 root       tty  136, 0 Oct 31 16:12 0
crw--w---- 1 user       tty  136, 1 Oct 31 16:09 1
crw--w---- 1 user       tty  136, 2 Oct 31 14:19 2
crw--w---- 1 root       tty  136, 3 Oct 31 14:17 3
c--------- 1 root       root   5, 2 Oct 31 14:17 ptmx

Then created a new user "admin" and added is to the "sudo" group then listed the permissions again and found:

admin@server:~$ ls -l /dev/pts/*
crw------- 1 root       tty  136, 0 Oct 31 16:14 /dev/pts/0
crw--w---- 1 user       tty  136, 1 Oct 31 16:09 /dev/pts/1
crw--w---- 1 user       tty  136, 2 Oct 31 14:19 /dev/pts/2
crw--w---- 1 root       tty  136, 3 Oct 31 16:15 /dev/pts/3
crw--w---- 1 admin      tty  136, 4 Oct 31 16:15 /dev/pts/4
c--------- 1 root       root   5, 2 Oct 31 14:17 /dev/pts/ptmx

Now at this point I'm not sure if there is the real cause, as you can see "root" does not have write permissions to group while "admin" does.

Now if I run the command from user "admin" rather than "root" I'm able to get the my scripts running properly.

As for the 2nd error mentioned above when running "tmux" from "user", it seems to only occur after running the script from "root", that ends with permission denied mentioned in the first error above.

At this stage I'm not sure if I can call this solved, perhaps I shouldn't have used the "root" shell in the first place and created a new user and added it to the "sudo" group.

1

Run sudo with -i option: sudo -i -u user ./test_script.sh. That will set environment variables to user, too.

Your Answer

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

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