0

I have created a python application in my Ubuntu machine which run commands to terminal. I would like when the program start to continue running as root. I know that to get a root terminal we use the following command:

sudo -i

then the shell ask us to type our pass and when we do it we have root for that terminal season until we type

exit

But can I set the pass parameter though a argument ? So the terminal won't prompt a 'give a pass' line? Another question is when I use a live usb and I want to run sudo -i to get root privileges do I need to type a pass or not?

  • 1
    It's a bad idea to pass passwords with command-line arguments. Command-lines arguments can be seen from un-priviledges users eg. with the ps command and will be logged in your bash-history. Both are things you should avoid. – MadMike Jul 30 '15 at 9:19
  • @MadMike ok but can a window be prompt with sudo -i command? Cause I don't get any window when I run the command in my python program. And what happens when I use live Ubuntu? Can I use sudo -i wihout pass? – Nikos KLon Jul 30 '15 at 9:22
  • You will get a graphical password prompt when you use gksu instead of sudo. I'm not sure how to start a terminal session with root-privileges with gksu. – MadMike Jul 30 '15 at 9:26
  • @MadMike but gksu isn not installed and I need to use the python script in all ubuntu machines either they have or not gksu. If I have a live cd then can just use sudo -i ? As this question say (askubuntu.com/questions/284306/…) – Nikos KLon Jul 30 '15 at 9:37
1

Simply start your command with sudo rights:

sudo your_python_script

And NEVER use your password in a parameter.

  • ok when I use live usb and want to run my script the sudo command won't ask for password, will it? – Nikos KLon Jul 30 '15 at 14:01
  • Sorry, no idea. I've never tried this. – A.B. Jul 30 '15 at 14:05
0

Firstly I don't know whether the logic is correct with my option, but it certainly works.

First you create a rsa pub key using the command:

ssh-keygen -t rsa

Then copy and append the pub key to .ssh/authorized_keys of root by using the command:

ssh-copy-id root@<your ip>

After that when you try ssh root@<your ip>, it won't prompt for the root password and you will be able to get root prompt.

For example, if you want to run a shell script, run.sh in /opt directory as root, from your login without prompting password try this:

ssh -X root@<your ip> "sh /opt/run.sh"

'-X' option is used to set $DISPLAY for root.

Hope this idea helps.

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.