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
Root privilegesNo
Estimated completion time2m
[/donotprint]This command reads var/run/utmp or /etc/utmp file to display the name of the current user.


The basic syntax:

logname --help
logname --version

logname command examples

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


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:

## My backup function
  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

$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

# : A shell script to display login name
## Let us change variable values ##
## 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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

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.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @