如何在 linux 或 unix 上找到用户的主目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/491877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:56:10  来源:igfitidea点击:

How to find a user's home directory on linux or unix?

javalinuxunixgrails

提问by

How do I find the home directory of an arbitrary user from within Grails? On Linux it's often /home/user. However, on some OS's, like OpenSolaris for example, the path is /export/home/user.

如何从 Grails 中找到任意用户的主目录?在 Linux 上,它通常是 /home/user。但是,在某些操作系统上,例如 OpenSolaris,路径是 /export/home/user。

采纳答案by Joachim Sauer

For UNIX-Like systems you might want to execute "echo ~username" using the shell (so use Runtime.exec()to run {"/bin/sh", "-c", "echo ~username"}).

对于类 UNIX 系统,您可能希望echo ~username使用 shell执行“ ”(因此用于Runtime.exec()运行{"/bin/sh", "-c", "echo ~username"})。

回答by Torsten Marek

You can use the environment variable $HOMEfor that.

您可以$HOME为此使用环境变量。

回答by Yuliy

To find the home directory for user FOO on a UNIX-ish system, use ~FOO. For the current user, use ~.

要在 UNIX-ish 系统上查找用户 FOO 的主目录,请使用~FOO. 对于当前用户,使用~.

回答by Alnitak

If you want to find a specific user's home directory, I don't believe you can do it directly.

如果要查找特定用户的主目录,我不相信您可以直接完成。

When I've needed to do this before from Java I had to write some JNI native code that wrapped the UNIX getpwXXX()family of calls.

当我之前需要从 Java 执行此操作时,我必须编写一些包含 UNIXgetpwXXX()调用系列的JNI 本机代码。

回答by Mark

Can you parse /etc/passwd?

你能解析 /etc/passwd 吗?

e.g.:

例如:

 cat /etc/passwd | awk -F: '{printf "User %s Home %s\n",  , }'

回答by Keltia

Find a Java wrapper for getpwuid/getpwnam(3)functions, they ask the system for user information by uid or by login name and you get back all info including the default home directory.

查找getpwuid/getpwnam(3)函数的 Java 包装器,它们通过 uid 或登录名向系统询问用户信息,然后您将获得包括默认主目录在内的所有信息。

回答by Jeff

The userdir prefix (e.g., '/home' or '/export/home') could be a configuration item. Then the app can append the arbitrary user name to that path.

userdir 前缀(例如,“/home”或“/export/home”)可以是一个配置项。然后应用程序可以将任意用户名附加到该路径。

Caveat: This doesn't intelligently interact with the OS, so you'd be out of luck if it were a Windows system with userdirs on different drives, or on Unix with a home dir layout like /home/f/foo, /home/b/bar.

警告:这不会与操作系统进行智能交互,因此如果它是在不同驱动器上具有 userdirs 的 Windows 系统,或者在具有 /home/f/foo、/home 等主目录布局的 Unix 上,您将不走运/b/酒吧。

回答by Daniel Hiller

Normally you use the statement

通常你使用语句

String userHome = System.getProperty( "user.home" );

to get the home directory of the user on any platform. See the method documentation for getPropertyto see what else you can get.

在任何平台上获取用户的主目录。请参阅getProperty的方法文档以了解您还可以获得什么。

There may be access problems you might want to avoid by using thisworkaround (Using a security policy file)

使用变通方法(使用安全策略文件)可能会出现您想要避免的访问问题

回答by paf0

For an arbitrary user, as the webserver:

对于任意用户,作为网络服务器:

private String getUserHome(String userName) throws IOException{
    return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(new String[]{"sh", "-c", "echo ~" + userName}).getInputStream())).readLine();
}

回答by Jared

I assume you want to find the home directory of a DIFFERENT user. Obviously getting the "user.home" property would be the easiest way to get the current user home directory.

我假设您想查找不同用户的主目录。显然,获取“user.home”属性是获取当前用户主目录的最简单方法。

To get an arbitrary user home directory, it takes a bit of finesse with the command line:

要获得任意用户主目录,需要在命令行上花点功夫:

String[] command = {"/bin/sh", "-c", "echo ~root"}; //substitute desired username
Process outsideProcess = rt.exec(command);
outsideProcess.waitFor();
String tempResult;
StringBuilder sb = new StringBuilder();
while((tempResult = br.readLine()) != null) sb.append(tempResult);
br.close();
return sb.toString().trim();

Now technically, we should have a thread waiting on the stdout and stderr so the buffers don't fill up and lock up the process, but I'd sure hope the buffer could at least hold a single username. Also, you might want to check the result to see if it starts with ~root (or whatever username you used) just to make sure the user does exist and it evaluated correctly.

现在从技术上讲,我们应该有一个线程等待 stdout 和 stderr,这样缓冲区就不会填满并锁定进程,但我肯定希望缓冲区至少可以保存一个用户名。此外,您可能想要检查结果以查看它是否以 ~root(或您使用的任何用户名)开头,以确保用户确实存在并且正确评估。

Hope that helps. Vote for this answer if it does as I'm new to contributing to SO and could use the points.

希望有帮助。如果我刚开始为 SO 做出贡献并且可以使用这些积分,那么请投票支持这个答案。