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
Description Show login name
Category
Difficulty
Root privileges No
Estimated completion time 2m
[/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
Category List of Unix and Linux commands
File Management cat
Network Utilities dighostip
Package Manager apkapt
Processes Management bgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searching whereiswhich
User Information idgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Total
0
Shares
Leave a Reply

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

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts