Find current user linux

How do I get the current user’s username in Bash?

It seems none of the methods proposed so far work without relying on $USER or invoking a separate process. Is there no bash builtin to get the username without invoking a separate process?

When you’ve heard a command but aren’t sure how to use it, checking man whoami is usually a good first stop to check for documentation.

14 Answers 14

On the command line, enter

Just a quick note that $USER and whoami return different values if your running a command through ssh as another user. whoami returns the OS user and $USER returns the ssh user.

In some cases, $USER is not set at all. Worse, it is just an environment variable, so it can be overridden by the user: USER=thisisnotmyname bash -c ‘echo $USER’ # prints thisisnotmyname

@SethMMorton I realise I made the issue sound worse than it usually is. To answer the question, though, using whoami (as you suggested) eliminates the problem altogether, assuming overridden environment variables is a potential issue in your context.

«current username» is slightly ambiguous. What do you want to get when running under sudo? «echo $USER» produces the name I logged in as whether run under sudo or not, while «whoami» returns «root» when run under sudo and my actual login name otherwise. Scripts that need to be run as sudo are more likely to be in that minority of scripts that have need of your login name rather than «root».

An alternative to whoami is id -u -n .

id -u will return the user id (e.g. 0 for root).

Unless I’m mistaken this would be the way to go if portability is a concern as the id command and -u and -n flags are a part of posix

This really should be the accepted answer. «$» and whoami both depend on how you log in. Specifically, login shells and sudo will set $USER, and whoami looks at the user attached to stdin. However, if you are running a batch job from cron, or you are running a startup script as a different user than root, then these will either output the wrong user (root) or nothing at all. This answer will return the correct value regardless by looking at process’s user ID.

if you tried the command before adding untrue comments, you would see that the -n argument prints the username, just like the original question asked. see the following: id -u -n prints brett — even on darwin.

A great alternative when checking on live container instances with very few command line apps installed. whoami isn’t installed on many of the lite images out there.

Читайте также:  Linux finding usb devices

Use the standard Unix/Linux/BSD/MacOS command logname to retrieve the logged in user. This ignores the environment as well as sudo, as these are unreliable reporters. It will always print the logged in user’s name and then exit. This command has been around since about 1981.

My-Mac:~ devin$ logname devin My-Mac:~ devin$ sudo logname Password: devin My-Mac:~ devin$ sudo su - My-Mac:~ root# logname devin My-Mac:~ root# echo $USER root 

This was particularly helpful to me over whoami or $USER as I am using sudo to execute as another user, but want the original user not the sudo user.

BTW this is the best answer, not only for me personally but also to the purpose of the OP’s question.

A hack the I’ve used on Solaris 9 and Linux and which works fine for both of them:

This snippet prints the name of the user with the current EUID.

NOTE: you need Bash as the interpreter here.

On Solaris you have problems with methods, described above:

  • id does not accept the -u and -n parameters (so you will have to parse the output)
  • whoami does not exist (by default)
  • who am I prints owner of current terminal (ignores EUID)
  • $USER variable is set correctly only after reading profile files (for example, /etc/profile )

Why do you need bash as the interpreter? Nothing in the command line shown seems to be specific to any shell. In fact, why even include the pipe through awk? As far as I can tell, your ps command is everything required to display the owner of the current shell’s pid.

For us as humans to disregard the superfluous information is natural. The awk portion isolates the desired data— for variables or in general the computer that can’t make on the fly assumptions just yet at this rudimentary level.

