Java current machine name and logged in user?
Asked Answered
N

4

176

Is it possible to get the name of the currently logged in user (Windows/Unix) and the hostname of the machine?

I assume it's just a property of some static environment class.

I've found this for the user name

com.sun.security.auth.module.NTSystem NTSystem = new
        com.sun.security.auth.module.NTSystem();
System.out.println(NTSystem.getName());

and this for the machine name:

import java.net.InetAddress;
...
String computerName;
...
try {
    computerName = InetAddress.getLocalHost().getHostName();
}

catch(Exception ex) {
    ...
}

Is the first one just for Windows?

And what will the second one do, if you don't have a hostname set?

Newman answered 23/1, 2009 at 16:2 Comment(1)
The NTSystem class only exists on Windows JDK distributionsParlormaid
L
286

To get the currently logged in user:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());
Litt answered 23/1, 2009 at 16:8 Comment(4)
Does System.getProperty("user.name"); work on both windows and linux?Guardsman
However, the user.name value can be spoofed, so it should not be used upon for authentication.Holding
System.getProperty("user.name") is NOT currently logged in user, it's user under who's security context is JVM running.Poona
As for what OP refers to as "machine name" or "computer name" this answer is incorrect. Java has no way to obtain the "computer name", i.e. the name assigned to the computer early on in the boot process and unrelated to network. All OS'es have this concept, but unfortunately this value is not exposed in Java. However often - but not always - the above method will indeed return the computer name. See https://mcmap.net/q/92672/-recommended-way-to-get-hostname-in-java for explanation.Counterweight
B
106

To get the currently logged in user:

System.getProperty("user.name");

To get the host name of the machine:

InetAddress.getLocalHost().getHostName();

To answer the last part of your question, the Java API says that getHostName() will return

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.

Blavatsky answered 23/1, 2009 at 16:28 Comment(2)
Upvote for answering all of the OPs original questions bar one clearly and concisely. Surely this should be the accepted answerFlameproof
Agree with @Flameproof - up-vote for clear and concise answer to all points.Ligroin
S
11

Using user.name is not secure as environment variables can be faked. Method you were using is good, there are similar methods for unix based OS as well

Stationary answered 24/9, 2012 at 12:58 Comment(4)
So this is just for windows? Is there a platform independent way to do this?Gaudy
System properties are not environment variables. Use System.getenv("USER") for environment variables. System.properties can still be set by user on java commandline with java -Duser.name=someoneelse so still not secureSeptuplet
"There are similar methods for unix based OS as well": Thanks, what i was looklng for. So what is the method for these OS? my research, currently conducted me ... here :) using "whoami" ?Taoism
What is "secure" methods that cannot be faked? Is it exists?Dashed
R
2

To get the currently logged in user path:

System.getProperty("user.home");
Recriminate answered 27/10, 2017 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.