How do I run a sudo command needing password input in the background?

user364819

I have recently disabled sudo's authentication caching ability so that it now prompts me for a password every time.

And though this is good for security, it has caused one slight problem which I haven't been able to figure out a solution for, I am unable to run commands which go along the lines of:

sudo <command> &

In the past I would have run a sudo command before that, it would have cached my password and allowed me to run sudo commands without prompt for the next few minutes, and thus it would allow me to run the command.
But when I run it now as there is no caching before hand and as it starts a new thread immediately and sudo doesn't even prompt me for a password, I am unable to run it in this way.

So unless I run sudo -i before it I am unable to run a command in this format which is becoming rather annoying.
So I was wondering if there is any way to get around this and still run programs and commands in this way?

I am running Ubuntu GNOME 15.10 with GNOME 3.18, and specifically the program I want to run in this fashion is etherape if that makes any difference, but I would really like the solution to work for all programs and commands.

muru

Instead of running sudo in the background, tell sudo to run the command in the background. From man sudo:

-b, --background
     Run the given command in the background.  Note that it is not
     possible to use shell job control to manipulate background
     processes started by sudo.  Most interactive commands will
     fail to work properly in background mode.

For example:

sudo -b sleep 10

Another way would be to just use a shell to run the command:

sudo sh -c 'sleep 10 &'

Another option would to specify a graphical program for obtaining the password, and send sudo to the background anyway:

-A, --askpass
     Normally, if sudo requires a password, it will read it from
     the user's terminal.  If the -A (askpass) option is
     specified, a (possibly graphical) helper program is executed
     to read the user's password and output the password to the
     standard output.  If the SUDO_ASKPASS environment variable is
     set, it specifies the path to the helper program.  Otherwise,
     if sudo.conf(5) contains a line specifying the askpass
     program, that value will be used.  For example:

         # Path to askpass helper program
         Path askpass /usr/X11R6/bin/ssh-askpass

     If no askpass program is available, sudo will exit with an
     error.

Askpass programs are typically used for SSH. One such is provided by the ssh-askpass-gnome package, which is installed by default, at least on Ubuntu 15.10.

SUDO_ASKPASS=/usr/bin/ssh-askpass sudo -A sleep 10 &

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to run a command in background always?

분류에서Dev

How to find the full path of sudo to run a command

분류에서Dev

How do I run an arbitrary shell command from Deno?

분류에서Dev

In zsh how do I bind a keyboard shortcut to run the last command?

분류에서Dev

How do I run a gui app from the command line?

분류에서Dev

How do I dynamically focus on input element at run time

분류에서Dev

How can I make running a command depend on the input of a correct password (Tkinter GUI)?

분류에서Dev

Cannot run any sudo command

분류에서Dev

How "sudo" command works?

분류에서Dev

How to run a program in background and also using && to execute another command

분류에서Dev

How do I run a command line program as Administrator under Windows 7?

분류에서Dev

how do I save a ftp password but get the plain password later

분류에서Dev

How do I allow SFTP with a password BUT not SSH?

분류에서Dev

How do I tell sudo to write files with a umask of 0022?

분류에서Dev

How can I play a song in the background via my command line?

분류에서Dev

How to background a command chain?

분류에서Dev

how to use the background command?

분류에서Dev

Any security risks with sudo password from standard input over ssh?

분류에서Dev

How do I run a pitest ant script

분류에서Dev

How do I run bootchart on Fedora 23

분류에서Dev

How do I run fsck manually?

분류에서Dev

How do I throttle a command in a terminal window?

분류에서Dev

How do I look at the source code for a command?

분류에서Dev

How do I set the at command shell to bash?

분류에서Dev

How do I input Greek characters in Ubuntu?

분류에서Dev

How do I turn off all the password prompts?

분류에서Dev

How do I prevent PolicyKit from asking for a password?

분류에서Dev

How do I password protect a .tgz file with tar in Unix?

분류에서Dev

How do I reset the password on a Dedicated Micros DVR?

Related 관련 기사

  1. 1

    How to run a command in background always?

  2. 2

    How to find the full path of sudo to run a command

  3. 3

    How do I run an arbitrary shell command from Deno?

  4. 4

    In zsh how do I bind a keyboard shortcut to run the last command?

  5. 5

    How do I run a gui app from the command line?

  6. 6

    How do I dynamically focus on input element at run time

  7. 7

    How can I make running a command depend on the input of a correct password (Tkinter GUI)?

  8. 8

    Cannot run any sudo command

  9. 9

    How "sudo" command works?

  10. 10

    How to run a program in background and also using && to execute another command

  11. 11

    How do I run a command line program as Administrator under Windows 7?

  12. 12

    how do I save a ftp password but get the plain password later

  13. 13

    How do I allow SFTP with a password BUT not SSH?

  14. 14

    How do I tell sudo to write files with a umask of 0022?

  15. 15

    How can I play a song in the background via my command line?

  16. 16

    How to background a command chain?

  17. 17

    how to use the background command?

  18. 18

    Any security risks with sudo password from standard input over ssh?

  19. 19

    How do I run a pitest ant script

  20. 20

    How do I run bootchart on Fedora 23

  21. 21

    How do I run fsck manually?

  22. 22

    How do I throttle a command in a terminal window?

  23. 23

    How do I look at the source code for a command?

  24. 24

    How do I set the at command shell to bash?

  25. 25

    How do I input Greek characters in Ubuntu?

  26. 26

    How do I turn off all the password prompts?

  27. 27

    How do I prevent PolicyKit from asking for a password?

  28. 28

    How do I password protect a .tgz file with tar in Unix?

  29. 29

    How do I reset the password on a Dedicated Micros DVR?

뜨겁다태그

보관