root - How do I tell if zsh is running with privileges?

06
2014-04
  • Demetri

    In my .zshrc, I have commands that launch instances of various program, like Mozilla Firefox and Evolution. I obviously do not want to launch them as root so I want to check whether I am root before I launch them. How do I do this?

  • Answers
  • mpy

    You can use e.g. such a conditional statement:

    PRGCMD="firefox"
    PRGUSR="user"
    if [[ $UID == 0 || $EUID == 0 ]]; then
       # root
       echo Running programm as User $PRGUSR
       sudo -u $PRGUSR ${(z)PRGCMD}
    else
       ${(z)PRGCMD}
    fi
    

    It checks first, if the real user ID or the effective user id is zero, that is root.

    If so, it uses sudo to run the program defined in the variable PRGCMD as the user definded in the variable PRGUSR. (You can omit this line if you want only the warning message.)

    ${(z)PRGCMD} splits words as if zsh command line in case PRGCMD contains spaces, i.e. parameters to the program.

    A short variant, which only runs the program as non-root is: [[ $UID != 0 || $EUID != 0 ]] && firefox


    Additionally you perhaps want to include %# into your prompt, so you can see that your current shell is privileged. From man zshmisc:

    %#     A `#' if the shell is running with privileges, a `%' if not.  Equivalent to `%(!.#.%%)'.  The definition of `privileged', for  these
           purposes,  is that either the effective user ID is zero, or, if POSIX.1e capabilities are supported, that at least one capability is
           raised in either the Effective or Inheritable capability vectors.
    

    The code judging if the shell is running with privileges (defined in privasserted() in utils.c) can IMHO not be done in shell code, so if you want the exact same behavior it's probably the best to parse the output of the %# prompt expansion:

    [[ $(print -P "%#") == '#' ]] && echo shell privileged || echo shell not privileged
    

  • Related Question

    linux - How can I install DropBox without root privileges?
  • Manuel

    Is it possible to install Dropbox without root privileges in Linux?

    I've tried to install it at my university, using the sources found here, but I seem to need to enter the root password (which of course, I don't know).


  • Related Answers
  • quack quixote

    You don't give your distribution, but since you aren't installing as root it's probably not important. (The binaries are meant to be installed system-wide, so unless you can convince your system administrator to install the package, you'll need to compile and install to your home directory.)

    Essentially, Dropbox's Linux client is two parts: a daemon, dropboxd, that provides a "per-user closed-source daemon process that makes sure your $HOME/Dropbox directory is properly synchronized", and a client that connects to the daemon and provides information. The binaries provided supply a Nautilus plugin for Gnome, although there are also CLI clients available.

    You'll want to refer to Installing to a Text-Based Linux Environment, which includes links to the daemon binaries for 32-bit and 64-bit systems. If you don't need the Nautilus plugin, make use of the official Dropbox CLI script or a user-created alternative CLI script.


    If you really want the Nautilus plugin, you'll need to build it from the source package, and install it to your home directory.

    Untar the source package:

    tar xjf nautilus-dropbox-0.6.1.tar.bz2 
    cd nautilus-dropbox-0.6.1
    

    Run the configure script; add the options you wnat, but you'll need the --prefix= option:

    ./configure --prefix=/home/<username>
    

    If this fails, you'll probably need to grab headers for the libraries needed to compile the program: GTK 2.12, GLib 2.13, Nautilus 2.16, and Libnotify 0.4.4 (minimum versions; if you can match up the version provided by your system, you can avoid the actual compilation and just grab header files to install to your own ~/include directory).

    Build and install the binaries; they should be installed to your home directory (eg ~/bin, ~/share, ~/lib, ~/man, etc).

    make
    make install
    

    Note this is the basic process, and you may need to work through them a few times depending on what libraries and headers are available on your system. All of these steps are possible without root access, but you will have to modify any installations to install them to your home directory instead of system locations like /lib or /usr.

    If you run into trouble, your best bet for finding help is the Dropbox forums.

  • juzna.cz

    On Ubuntu, just download the standard .deb package and using any archive tool find and extract dropbox file (which is a python script). Store it somewhere in your home directory, and run ~/dropbox start -i to install the daemon. That's it, working fine at my univeristy.

  • N. L.

    The "Installing to a Text-Based Linux Environment" isn't working as I write this answer.

    Simple command line install instructions can now be found on the Dropbox download page

    Next, run the Dropbox daemon from the newly created .dropbox-dist folder.

    $ ~/.dropbox-dist/dropboxd
    

    Here you are:

    1. sharing any user-owned directory.
    2. running this as your standard user.

    The steps are described on this blog post.