Linux / Unix: logname Command Examples To Display Loginname

I am a new Linux and Unix system user. How do I find out the current login name on Linux or Unix-like operating system using command prompt?

You can display or print the name of the current user (also know as calling user) using logname command. [donotprint]

logname command details
DescriptionShow login name
CategoryN/A
DifficultyEasy
Root privilegesNo
Estimated completion time2m
Contents
[/donotprint]This command reads var/run/utmp or /etc/utmp file to display the name of the current user.

Syntax

The basic syntax:

logname
logname --help
logname --version

logname command examples

Just type the following command to display user’s login name:

logname

Sample outputs:

Animated gif 01: logname command in action along with id and whoami commands

You can use logname command in a shell script using Command substitution feature as follows:

#/bin/bash
## 
## My backup function
###
do_backup(){
  tar -zcvf /nas01/backup/home.files.tar.gz /home/ 
}
##
## Only run backup job, if it is backup user else die with message ##
##
[ "$(logname)" == "backup" ] && { echo "Starting backup..."; do_backup; } 
                             || { echo "Requires backup user account."; exit 1; }
 
exit 0

Historic commands equivalent to logname

The logname command is equivalent of running the following id command or whoami command:
id -un
OR
whoami

$USER and $LOGNAME shell variable

You can also use the $USER shell variable as follows to show user’s login name:

echo "Hi, $USER! Let us be friends."
echo "Hello, $LOGNAME! "

Sample outputs:

Hi, vivek! Let us be friends.
Hello, vivek!

Please note that the logname utility explicitly ignores the LOGNAME and USER environment variables because the environment cannot be trusted. This is the main reason to use logname command. Try the following demo

#!/bin/bash
# getlogin-name.sh : A shell script to display login name
 
## Let us change variable values ##
USER="foo"
LOGNAME="bar"
 
## Greet user ##
## Display imposter name i.e. you can not trust $USER and $LOGNAME ##
echo "Hi,$USER! You are Imposter, for sure. Sorry, I can not trust you."
echo "Hello, $LOGNAME! You are Imposter, for sure. I cannot trust you. Go away! "
 
## Display real login name ##
echo "Hello, $(logname)! Let us be friends."

Sample outputs:

Hi,foo! You are Imposter, for sure. Sorry, I can not trust you.
Hello, bar! You are Imposter, for sure. I cannot trust you. Go away! 
Hello, vivek! Let us be friends.
See also
  • logname(1) Linux command man page
  • /etc/passwd file
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.