I know you can use su to switch users within the terminal. As the account I use is non-root (without superuser permissions), it brings up a password prompt for the user I log into.

Is there a way to use su or something similar to login using a one line command? For example,


Something like this could be used in a bash script for example.

  • 3
    Use expect, it is not a good idea to include passwords in text files. lifewire.com/linus-unix-command-expect-2201096 – Panther Sep 11 '17 at 17:29
  • @bodhi.zazen Ah, could you give me an example for this scenario? – Graviton Sep 11 '17 at 17:33
  • 1
    Well IMO use sudo sudo -u USERNAME COMMAND, it will use your password or you can configure sudo to run your command without a password. I can not give you a better example unless you post your script and describe what you are doing and why, probably a better way, but hard to tell. – Panther Sep 11 '17 at 17:57
  • @bodhi.zazen Unfortunately using a non-sudo account as well. The script doesn't run any commands once logged in. Just a method to log in without typing a password. – Graviton Sep 11 '17 at 18:02
  • 1
    The advantage of sudo is that you can configure it to allow access only those commands you wish. Again, if you need help, post additional details. – Panther Sep 11 '17 at 20:30

While I wouldn't recommend it (unless the use case explicitly calls for it), you can use something like the following in a shell script:

"echo PASSWORD | sudo -S COMMAND"

Where PASSWORD is the user password, and COMMAND is the command to run at the elevated permissions level.

Note that the -S option is important, as it redirects input from term to standard input. See Sudo Manual for additional details.

  • Is there a non-sudo alternative? – Graviton Sep 11 '17 at 18:11
  • From your follow-on question, I'm a bit more unclear on your use case. Perhaps you can provide a good use case for your issue. That said, to answer your question: not at a command level, though you could always alter user permissions held in /etc/pam.d/su. For details on that solution over on User & Linux SE. But again, I'd use caution until we have a more specific idea of what it is that you want to do. – richbl Sep 11 '17 at 22:23

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.