On Solaris, use command -p id (from a POSIX shell) or /usr/xpg4/bin/id . More generally, on Solaris, you’d want to modify your environment to put yourself in a POSIX environment (with something like PATH= getconf PATH` and be sure to run /usr/xpg4/bin/sh ) to avoid being stuck with commands from the 70s/80s.

  1. id prints the user id along with the groups. Format: uid=usernumber(username) .
  2. whoami gives the current user name

$whoami isn’t available as a variable in bash. You need to do either $(whoami), or `whoami` to actually execute the whoami command!

When root (sudo) permissions are required, which is usually 90%+ when using scripts, the methods in previous answers always give you root as the answer.

To get the current «logged in» user is just as simple, but it requires accessing different variables: $SUDO_UID and $SUDO_USER .

echo $SUDO_UID echo $SUDO_USER 
myuid=$SUDO_UID myuname=$SUDO_USER 

In Solaris OS I used this command:

$ who am i # Remember to use it with space. 

On Linux- Someone already answered this in comments.

Those 2 commands display 2 different informations. Just log as root, use «su — xxx», and see for yourself.

. gets you the regular user (if non-sudo) → or ← the regular user behind the current sudo call.

How could i do it in nested quotes? e.g. my_var=»$(‘/some/path/to/$/more/path’ ‘and/something/else’)»

The current user’s username can be gotten in pure Bash with the $ parameter expansion (introduced in Bash 4.4):

The : built-in (synonym of true ) is used instead of a temporary variable by setting the last argument, which is stored in $_ . We then expand it ( \u ) as if it were a prompt string with the P operator.

Читайте также:  Astra linux orel sources list

This is better than using $USER , as $USER is just a regular environmental variable; it can be modified, unset, etc. Even if it isn’t intentionally tampered with, a common case where it’s still incorrect is when the user is switched without starting a login shell ( su ‘s default).

Источник

How to Check the Current User in Linux Shell: Top Commands for User Information

Learn how to check the current user and logged-in users in Linux shell using top commands like ps, w, who, /etc/passwd, whoami, and more. Ensure system security and troubleshoot issues.

As a Linux system administrator, it is important to know how to check the current user in Linux shell. There are various commands available in the Linux shell that can be used to check the current user and logged-in users. In this blog post, we will discuss the top Linux shell commands that can be used to check user information.

Using ps -p $$ and echo “$SHELL” to Check the Current User in Linux Shell

The ps command with the -p option followed by the process ID ( $$ ) displays information about the current process, including the user who started it. The echo command with the $SHELL variable shows the current user’s default shell. These commands are useful for quickly checking the current user in Linux shell.

Using w command and who command to Show Current Logged In Users in Linux

The w command displays the list of logged-in users along with their processes and system uptime. The who command shows the list of users currently logged in, their login time, and terminal. These commands are helpful to check who is currently logged in to the system.

Using /etc/passwd File to Check Existing Groups and Users

The /etc/passwd file stores user account information, including the username, user ID, default group ID, home directory, and default shell. The file can be used to check existing users and groups on a Linux system.

Using whoami Command and id Command to Display the Username of the Current User

The whoami command displays the username of the current user. The id command shows the user and group information for the current user. These commands are useful for quickly identifying the current user and their group information.

Other Methods to Find User Account Info and Login Details in Linux

The finger command searches for information about a user on a Linux system, including the user’s full name, home directory, and login shell. The logname command displays the current login name on a Linux or Unix-like operating system using the command prompt. The users command lists all users on the system. The getent command can be used to list all users in the system, including those in remote databases such as LDAP.

finger username logname users getent passwd 

Other helpful code examples for checking the current user in Linux shell

In shell, linux get current user code example

In shell, how to see users logged in linux code example

# See who is logged in and what they are running ps au

In shell, check user shell in linux code example

In shell, how to see users logged in linux code example

Conclusion

In conclusion, checking the current user and logged-in users is essential for administering a Linux system. The Linux shell provides various commands to check user information, including ps , echo , w , who , /etc/passwd , whoami , id , finger , logname , users , and getent . Using these commands can help users find user account information and login details, troubleshoot issues, and ensure system security. As a Linux system administrator, it is important to know how to use these commands to check user information on a Linux system.

Читайте также:  Arm linux gnueabi linaro

Источник

How to know your current user in Linux without ‘whoami’ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

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!

That assumes you’re using that as your user database and not LDAP. That’s not always a safe assumption.

3 Answers 3

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:

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.

Источник

Оцените статью
Adblock
detector