Show current users 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.

Читайте также:  Astra linux oracle client

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.

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.

Читайте также:  Check file system linux ext4

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.

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).

Источник

12 Ways to Find User Account Info and Login Details in Linux

This article will show you useful ways to find information about users on a Linux system. Here we’ll describe commands to get a user’s account details, show login details as well what users are doing on the system.

If you want to add or create users in Linux, use the useradd command, and to modify or change any attributes of an already created user account, use the usermod command via the command line.

You might also like:

We’ll start by looking at 12 useful commands to find a user’s account information, then proceed to explain commands to view login details in the Linux system.

1. id Command – Show User and Group IDs

The id is a simple command line utility for displaying a real and effective user and group IDs identity information for the current user or specified user.

View User and Group ID

2. groups Command – View User Group Memberships

The groups command is used to display the group memberships for a user. It lists all the groups that a user belongs to, including both primary and supplementary groups.

Check User Groups in Linux

3. finger Command – Show User Information

The finger command is used to search for information about a user on Linux, which includes detailed information about a specific user or a list of users, including their login name, real name, terminal, idle time, login time, and other relevant details.

The finger command doesn’t come pre-installed on many Linux distributions, you need to install it using your default package manager as shown.

$ sudo apt install finger [On Debian, Ubuntu and Mint] $ sudo yum install finger [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/finger [On Gentoo Linux] $ sudo apk add finger [On Alpine Linux] $ sudo pacman -S finger [On Arch Linux] $ sudo zypper install finger [On OpenSUSE]

It shows a user’s real name; home directory; shell; login: name, time; and so much more as shown below.

Check User Info in Linux

4. getent Command – Fetch User Info from System Database

The getent command is used to retrieve information from various databases, including the system user and group databases. It can be used to retrieve information about users, groups, hosts, networks, protocols, and other system entities that are stored in database files like /etc/passwd, /etc/group, /etc/hosts, etc.

Читайте также:  Linux read file mode

To get a user’s account details, use the passwd database and the username as follows.

Fetch User Info in Linux

5. grep Command – Search for Patterns or Specific Text in Files

The grep command is a powerful command used to search for patterns or specific text in files. It allows you to filter and extract lines from text based on matching patterns. The name “grep” stands for “Global Regular Expression Print“.

You might also like:

You can use grep to find information about a specific user from the system accounts file: /etc/passwd as shown below.

Grep - Fetch User Info in Linux

6. lslogins Command – Display User Information in Linux

The lslogins command shows information about known users in the system, which typically includes details such as the username, UID (User ID), GID (Group ID), home directory, shell, last login time, and more, depending on the options used and the system configuration.

$ lslogins -u tecmint $ lslogins -u

Display User Info in Linux

7. users Command – List Current Logged-In Users on Linux

The users command is used to display the list of currently logged-in users on the Linux system.

Show Current Logged in Users in Linux

8. who Command – Show Information Of Currently Logged-In Users

The who command is used to display users who are logged on to the system, including the username, terminal, login time, and remote host from which the user is logged in.

Show Current Logged in Users

9. w Command – Show Currently Logged-In User Activity

The w command shows a summary of the currently logged-in users and their activity, which displays the login session, including the username, terminal, login time, idle time, JCPU (total CPU time used by all processes), PCPU (CPU time used by the current process), and the command or process running on the terminal.

Show Currently Logged-In User Activity

10. last Command – Show Most Recent Login Session

The last command displays a list of the most recent login sessions, which includes information about the users who have logged in, their login times, and the terminals or remote hosts they used for login.

Show Most Recent Login Sessions

To show all the users who were present at a specified time, use the -p option as follows.

List Most Recent User Logins

11. lastb Command – Show Failed Login Attempts

The lastb command is used to display a list of the last failed login attempts on the system. It reads from the system log file that records failed login attempts, typically stored in /var/log/btmp.

Show Failed Login Attempts in Linux

12. lastlog Command – List User Login Information

lastlog command is used to find the details of the most recent login information for all users or a specific user on the system, which provides details about the last login time and location for each user.

$ lastlog OR $ lastlog -u tecmint

List User Login Information

That’s it! If you know any other command-line trick or command to view user account details do share with us.

You might also like:

In this article, we’ve explained various ways to find information about users and login details on a Linux system. You can ask any questions or share your thoughts via the feedback form below.

Источник

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