I need two suggestions for making my script working from local server as well as ssh execution

1. How to update the sudoers

In server i have a path


all files of parent and child_dir are owned by tomcat user and group.

-rw-rw-r-- 1 tomcat tomcat 0 Feb 17 02:23 20221119.status
-rw-rw-r-- 1 tomcat tomcat 0 Feb 17 02:23 20221116.status
-rw-rw-r-- 1 tomcat tomcat 0 Feb 17 02:23 20221112.status

I want user named new_user to able to rename the files under the /var/log/data/status/ as part of my script. I have tried to edit my sudoers file like below but it to run without password because tomcat is application user does not have password, Unsure how to make sudoers changes for it.

 new_user  ALL=NOPASSWD: /bin/bash /path/my_script [a-z_]* 20[0-9]*

what i want to achieve is, to run without any password

sudo -u new_user /bin/bash /path/my_script

2. Run the script over ssh

My another requirement is to run the same script from other server over ssh, if possible, please help with the syntax

  • On server A (Login as my user iamsage) -

i need to run something like this

ssh new_user@remote_host "sudo -u new_user /path/my_script arg1 arg2"
  • Why do you want to run sudo su - new_user /bin/bash /path/my_script instead of sudo -u new_user /path/my_script arg1 arg2? Please edit your question to add requested information.
    – Bodo
    Commented Feb 17, 2023 at 11:04
  • yep, typo thanks, updated it.
    – iamsage
    Commented Feb 17, 2023 at 11:07
  • Please clean up your question, it is a bit confusing. Your title mentions sudo su, your question body talks about sudo -u -, and I guess what you actually want is sudo -u (with no extra -). Also, please clarify what you need to run: who runs the script? How? What command? Can new_user ssh into the machine? Can't you just do ssh new_user@ip /path/my_script arg1 arg2?
    – terdon
    Commented Feb 17, 2023 at 11:20
  • {{/var/log/data/status/<some_files}} is possible as a path, but it's quite unusual, and a path like that is unlikely to help when you're struggling with something else. Commented Feb 17, 2023 at 11:25
  • What's the - in sudo -u - new_user ...? Did you run the command? Copy&paste the command you run on your system and the resulting output or error message. Your ssh command lacks the host name. It does not make sense to use sudo -u new_user when your process is already running as new_user by ssh new_user@host ... There might be more misunderstanding in the question. Please explain what users are involved. sudo su - new_user ... or sudo -u new_user ... allow some other user to run a command as new_user. You might need new_user to run a command as user or group tomcat.
    – Bodo
    Commented Feb 17, 2023 at 11:31

1 Answer 1


Your sudoers line isn't quite right. Try this instead

your_user ALL=(new_user) NOPASSWD: /path/my_script

Then ensure your script is executable and has #!/bin/bash as its first line.

The big issue with the remote invocation suggestion is you're trying to log in as the new user, so you wouldn't need to use sudo. Try either of these but don't mix them:

ssh remote_host sudo -u new_user /path/my_script arg1 arg2
ssh new_user@remote_host /path/my_script arg1 arg2

In both cases it's quite permissible to quote the entire command intended for remote execution but I've not done that here


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .