How to Determine the Current User Account in Linux

609
How to Determine the Current User Account in Linux

Here we can see, “How to Determine the Current User Account in Linux”

If Linux means anything, it means choice. You’ll achieve even an easy task like identifying the present user in some ways. This tutorial will show you ways to use a number of the quickest and easiest methods.

Why would you like to seek out the identity of the present user? In many cases, the owner of the pc is that the only user and, without getting too existential, they probably know themselves. Perhaps, but it’s also common for people to make additional user accounts to permit relations to access the pc. And, if you’re connected to a foreign shell on a server somewhere, you’ll need a brief reminder of the username you’re logged in with. If you see a logged-in session with nobody attending, how does one identify the present user from the command line?

Let’s try the simplest option first. All we’d like to try to do is check out the prompt. By default, Linux distributions have the username within the prompt. Simple, and we didn’t even need to A thing.

Use whoami to Display Your Username

To use the terminal window to display the user you’re actively logged in as, type the subsequent command:

whoami

The output of the above command shows the present user, like this:

jacob

To quickly see how this works with another user account (if you haven’t made the other users in Linux), use the sudo command to log in as root:

sudo su

Then, if you rerun the whoami command, you will be told that you’re root.

How to Do It With id -un

In a strange circumstance where whoami isn’t installed, there’s another command you’ll use to display your current username.

id -un

The result’s precisely the same because of the whoami command to display Jacob during this example.

The id command can show quite the present user. It also can show the user id, group id, and groups to which the user belongs. for instance, to point out only the influential group the user belongs to, type the following:

id -g

The above command only shows the group id. It doesn’t show the group name. to point out the effective group name, execute this command:

Also See:  Minecraft: Axolotls | How to tame an axolotl in the Cliffs & Caves update
id -gn

You can display every group id that a user belongs to by entering this:

id -G

The above command only shows the group ids. you’ll also use the Linux id command to display the group names:

id -Gn

If you would like to display your user id without the username, run the subsequent command:

id -u

More Information

You can use the –help switch with either whoami or id to seek out the present man page for every program.

id --help
whoami --help

To see the present version of id or whoami, use the subsequent commands:

id --version
whoami --version

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

We’ll start by watching commands to seek out a user’s account information, then elucidate commands to look at login details.

1. id Command

id may be a simple instruction utility for displaying true and effective user and group IDs as follows.

$ id tecmint 

uid=1000(tecmint) gid=1000(tecmint) groups=1000(tecmint),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),130(sambashare)

2. groups Command

groups command is used to show all the groups a user belongs to like this.

$ groups tecmint

tecmint : tecmint adm cdrom sudo dip plugdev lpadmin sambashare

3. finger Command

finger command is employed to look at information a few users on Linux. It doesn’t come per-installed on many Linux systems.

To install it on your system, run this command on the terminal.

$ sudo apt install finger	#Debian/Ubuntu 
$ sudo yum install finger	#RHEL/CentOS
$ sudo dnf install finger	#Fedora 22+

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

$ finger tecmint

Login: tecmint        			Name: TecMint
Directory: /home/tecmint            	Shell: /bin/bash
On since Fri Sep 22 10:39 (IST) on tty8 from :0
   2 hours 1 minute idle
No mail.
No Plan.

4. getent Command

getent maybe an instruction utility for fetching entries from Name Service Switch (NSS) libraries from a selected system database.

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

$ getent passwd tecmint

tecmint:x:1000:1000:TecMint,,,:/home/tecmint:/bin/bash

5. grep Command

grep command may be a powerful pattern searching tool available on most if not all Linus systems. you’ll use it to seek out information a few specific users from the system accounts file: /etc/passwd as shown below.

Also See:  Installing Windows from USB
$ grep -i tecmint /etc/passwd

tecmint:x:1000:1000:TecMint,,,:/home/tecmint:/bin/bash

6. lslogins Command

lslogins Command shows information about known users within the system; the -u flag only displays user accounts.

