Linux logname Command Tutorial for Beginners (with Examples)

Linux offers several commands that display in output the current user's login name. One of those is logname. In this tutorial, we will quickly understand the basics of this command line tool using some easy to understand examples. But before we do that, it's worth mentioning all examples in this tutorial have been tested on CentOS 7.

Linux logname command

As already mentioned in the beginning, the logname command lets you know the login name of the current (or calling) user. Here's the syntax of this utility:

logname [OPTION]

And following is how the tool's man page explains it:

logname - print user´s login name
Print the name of the current user.

Here are some Q&A-styled examples that'll give you a good idea on how the logname utility works.

Q1. How to use logname?

Basic usage is very easy - all you have to do is to run 'logname' without any arguments.

logname

The command will produce the current user's login name. For example, in my case, it produced the following output:

tarra9779725351

Q2. Where does logname look for information?

The logname command accesses a system maintained file (often /var/run/utmp or /etc/utmp) to grab the information it produces in output. If the command executes successfully, its exit status is 0, else the status is 1.

Q3. How is logname different from other similar commands?

Some of you might argue why is the logname command is even needed when there exist other similar commands like whoami? Well, there's at-least one major difference between those commands and logname: it returns the underlying username even when you use it in the sudo mode.

The following screenshot clearly shows other similar commands produce 'root' in output when executed in the sudo mode, but logname produces the underlying username even in that case.

How is logname different from other similar commands

Note: To display generic help or version-related information, use the --help and --version command line options.

Conclusion

Logname is a very basic command line utility - it's easy to understand and use, with virtually no command line options. So overall, the learning curve is non-existent. To know more about this tool, head to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment