ubuntu添加系统服务实现开机root权限运行

在Ubuntu20.04系统中,为实现程序/home/xxx/test开机自动启动并以root权限运行,但不需输入密码,文章提供了创建systemd服务的方法。具体步骤包括创建service配置文件,定义服务启动命令,然后通过systemctl进行安装、重载配置、启用服务。重启后,通过systemctl检查服务运行状态。
摘要由CSDN通过智能技术生成

需求

开机自动运行程序(或脚本),需要以root权限运行但不输入密码,也不能将密码写入文件。

环境

Ubuntu 20.04

解决方案

添加系统服务,然后通过systemctl控制。

操作步骤

假设目标程序为/home/xxx/test

1、创建service配置文件

[Unit]
Description=test
DefaultDependencies=no
#Before=cloud-init-local.service
After=vgauth.service
After=apparmor.service
#RequiresMountsFor=/tmp
After=systemd-remount-fs.service systemd-tmpfiles-setup.service systemd-modules-load.service

[Service]
ExecStart=/home/xxx/test
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target
Alias=test

保存为test.service。

2、执行下面的命令安装

sudo cp ./test.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable test

3、重启,查看服务是否运行

sudo systemctl list-units |grep test
sudo systemctl list-unit-files |grep test

参考文献

https://www.cnblogs.com/ggzhangxiaochao/p/15039617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值