Running an executable as a different user (setuid)

Some executables need to be executed as a user other than the current user. For example, the http server may be initiated during the boot sequence by root, but the task should be owned by the httpd user. The setuid permission enables the file to be executed as the file owner when any other user runs the program.

First, change the ownership to the user that needs to execute it and then log in as the user. Then, run the following commands:

    $ chmod +s executable_file
    # chown root.root executable_file
    # chmod +s executable_file
    $ ./executable_file

Now it executes as the root user regardless of who invokes it.

The setuid is only valid for Linux ELF binaries. You cannot set a shell script to run as another user. This is a security feature.