$ lslogins -u

UID USER       PROC PWD-LOCK PWD-DENY LAST-LOGIN GECOS
   0 root        144                              root
1000 tecmint      70                     10:39:07 TecMint,,,
1001 aaronkilik    0                              
1002 john          0                              John Doo

7. users Command

users command shows the usernames of all users currently logged on the system like so.

$ users

tecmint
aaron

8. who Command

who Command is employed to display users logged on the system, including the terminals they’re connecting from.

$ who -u

tecmint  tty8         2017-09-22 10:39 02:09        2067 (:0)

9. w Command

w command shows all users who are logged on the system and what they’re doing.

$ w

12:46:54 up  2:10,  1 user,  load average: 0.34, 0.44, 0.57
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
tecmint  tty8     :0               10:39    2:10m  4:43   0.46s cinnamon-sessio

10. last or lastb commands

last/lastb commands display an inventory of the last logged in users on the system.

$ last 
OR
$ last -a   #show hostname on the last column

List of Last Logged Users

tecmint  tty8         Fri Sep 22 10:39    gone - no logout  :0
reboot   system boot  Fri Sep 22 10:36   still running      4.4.0-21-generic
tecmint  tty8         Thu Sep 21 10:44 - down   (06:56)     :0
reboot   system boot  Thu Sep 21 10:42 - 17:40  (06:58)     4.4.0-21-generic
tecmint  tty8         Wed Sep 20 10:19 - down   (06:50)     :0
reboot   system boot  Wed Sep 20 10:17 - 17:10  (06:52)     4.4.0-21-generic
tecmint  pts/14       Tue Sep 19 15:15 - 15:16  (00:00)     tmux(14160).%146
tecmint  pts/13       Tue Sep 19 15:15 - 15:16  (00:00)     tmux(14160).%145
...

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

$ last -ap now

tecmint  tty8         Fri Sep 22 10:39    gone - no logout  :0
reboot   system boot  Fri Sep 22 10:36   still running      4.4.0-21-generic

wtmp begins Fri Sep  1 16:23:02 2017

11. lastlog Command

lastlog command is employed to seek out the small print of a recent login of all users or a given user as follows.

$ lastlog  
OR
$ lastlog -u tecmint 	#show lastlog records for specific user tecmint

Records of Recent Logged Users

Username         Port     From             Latest
root                                       **Never logged in**
kernoops                                   **Never logged in**
pulse                                      **Never logged in**
rtkit                                      **Never logged in**
saned                                      **Never logged in**
usbmux                                     **Never logged in**
mdm                                        **Never logged in**
tecmint          pts/1    127.0.0.1        Fri Jan  6 16:50:22 +0530 2017
..

That’s it! Do share it with us if you recognize the other command-line trick or command to look at user account details.

Conclusion 

I hope you found this helpful guide. If you’ve got any questions or comments, don’t hesitate to use the shape below. 

User Questions:

  1. What is your username?

Alternatively, as an account name, login ID, nickname, and user ID, username or user name is given to a user on a computer or network. This name is usually an abbreviation of the user’s full name or an alias.

  1. What is a real User ID?

Create a User ID that will be easy for you to recollect but hard for others to guess. Your User ID must be 6-16 characters and may contain most special characters apart from ` ‘ ” \ – ; () = and spaces. Your User ID and password can’t be equivalent.

Also See:  How to Unblock “Potentially Sensitive Content” on Twitter
  1. What is user ID Linux?

A UID (user identifier) may be a number assigned by Linux to every user on the system. This number is employed to spot the user to the design and work out which system resources the user can access. UID 0 (zero) is reserved for the basis.

  1. Separate user account for games; what’s wrong?

Separate user account for games, what’s wrong? from linux_gaming

  1. Protip: a straightforward method of managing & finding & deploying all of your little utility shell scripts…

Protip: an extremely simple method of managing & finding & deploying all your little utility shell scripts… from linux