Announcement

Collapse
No announcement yet.

How do I run commands without using Sudo?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I run commands without using Sudo?

    Hi everyone, I'm a first time Kubuntu Forums poster, and a novice Kubuntu user (and I have to say that Microsoft need to watch out - I'm THIS close to saying goodbye to Windows permanently!)

    Anyway, I digress. I have a few shell commands that I would like to create desktop shortcuts for. I know I can just open a terminal and run them there, but I'd like to create desktop shortcuts purely for convenience.

    The commands are as follows:

    Shutdown/Reboot:

    sudo shutdown -h now
    sudo reboot


    Xampp (PHP/Apache Server):

    sudo /opt/lampp/lampp start
    sudo /opt/lampp/lampp stop


    Now, the problem is, as you can see, that these commands need SUDO in order to run. I'm not TOO concerned about this for the Xampp commands, because Xampp is something I'm only planning to start if I need to use it, so I'm happy to create a shell script that runs these commands (and then prompts for a password each time. But how can I run shutdown and reboot commands without needing sudo? Is this even possible?

    #2
    If you just want a desktop shortcut to shutdown/reboot, Id use the Lock/Logout widget. Right click on the desktop, and click Unlock Widgets. Right click again and click Add Widgets. Drag the one you want anywhere on the desktop.

    Comment


      #3
      Welcome to the better world of Kubuntu, I am not a very computer um.... geek, no I am a medical geek.

      With that said I am going to show you how I make my "short cuts" to those terminal commands. If you find any terminal commands that you want to run often at the click of an icon, you place those commands in a text file that ends with .sh (shell). Think of them like the old batch files in Windows DOS. Here are some examples I posted --> Simon's Bash

      To use my scripts make sure you have Kdialog installed.

      Code:
      #!/bin/bash
      kdialog --title "Shutdown" --warningyesno "Confirm shutdown of Xampp Server?"
      case $? in 
          0) RootID=`kdialog --title "Root Password" --password "To proceed root access is required:"`
             ;;
          *) exit
             ;;
      esac # Look case spelled backwards... who knew?
      
      #This is a comment line :D
      
      echo $RootID | sudo /opt/lampp/lampp start
      echo $RootID | sudo /opt/lampp/lampp stop
      Now that is saved anywhere you like on the system. Make sure it is executable in properties.



      You can right click and make a shortcut to application anywhere you like on the system. This is my desktop all my icons are in the lower right; Browser, Games, File Manager, Terminal, and Screen Resolution. I am a zen minimalist...

      Click image for larger version

Name:	screen4.png
Views:	1
Size:	632.5 KB
ID:	640670

      When I click Games it simply opens the games directory with more folder options. You can also change the icons for folders.

      Click image for larger version

Name:	desktop1.png
Views:	1
Size:	124.2 KB
ID:	640591

      Also read this; about desktop layout styles.

      Here is a better use of the code:
      Code:
      #!/bin/bash
      RootID=`kdialog --title "Xampp Server Root Password" --password "To proceed root access is required:"`
      
      kdialog --radiolist "Select Option:" 1 "Start Server" on  2 "Stop Server" off
      case $? in 
          1) echo $RootID | sudo -S /opt/lampp/lampp start
             ;;
          2) echo $RootID | sudo -S /opt/lampp/lampp stop
             ;;
          *) exit
             ;;
      esac
      I just realized the other way you would require 2 icons.
      Last edited by Simon; Feb 22, 2014, 07:39 PM. Reason: correction :D

      Comment


        #4
        Short answer yes you need to use sudo for those commands.

        Its a matter of user rights. Only the root user should be able to stop /start services and shutdown /reboot. In your Desktop enviroment you can shutdown/reboot because the service your desktop is running under (KDM / LIGHTDM / GDM / XDM / etc..) is running as root. When a shutdown/reboot request is issued from the desktop its then passed to the DM service. The DM service then runs the command removing the need for sudo because its already running as root.

        hope that helps you understand why a bit more.
        Last edited by sithlord48; Feb 22, 2014, 09:21 AM.
        Mark Your Solved Issues [SOLVED]
        (top of thread: thread tools)

        Comment


          #5
          Code:
          #!/bin/bashRootID=`kdialog --title "Xampp Server Root Password" --password "To proceed root access is required:"`
          
          
          kdialog --radiolist "Select Option:" 1 "Start Server" on  2 "Stop Server" off
          case $? in 
              1) echo $RootID | sudo /opt/lampp/lampp start
                 ;;
              2) echo $RootID | sudo /opt/lampp/lampp stop
                 ;;
              *) exit
                 ;;
          esac
          Does this work for anyone? I can't get the $RootID portion to work at all. The request for my password still shows up in the terminal here.


          EDIT: I think I got it to work with "sudo -S blahblah"
          Last edited by oshunluvr; Feb 22, 2014, 12:41 PM.

          Please Read Me

          Comment


            #6
            oshunluvr, Thanks for correcting me.

            @sithlord48: I don't think the OP was trying to bypass admin privileges and my bash script prompts for a password. I assume he simply wants to click an icon rather than drop to terminal and type a sting command as long as the alphabet.

            Comment


              #7
              The simplest option to run a script or command "as root" from a desktop icon is to simply use "kdesudo command/script" which will pop up password dialog and then run the command/script as root.

              If going for the script route for lampp, I'd probably write the script to test whether lampp is running and then start or stop using that condition (this would result in a simple "toggle on/off" desktop icon).

              Comment


                #8
                Thanks everyone for your input! I eventually decided to add the logoff widget to my panel, and I have two scripts for LAMPP that I run as needed (doesn't bother me as much that these ask for password because I don't always use LAMPP).

                Comment


                  #9
                  Originally posted by SamuelHolmes82 View Post
                  Thanks everyone for your input! I eventually decided to add the logoff widget to my panel, and I have two scripts for LAMPP that I run as needed (doesn't bother me as much that these ask for password because I don't always use LAMPP).
                  i did the same thing as you for LAMP. however, after some updates i noticed that LAMP just started and ran by default despite me turning it off and before i told the OS not to run it at start up. when shuttign down make sure LAMP is off.

                  Furthermore i found out that turnkey linux+virtualbox is very good way to test stuff on local server. it takes about 256MB ram to run server is easy to setup and it runs reasonably fast. plus they provide readily made virtualbox images, so no need to install in virtualbox etc. based on Debian stable. just some food for thought...

                  Comment

                  Working...
                  X