How to use logname command in Linux.

How To Use Logname Command In Linux.

In the world of Linux and Unix-based systems, the command-line interface is a powerful tool that allows users to interact with the system efficiently. One such command that might not be as well-known as some others is “logname.” This command is a utility for retrieving the login name of the current user. In this article, we will delve into the logname command, providing a detailed explanation of its usage with practical examples.


Usage of the logname Command:

The logname command, as the name suggests, is used to display the login name of the current user. The login name is the username that you used to log in to the system. It can be helpful in various situations, such as writing scripts or automating tasks that require user-specific actions.


Basic Syntax:

The basic syntax of the logname command is as follows:


Examples of logname Command Usage:

Let’s explore some practical examples of using the logname command:

  1. Display the Current User’s Login Name:

    To retrieve the login name of the current user, simply enter the following command:


    This will return the username of the currently logged-in user.

  2. Use logname in a Shell Script:

    You can use the logname command in a shell script to perform user-specific actions. Here’s an example of a simple script that uses logname:

    echo "Hello, $current_user! Welcome to the script."

    Running this script will greet the user with their login name.

  3. Combine logname with Other Commands:

    You can combine the logname command with other commands to perform user-specific tasks. For instance, you can create a directory with the current user’s name:

    mkdir $(logname)_directory

    This will create a directory with the name of the current user.

  4. Redirect logname Output to a File:

    If you want to save the login name to a file, you can use output redirection. For example:

    logname > username.txt

    This will create a file named “username.txt” containing the login name.


Advanced Usage of the logname Command:

  1. Use in Conditional Statements:

    The logname command can be used in conditional statements to execute specific actions based on the current user. For example, you can create a script that performs different tasks for different users:

    if [ "$user" == "user1" ]; then
        # Actions for user1
    elif [ "$user" == "user2" ]; then
        # Actions for user2
        # Default actions

    This allows for user-specific configurations or behaviors in your scripts.

  2. Logging and Auditing:

    The logname command can be useful for logging and auditing purposes. You can incorporate it into your logging scripts to record which user performed specific actions, making it easier to track system activities.

    action="Some important action"
    echo "$timestamp - $user performed $action" >> system_log.txt

    This script will log the user’s actions along with a timestamp.

  3. Securing Sensitive Data:

    When working with sensitive data or configuration files, you can use the login name as part of a security mechanism. By ensuring that certain operations or access is restricted to specific users, you can enhance security.

    if [ "$(logname)" != "privileged_user" ]; then
        echo "Access denied. You must be 'privileged_user' to perform this action."
        exit 1

    This can help prevent unauthorized access to critical parts of the system.

  4. Integrating with Other Commands:

    Logname can be used in combination with other commands to customize their behavior. For instance, you can use it in combination with “sudo” to perform actions as the user invoking the command.

    sudo -u $(logname) some_commandv

    This executes “some_command” as the user who ran the script.



  • The logname command fetches the login name from the environment variable, which is set at login time. It may not reflect changes in the user’s identity made with “su” or “sudo.”

  • Always be cautious when using login names for security-related decisions. They can be spoofed, so use them as a part of your overall security strategy, not as the sole factor.

  • Be aware that some Linux distributions might not have the logname command installed by default. In such cases, you can usually find it in the “util-linux” package.

  1. Use in System Administration:

    System administrators can find the logname command especially useful for troubleshooting or system maintenance. By including logname in scripts or commands, administrators can maintain a record of who performed specific tasks on the system. This traceability can be vital when diagnosing issues or auditing system activities.

    # Example: Logging package updates with logname
    apt-get update >> update_log_${user}_${timestamp}.txt

    This script logs package updates with the current user’s name and a timestamp for reference.

  2. Logging Terminal Sessions:

    If you want to maintain a record of terminal sessions by user, the logname command can be used to include the user’s name in session logs. By appending the user’s name to your session logs, you can identify which user ran specific commands and when.

    script session_${user}.log

    Running this script will capture the entire terminal session in a file named after the current user.

  3. Customizing User Environment:

    The login name can be used to customize user environments by including it in configuration files such as the user’s shell profile. This way, each user can have their own custom settings based on their login name.

    For example, you can define specific aliases, paths, or environment variables in the user’s profile (e.g., .bashrc for Bash) based on their login name.

    # In the user's .bashrc file
    if [ "$(logname)" == "user1" ]; then
        alias myalias="some_command"
        export MY_PATH="/path/for/user1"
  4. Combining with Other System Information:

    You can combine the logname command with other system information commands to create more informative log entries or scripts. For instance, you can add the hostname and the current working directory to your logs or scripts for better context.

    echo "User: $user, Host: $hostname, Current Directory: $cwd" >> activity_log.txt

In summary, the logname command is a valuable tool in the Linux environment for various purposes, from user-specific scripting to enhancing system administration and security. By creatively using logname alongside other Linux utilities and within scripts, you can simplify tasks, improve system logging, and create a more personalized experience for each user on the system.



The logname command in Linux is a simple yet handy utility for retrieving the login name of the current user. It can be used in various situations, including scripting, automation, and user-specific tasks. By understanding how to use logname effectively, you can enhance your command-line skills and make your Linux experience more efficient.