0

I what to run jar as service in linux, All the guides showed that you need to create a file.service in the /etc/systemd/system folder, I want to write a script with a command that will create the file But every command I tried requires using SUDO and entering a password, I want to bypass entering the password, and for everything to be automatic in the script Can anyone help?

I tried to create the file in another folder and copy with cp command but still I have to insert password. I tried the command echo "XXX" > "/etc/stsyemd/system/filename.service" but permission denied.

2 Answers2

0

You can't generally "bypass entering the password" for sudo in a script (you can of course edit the configuration of sudo, but that, as you might expect, requires you to use sudo first). That's the point of having any separation of users.

So, what you want is impossible. If one could just write a script that can become root without authentication, there would be no point in having users that cannot do all the destructive things that root can do.

and for everything to be automatic in the script

Such installation scripts can fairly assume that they are run by root, so your script itself doesn't have to deal with that and would instead be run with sudo (as a whole, not internally).

Other than that, "I want to run a jar" might suggest you shouldn't be trying to install a service in /etc/systemd/system at all – but a user service for your current user only, into ~/.config/systemd/user, for which you don't need any special privileges at all – and thus no access to root privileges, and no sudo.

  • When I use this directory: ~/.config/systemd/user and start the file.service - after reboot the service stopping. I want the service will start after reboot. what can I do? – user14490920 Aug 20 '23 at 13:21
  • https://unix.stackexchange.com/questions/251211/why-doesnt-my-systemd-user-unit-start-at-boot – Marcus Müller Aug 20 '23 at 14:39
0

Bypassing the password in sudo is simple.

  1. Add the line
youruser_ID ALL=(ALL) NOPASSWD:ALL

to the sudoers-file (using visudo). Never again password typing!

Or 2) Add the line

Defaults timestamp_timeout=60

below the Defaults env_reset in the sudoers file. Now, you can do sudo id, and it will ask you for a passworde, but the rest of the hour, you can do sudo without password.

But in general, scripts that install files in the /etc/systemd/system should be run as root. So

sudo install_script

would be a better option.

Ljm Dullaart
  • 4,643