Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Units from non root user (for commands start/stop/enable/disable)

I want to execute my scripts at boot time through systemd (OS is Fedora Core 16) from some non-root user (e.g. michael).

I don't need root privileges - I only want to run at boot time and at shutdown time some of my units in systemd. And I don't want to turn on a root user for this. I only want to use power of systemd syntax of units for starting and watching for my process (of course without root access). Is it possible?

I didn't find any help in the documentation. I only found in systemd(1) -> DIRECTORIES -> "User unit directories":

$ pkg-config systemd --variable=systemduserunitdir
/usr/lib/systemd/user

But the /usr/lib/systemd/user is:

drwxr-xr-x. 2 root root

I cannot imagine how I can use this directory as a non-root user...

like image 461
Perlover Avatar asked Apr 18 '12 09:04

Perlover


People also ask

Should we allow non-root user to run change root yes or no?

It always a Better to run the system as non-root user. The account created during installation is an administrator account. In Ubuntu, root account has no password and as a consequence, you can not login as the root user. There are ways to circumvent this, but this is the default setting.

How do I start a service as a non-root user in Linux?

Run Systemd Service as standard Logged in userCreate a systemd service unit file under the directory. Reload systemd. Confirm the service is available. $ systemctl --user list-unit-files syncthing.

Can you run a command on a Linux server if you are not the root user?

When it comes to working with Linux, there are two ways to run administrative applications. You can either change over to the super user, also known as the root account, using the “su” command, or you can use the “sudo” command.


2 Answers

This Arch Linux document may help.

systemd offers users the ability to run an instance of systemd to manage their session and services. This allows users to start, stop, enable, and disable units found within certain directories when systemd is run by the user. This is convenient for daemons and other services that are commonly run as a user other than root or a special user, such as mpd.

They suggest starting a system --user instance via xinit, but I suppose you want to start the user services regardless of whether the user is logged in.

I would normally do that by adding an @reboot line to my crontab.

From Wikipedia:

@reboot can be useful if there is a need to start up a server or daemon under a particular user, and the user does not have access to configure init to start the program.

Maybe you can start systemd --user via @reboot!!!

like image 131
richardw Avatar answered Sep 22 '22 13:09

richardw


Create a user systemd unit file for 'michael'.

Here's the magic to ensure users systemd process will kick off at boot instead of at login:

loginctl enable-linger michael

Full details

like image 23
Guy Gangemi Avatar answered Sep 26 '22 13:09

Guy Gangemi