Linux run service as root

Run Bash script as root in startup on ubuntu 18.04

I wanted to run a bash script as root in startup. First I started using RC.Local and Crontab but nothing works.

4 Answers 4

Create the service file as in the template below and add the file in the location /etc/systemd/system/

[Unit] Description = ~Name of the service~ [Service] WorkingDirectory= ~directory of working file~ ExecStart= ~directory~/filename.sh [Install] WantedBy=multi-user.target 

Start the service file by the name using

systemctl start servicefile.service 
systemctl enable servicefile.service 
systemctl status servicefile.service 
systemctl stop servicefile.service 

Create a systemd unit file in /etc/systemd/system/ and use it to execute your script. (i.e. hello-world.service ).

[Unit] Description=Hello world After=sysinit.target StartLimitIntervalSec=0 [Service] Type=simple Restart=no RemainAfterExit=yes User=root ExecStart=/bin/echo hello world ExecStop=/bin/echo goodby world [Install] WantedBy=multi-user.target 

Now you can use it through systemctl as you would with other services.

$ systemctl enable hello-world $ systemctl start hello-world $ systemctl stop hello-world $ systemctl status hello-world ● hello-world.service - Hello world Loaded: loaded (/etc/systemd/system/hello-world.service; enabled; vendor preset: enabled) Active: inactive (dead) since Wed 2019-10-09 13:54:58 CEST; 1min 47s ago Process: 11864 ExecStop=/bin/echo goodby world (code=exited, status=0/SUCCESS) Main PID: 11842 (code=exited, status=0/SUCCESS) Oct 09 13:54:38 lnxclnt1705 systemd[1]: Started Hello world. Oct 09 13:54:38 lnxclnt1705 echo[11842]: hello world Oct 09 13:54:57 lnxclnt1705 systemd[1]: Stopping Hello world. Oct 09 13:54:57 lnxclnt1705 echo[11864]: goodby world Oct 09 13:54:58 lnxclnt1705 systemd[1]: Stopped Hello world. 

Make sure that you use the full path to your script in the unit file (i.e. /bin/echo). Check out the documentation about keys used in hello-world.service :

Читайте также:  Линукс скопировать весь диск

Источник

Running a service as root

I have a java program that I use to automate the process of creating VPN settings for clients. The program calls couple of bash scripts, create and copies files around. I have to run it under root user because the whole VPN config is under /etc/openvpn. For this directory I need root privileges. On the same machine I have Glassfish application server and it will call the mentioned Java program. Glassfish is run under non-root user. What is the best, most secure way of running a program as a root user of course without entering a password if I run it via sudo?

2 Answers 2

safest option would be to find out what group openvpn is owned by and add a non privileged user with access to it — the recommended way is to create an openvpn user, and give it privileges to use openvpn using sudo.

Adding the user you run this program under to the group (probably with just read acces) to give it access to openvpn, and giving it the necessary sudo privileges (to run openvpn — identical settings to what the openvpn user uses) should do the trick safely.

I’m already doing that. The problem is not in running OpenVPN itself. The Java program that I have calls scripts to create a clients certificate for example. The whole configuration is in a directory owned by root.root. Even OpenVPN runs as root at first and then changes the priviilges to a non priviliged user.

well, changing the ownership of the directory is my solution. Then give permissions to the user. That way you wouldn’t need to run this as root — you’d use a regular user and groups ‘properly’ rather than running something as root

Читайте также:  Load kernel module at boot linux

Источник

How to use user-systemctl as root?

I want to start a user service from my root shell without logging into the users shell. I have tried the following command, but it fails:

#> su - username -c "systemctl --user" Failed to connect to bus: No such file or directory 

4 Answers 4

The following worked for me:

runuser -u USER -- systemctl --user start USER.SERVICE 

The following command seems to work, but looks rather unpleasant.

su - username --shell=/bin/sh -c 'export XDG_RUNTIME_DIR=/run/user/$(id -u); systemctl --user' 
# systemctl start servicename@username 

for starting up my own global systemd-managed tmux session.

Putting all parts of the other answers together (and using the recommended runuser instead of su ) this works on current Debian/Ubuntu:

runuser username -c 'env XDG_RUNTIME_DIR=$(id -u) systemctl --user' 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Run systemctl —user commands as root

I need root to be able to manage systemctl —user units. Right now I have user1 set up with systemd user units. If the user logs in directly via terminal, GUI, or SSH, they are able to to run all systemctl —user commands. While the user is still logged in, I can run the following as root and perform all systemctl —user commands at that user with no problem:

su - user1 -c "systemctl --user status myunit.service" 

However if the user logs off, then no one can run systemctl —user commands as that user, not even root. I will continue to get

Failed to connect to bus: No such file or directory 

Even if I sudo — user1 as root, that is not good enough, will get the same error. The user literally needs to login to manage that user’s units. Apparently this is a known «issue» (quotes as the system is running as designed). Note: I tried setting the XDG_RUNTIME_DIR environment varialbe in user1 ‘s bashrc , but that does not help. Another user seems to have found a workaround, but it does not work. Looks like the developers did not approve of his idea. The only work around I found is literally ssh into the user account to run commands like this after auth with public keys:

ssh user1@localhost -f 'systemctl --user status myunit.service' 

I am looking for a workaround that does not require a SSH connection. I need root to be able to manage a systemd user unit while that user is not logged in. How can I accomplish this?

Читайте также:  Linux rdp server vnc

Источник

Оцените статью
Adblock
detector