当前位置: 首页 > news >正文

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

需求

开机自动运行程序(或脚本),需要以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

http://www.lryc.cn/news/31471.html

相关文章:

  • 【阅读笔记】你不知道的Javascript--类与类型委托3
  • 文件服务设计
  • 【批处理脚本】-1.22-字符串界定符号 ““
  • 【Flutter·学习实践·UI篇】基础且重要的UI知识
  • 【OpenCV】车牌自动识别算法的设计与实现
  • SpringBoot发送邮件
  • BigInteger类和BigDecimal类的简单介绍
  • mysql五种索引类型---实操版本
  • 【微信小程序】-- 页面导航 -- 编程式导航(二十三)
  • 路由追踪工具 traceroute 使用技巧
  • NGINX学习笔记 - 一篇了解NGINX的基本概念(一)
  • Spring-Cloud-Gateway的过滤器的执行顺序问题
  • Android性能优化的底层逻辑
  • Gradle+SpringBoot多模块开发
  • Qt 之 emit、signals、slot的使用
  • 每日学术速递3.6
  • C# 将对象转换成字节数组(二进制数据)
  • 巾帼绽芬芳 一起向未来(下篇)
  • 代码还原小试牛刀(一):魔改的MD5
  • 6. 找大佬
  • 【CSS】标签显示模式 ① ( 标签显示模式 | 块级元素 )
  • hive真实表空间大小统计
  • 微信小程序引入Vant UI步骤
  • 【震撼发布】《致敬未来的攻城狮计划》| 文末赠书3本
  • 8.装饰者模式
  • GIT基础常用命令-1 GIT基础篇
  • 华为OD机试题,用 Java 解【数列描述】问题
  • 2022掉队的“蔚小理”,按下了兔年加速键
  • 【NLP相关】attention的代码实现
  • 凌恩生物资讯