174

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?

Beryllium
  • 12,808
  • 10
  • 56
  • 86
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238

4 Answers4

284

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());
Selva
  • 546
  • 5
  • 12
  • 34
cordellcp3
  • 3,557
  • 1
  • 17
  • 14
  • 3
    Does `System.getProperty("user.name"); ` work on both windows and linux? – Catfish Apr 02 '14 at 20:15
  • 7
    However, the `user.name` value [can be spoofed](http://stackoverflow.com/a/27899761/545127), so it should not be used upon for authentication. – Raedwald Jan 12 '15 at 10:26
  • 17
    System.getProperty("user.name") is NOT currently logged in user, it's user under who's security context is JVM running. – rkosegi Sep 30 '15 at 11:45
  • 1
    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 http://stackoverflow.com/a/40702767/1504556 for explanation. – peterh Nov 30 '16 at 17:19
102

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    Upvote for answering all of the OPs original questions bar one clearly and concisely. Surely this should be the accepted answer – Andy Jan 01 '13 at 21:53
  • 1
    Agree with @Andy - up-vote for clear and concise answer to all points. – Paul Eden Jan 30 '14 at 20:54
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

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
phoenix
  • 119
  • 1
  • 2
  • So this _is_ just for windows? Is there a _platform independent_ way to do this? – Stuart R. Jefferys Sep 24 '12 at 13:25
  • 7
    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 secure – dan carter Aug 23 '13 at 02:51
  • "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" ? – pdem Aug 26 '16 at 08:02
2

To get the currently logged in user path:

System.getProperty("user.home");
Swapnil1156035
  • 361
  • 1
  • 11