just wondering, if there is a way to know which user i'm using in linux, without using 'whoami' command in terminal. May be there is a way to know it from a file? Some file where the current username is stored or something?
Thanks in Advance!
just wondering, if there is a way to know which user i'm using in linux, without using 'whoami' command in terminal. May be there is a way to know it from a file? Some file where the current username is stored or something?
Thanks in Advance!
The scenic route:
The id command can display user information, but when called without arguments it also includes all your groups, so you'll have to massage the output a bit.
id
uid=501(robert) gid=20(staff) groups=20(staff),12(everyone)...
First, use cut to pick up only the user ID part:
id | cut -d " " -f 1
uid=501(robert)
Then send that through another cut to remove the leading uid= part:
id | cut -d " " -f 1 | cut -c 5-
501(robert)
Finally, use sed to remove all characters that are not letters:
id | cut -d " " -f 1 | cut -c 5- | sed -E 's/[^a-zA-Z]*//'g
robert
The quick route, as user2394284 mentioned:
id --user --name
Guess I should actually read the man pages I link to.
environment variables USER and/or LOGNAME are usually set up for your convenience.
$ echo $USER
bineinfachvorbeigekommen
$ echo $LOGNAME
bineinfachvorbeigekommen
On Linux, you can't be certain that a process is associated with a user name. The best you can be sure of is that it has a numerical user ID. In fact, it will have two numeric user IDs -- the "real user ID" and the "effective user ID". Normally these are the same.
These user IDs are exposed by the kernel -- there is a kernel system call that retrieves them, and wrappers for that system call in most programming languages (e.g., getuid() in C).
User name, however, is not an intrinsic property of the process, and there's no knock-down certain way to find it from within an application. In fact, it's possible to run a process under a (numerical) user ID that has no corresponding name -- this is something that won't normally happen in desktop Linux, but is relatively normal in embedded and container systems.
User names are stored in some authentication database, like /etc/passwd, and can be determined from the user ID -- usually. Most Linux shells set environment variables like $LOGNAME and $USER to make life a bit easier for programmers. Utilities like whoami will use various methods to map the numeric user ID to a name, but this won't be foolproof, for reasons explained above.