简体   繁体   English

如何以root身份启动Java程序,但降级为非root用户

[英]How to start Java program as root but downgrade to non-root user

I am writing a Java program which will be running on Linux servers. 我正在编写一个将在Linux服务器上运行的Java程序。 The program needs to start as "root", do a certain operation (reading a file), and then downgrade itself to a non-root user, after the file has been read and closed. 程序需要以“root”身份启动,执行某项操作(读取文件),然后在读取并关闭文件后将其自身降级为非root用户。

I read through the following questions, but I haven't found a viable solution - especially since I am not dealing with http ports. 我阅读了以下问题,但我没有找到可行的解决方案 - 特别是因为我没有处理http端口。

https://askubuntu.com/questions/62713/how-to-downgrade-user-permissions-on-a-java-app-after-opening-port-80-443 https://askubuntu.com/questions/62713/how-to-downgrade-user-permissions-on-a-java-app-after-opening-port-80-443

https://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user https://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user

Running a part of a Java Program as Root 以Root身份运行Java程序的一部分

Any ideas on how to implement what I am trying to do? 关于如何实现我想要做的事情的任何想法?

SOLVED: As suggested in the answer, JNA did the trick for me. 解决: 正如答案所示,JNA为我做了诀窍。

Unfortunately there is no way to do this in pure Java. 不幸的是,在纯Java中没有办法做到这一点。
But you can use JNI or JNA to call setuid system function. 但是您可以使用JNI或JNA来调用setuid系统函数。
For instance, you may reuse jetty-setuid package for doing so: JAR + Native library . 例如,您可以重复使用jetty-setuid包: JAR + Native库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java程序如何以非root用户身份以编程方式在端口80上启动Jetty - Using Java Program How to programmatically start Jetty on port 80 as non-root user 如何在CentOs 6上以非root用户身份运行Java服务 - How to run java service as a non-root user on CentOs 6 如何以非root用户身份运行Kafka? - How to run Kafka as non-root user? 以非root用户身份使用Solaris SMF运行Java应用程序 - Running Java Application with Solaris SMF as Non-Root User 可以使用Chef以非root用户身份安装Java吗? - Can Java be installed as non-root user with Chef? Java 应用程序无法以 debian 中的非 root 用户身份连接到 mysql - Java application fails to connect to mysql as non-root user in debian 如何在 Debian 上以非 root 用户身份运行 SpringBoot 应用程序? - How to run SpringBoot app as non-root user on Debian? Linux中的Java - root和non-root的不同外观类 - Java in Linux - different look and feel classes for root and non-root 作为非root用户的Java进程消耗100%的CPU,但是root用户很好 - Java process as non-root consumes 100% CPU, but as root is fine 如何为非 root 用户配置 Dockerfile 但授予他们访问权限以写入根目录中的文件? - How to configure Dockerfile for non-root user but give them access privileges to write to a file in the root dir?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM