I am trying to create a script that opens a new tmux session and then runs a command inside that tmux session as a different user.

The purpose of this is to create a new tmux session for a game server, which has a different unix user assigned to it. I have heard that giving servers their own users was good practice, but I would still like to have a script in the home directory of my main user to start the server for convenience. I would like the tmux session to be available from my main user, yet be logged in to the game server user.

The main problem is that there is a password prompt that I cannot get around, requiring me to attach to the tmux session, enter the password, and then detach. This is what I have tried:


tmux new -d -s Minecraft sudo -u minecraft /home/minecraft/server/start.sh

I have tried every combination of running the script with sudo, adding su - Minecraft to the script, and su Minecraft -c "script", and in each one it either requires me to attach to the tmux session and login or just does not work, leaving no tmux session open.

I am trying to find a solution which would allow me to type in my sudo password or the other user's password when I run the script which invokes tmux, instead of having to login by attaching and then detaching.


Two approaches:

  1. Edit /etc/sudoers to allow your user to do sudo -u minecraft, or specifically sudo -u minecraft /home/minecraft/server/start.sh, without a password.
  2. Do sudo -u minecraft sleep 0 in your script before the tmux command.  This will ask you for your password.  Subsequent sudo commands for the next few minutes will not require a password.

Assuming that ssh is running on your machine, and listening on the localhost interface, you could set up an ssh key, and put the public part of that key in the target user's .ssh/authorized_keys file.

Then you can:

ssh target_user@localhost 'whatever command you want to run'

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.