如何从 Eclipse 以超级用户身份运行我的应用程序?
当我尝试从 Eclipse 运行服务器应用程序时,遇到错误。错误为java.net.BindException:权限被拒绝。我认为这是因为我使用端口 443 来设置 SSL 连接。如果我使用 java 和 sudo 在命令行上运行代码,我可以解决这个问题。有没有办法设置 Eclipse,以便当我点击运行按钮时,我的应用程序会使用 sudo 执行?
I'm running in to an error when I try to run my server application from Eclipse. The error is java.net.BindException: Permission denied. I think this is because I am using port 443 to set up an SSL connection. I can get around this problem if I run my code on the command line using java and sudo. Is there a way to set up Eclipse so that when I hit the run button, my application is executed with sudo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以按照以下步骤以超级用户身份编译/调试应用程序。
重命名您的 java 应用程序
sudo mv /usr/lib/jvm/java-6-openjdk/jre/bin/java /usr/lib/jvm/java-6-openjdk/jre/bin/java.ori
创建以下脚本并存储它为 /usr/lib/jvm/java-6-openjdk/jre/bin/java
<块引用>
<前><代码>#!/bin/bash
# 文件:/usr/lib/jvm/java-6-openjdk/jre/bin/java
# 描述:jdk 的启动器。当以下情况时以 root 身份运行 jdk
# cmd-line-arg“--run-as-root”已指定。
#
jre =“/usr/lib/jvm/java-6-openjdk/jre/bin/java.ori”
run_as_root=假
参数=
# 过滤命令行参数
对于“$@”中的arg
做
案例“$arg”
--run-as-root) run_as_root=true
;;
*)args =“$args$arg”
;;
埃萨克
完毕
# 删除前导空格
args=$(echo $args | sed -e 's/^[ \t]*//')
如果 $run_as_root
然后
echo“警告:以 root 身份运行!”
gksu“$jre $args”
别的
$jre $args
菲
更改权限以使其可执行
sudo chmod 0755 /usr/lib/jvm/java-6-openjdk/jre/bin/java
启动 eclipse
要以 root 身份运行项目,您需要执行以下步骤:
注意:这个想法来自 http://www.eclipse.org/forums/index.php/mv/msg/87353/724852/#msg_724852
You can follow these steps to compile/debug applications as superuser.
Rename your java-application
sudo mv /usr/lib/jvm/java-6-openjdk/jre/bin/java /usr/lib/jvm/java-6-openjdk/jre/bin/java.ori
Create following script and store it as /usr/lib/jvm/java-6-openjdk/jre/bin/java
Change the permissions to make it executable
sudo chmod 0755 /usr/lib/jvm/java-6-openjdk/jre/bin/java
Startup eclipse
To run projects as root you need to follow these steps:
Note: The idea is from http://www.eclipse.org/forums/index.php/mv/msg/87353/724852/#msg_724852
假设您使用的是 Linux (*nix),
通过 sudo 命令启动 Eclipse 会话怎么样?
比如
现在无论你从 eclipse 做什么都会有 sudo 上下文?
Assuming you are on Linux (*nix),
How about starting your eclipse session via a sudo command?
Such as
Now whatever you do from eclipse will have the sudo context?
正如此线程中提到的:
正如 HTTP 服务中提到的:
org.osgi.service .http.port
- 指定用于 http 服务的端口号。根据 OSGi 规范,此属性的默认值为 80(需要 root 权限)。org.osgi.service.http.port.secure
- 指定用于安全 http 服务的端口号。根据 OSGi 规范,此属性的默认值为 443(需要 root 权限)。也许如果您尝试将最后一个属性修改为高于 1024 的值,它可以在不需要任何特殊权限的情况下工作。
As mentioned in this thread:
As mentioned in HTTP Service:
org.osgi.service.http.port
- specifies the port number to use for the http serving. The default value for this property is 80 (which requires root permission), as per the OSGi specification.org.osgi.service.http.port.secure
- specifies the port number to use for secure http serving. The default value for this property is 443 (which requires root permission), as per the OSGi specification.Maybe if you try to modify that last property to a value above 1024 it could work without requiring any special privilege.
另一种选择是使用 iptables 或 ipfilter 将端口 80 转发到端口 1024 以上。
(有人可以提供一个实用且易于理解的解释的链接吗?)
Another option would be to use iptables or ipfilter to forward port 80 to a port above 1024.
(Can someone contribute a link to a practical and easy-to-understand explanation ?)
如果这满足您的需求并且可能的话,更好的答案可能是路由器上的简单端口重定向。
当您现在只是开发(而不是安装)并且想要在调试器中运行它时,不要试图强制您的 linux/unix 打开保留端口,
将路由器设置为将传入(外部)端口 443 重定向到更适合您当前需求的端口(例如 4443)。
我认为大多数路由器都支持此功能,如果您的路由器不支持此功能,它可以为您的妈妈提供一个不错的圣诞节或生日礼物创意!
A better answer, perhaps, if this serves your needs AND is possible, could be simple port redirection on your router.
Instead of trying to force your linux/unix to open a reserved port, when you are only developing this now (not installing) and you want to run it in a debugger,
set your router to redirect incoming (external) port 443 to a port that is more convenient for your current needs (say 4443).
I think most routers support this, and if yours doesn't it gives your mum a good christmas or birthday present idea!
我正在编写 C 而不是 Java,但这在任何一种情况下都应该有效。
我使用远程调试 - 定义到 LOCALHOST 的“远程”连接,它允许您指定要连接的用户,指定 ROOT。然后在调试配置连接中定义一个远程应用程序:LOCALHOST。请务必选中主选项卡底部以及连接属性窗口下的“跳过下载到目标路径”。
I am writing C not Java but this should work in either case.
I use remote debug - define a "remote" connection to LOCALHOST which allows you to specify the user you will connect with, specify ROOT. Then define a Remote Application in debug configuration connection: LOCALHOST. Be sure to check "skip download to target path" at the bottom of the main tab as well as under the connection properties window.
为此,您可以使用远程Java应用程序机制。
运行 -> 部分调试配置...
(对于您来说,它将是 localhost 和 443)。
You can use Remote Java Application mechanism for this.
section in Run -> Debug configurations...
(for you it will be localhost and 443).
如果您使用外部工具(运行菜单/外部工具或工具栏上运行/调试图标旁边的图标),您可以使用任何脚本或您喜欢的任何内容。这些脚本可能会给您更高的权限,或者其他什么。
另一方面,通过这种方式调试应用程序可能会变得非常困难,因为“运行”命令和“调试”命令都不与此外部工具配置关联。也许可以连接应用程序的 Eclipse 调试器,但我不知道这是怎么可能的。
If you use External tools (Run menu/External tools or an icon next to the Run/Debug icons on the toolbar), you can use any scripts or whatever you like. The scripts may give you elevated rights, or whatever.
On the other hand, this way debugging the application can become very hard, as neither the Run nor Debug commands get associated with this External tool configuration. Maybe it is possible to connect the Eclipse debugger of the application, but I don't know, how that is possible.
您可以通过这种方式
所以你将有一个透明的调试(没有 sudo 包装器 - gdb 可以)。
缺点:这是一个本地安全漏洞。
解决方案:
将其放入/opt/my-stupid-eclipse
chmod +x 此脚本并将其列入 sudo 配置白名单。
将其添加到您的 makefile 中,指定服务器二进制文件的路径。
现在你有了非常奇怪但安全的脚本,其他用户无法更改......并且用任何恶意代码替换服务器二进制文件仍然存在一点漏洞,这将获得上限,因此没有文件名检查/严格会有所帮助......可以 $1被 bash 命令污染了,不是吗?你猜,没有。
You may go this way
So you will have a transparent debugging (no sudo wrapper - gdb ok).
Cons: it is a local security breach.
Solution:
put this to /opt/my-stupid-eclipse
chmod +x this script and whitelist it on sudo config.
Add it to your makefile, specify path to your Server binary.
Now you have pretty strange but secure script, that cannot be changed by other users... and still a little breach for replacing Server binary with any malicious code, that will gain caps, so no filename check/stricts will help.. can $1 be contaminated with bash commands, no? Guess, no.