1

i will create file sh (bash script)

#!/bin/bash
su - username -c XXXXXXXX

xxxxxx= command

where i put my password?( but NOT ask me password in terminal..how?)

2
  • 1
    Putting your root password in a script is a very bad idea. sudo su might be a better solution. (And sudo can be configured to work passwordless for certain commands).
    – Hennes
    Jun 22, 2015 at 16:28
  • it's not dangerous because it's for me my home, not in remote or in server.. Jun 22, 2015 at 16:36

1 Answer 1

1

You should look into properly setting up your sudoers file. You would do this with a program called visudo, which does some behind-the-scenes magic.

sudoers can be set up in a way such that any user can be given access to a limited set or all commands available on the system. You can also set it up so that a user can run any command as another user. I think this third method is the way you'll decide to go.

However, If you've decided this is something you truly want to accomplish, I would recommend determining what commands you want to run, and use the whitelist approach, vs unlocking all commands.

If you're running Chromium, or Firefox, or any server/client exposed to the Internet as a user in the sudoers file with unfettered, password-less access, you might as well be running them under root.

  1. visudo
  2. Add the following entry
    • user1 ALL=(user2) NOPASSWD: /bin/bash

user1@host $ sudo -u user2 cat /home/user2/.ssh/authorized_keys

Resources:

  1. allow sudo to another user without password
  2. visudo man page
  3. Ubuntu's Sudoers Documentation
2
  • sorry i dont understand, i try to understand: #!/bin/bash visudo username ALL=(user2) NOPASSWD: /bin/bash user1@host $ sudo -u user2 echo "banana" this is right or i'm wrong? Jun 22, 2015 at 16:51
  • Don't blindly do this. Read, read read. If you do this wrong, you can cause severe issues or introduce vulnerabilities. Read about visudo. Read about sudoers.
    – earthmeLon
    Jun 22, 2015 at 16:54

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