Linux / Unix: logname Command Examples To Display Loginname

by on January 23, 2012 · 0 comments· LAST UPDATED January 25, 2014

in , , ,

logname command

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.

logname command details
DescriptionShow login name
Category
Difficulty
Root privilegesNo
Estimated completion time2m
Contents
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

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
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: