Automating the "sudo su - user" command

38626
sam

I want to automate

sudo su - user 

from a script. It should then ask for a password.

5
Не используйте `sudo su - user`, используйте вместо этого` sudo -iu user`. (Кстати, проще управлять через `sudoers`.) grawity 13 лет назад 1
Как вы можете запустить `sudo su`, не имея возможности запустить` sudo visudo`? Hello71 13 лет назад 0

3 ответа на вопрос

7
Torian

I will try and guess what you asked.

If you want to use sudo su - user without a password, you should (if you have the privileges) do the following on you sudoers file:

<youuser> ALL = NOPASSWD: /bin/su - <otheruser> 

where:

  • <yourusername> is you username :D (saumun89, i.e.)
  • <otheruser> is the user you want to change to

Then put into the script:

sudo /bin/su - <otheruser> 

Doing just this, won't get subsequent commands get run by <otheruser>, it will spawn a new shell. If you want to run another command from within the script as this other user, you should use something like:

 sudo -u <otheruser> <command> 

And in sudoers file:

<yourusername> ALL = (<otheruser>) NOPASSWD: <command> 

Obviously, a more generic line like:

<yourusername> ALL = (ALL) NOPASSWD: ALL 

Will get things done, but would grant the permission to do anything as anyone.

когда команда sudo su - user выполняется, она запрашивает пароль. Я хочу решение, в котором скрипт AutomaticAaly откуда-то читает пароль. У меня нет разрешения делать то, что ты сказал ранее. sam 13 лет назад 0
У меня есть разрешение на сохранение пароля в файле. скрипт должен прочитать пароль из этого файла sam 13 лет назад 0
3
Olli

You can use command

 echo "your_password" | sudo -S [rest of your parameters for sudo] 

(Of course without [ and ])

Please note that you should protect your script from read access from unauthorized users. If you want to read password from separate file, you can use

 sudo -S [rest of your parameters for sudo] < /etc/sudo_password_file 

(Or whatever is the name of password file, containing password and single line break.)

From sudo man page:

 -S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character. 
Это на самом деле работает для меня. AlexandruC 9 лет назад 0
Это гениально Oscar Foley 8 лет назад 0
1
Mikel

The easiest way is to make it so that user doesn't have to type a password at all.

You can do that by running visudo, then changing the line that looks like:

someuser ALL=(ALL) ALL 

to

someuser ALL=(ALL) NOPASSWD: ALL 

However if it's just for one script, it would be more secure to restrict passwordless access to only that script, and remove the (ALL), so they can only run it as root, not any user, e.g.

Cmnd_Alias THESCRIPT = /usr/local/bin/scriptname someuser ALL=NOPASSWD: THESCRIPT 

Run man 5 sudoers to see all the details in the sudoers man page.

У меня нет разрешения на редактирование файла sudoers ... любого другого, чтобы он мог откуда-то прочитать пароль, чтобы это можно было автоматизировать. sam 13 лет назад 0
вам не повезло ... вы могли бы сделать это, скажем, "ожидать", но это позволило бы паролю вашего пользователя где-то жестко закодировать, где люди могли бы его увидеть (при условии, что вы правильно настроили разрешения, он все еще может быть читать по корню). Torian 13 лет назад 0
Попробуйте использовать `pect`. `Человек ожидает` для деталей. Mikel 13 лет назад 0
когда команда sudo su - user выполняется, она запрашивает пароль. Я хочу решение, в котором скрипт AutomaticAaly откуда-то читает пароль. У меня нет разрешения на редактирование файла sudoers. У меня есть разрешение на сохранение пароля в файле. Скрипт должен прочитать пароль из этого файла. sam 13 лет назад 0

Похожие вопросы