Linux path all user

Executive Summary

Adding a directory to the path of a user or all users would seem trivial, but in fact it isn’t. The best place to add a directory to the path of a single user is to modify that user’s .bash_profile file. To add it to all users except user root, add it to /etc/profile. To also add it to the path of user root, add it to root‘s .bash_profile file.

Disclaimer

Obviously, you use this document at your own risk. I am not responsible for any damage or injury caused by your use of this document, or caused by errors and/or omissions in this document. If that’s not acceptable to you, you may not use this document. By using this document you are accepting this disclaimer.

Pre and Post Pathing

Linux determines the executable search path with the $PATH environment variable. To add directory /data/myscripts to the beginning of the $PATH environment variable, use the following:

  1. If, within the script, you export the environment variable it is effective within any programs called by the script. Note that it is not effective within the program that called the script.
  2. If the program that calls the script does so by inclusion instead of calling, any environment changes in the script are effective within the calling program. Such inclusion can be done with the dot command or the source command. Examples:
    . $HOME/myscript.sh
    source $HOME/myscript.sh

As an example, the bash shell program incorporates the contents of file .bash_profile by inclusion. So putting the following 2 lines in .bash_profile:

PATH=$PATH:/data/myscripts
export PATH

effectively puts those 2 lines of code in the bash program. So within bash the $PATH variable includes $HOME/myscript.sh, and because of the export statement, any programs called by bash have the altered $PATH variable. And because any programs you run from a bash prompt are called by bash, the new path is in force for anything you run from the bash prompt.

The bottom line is that to add a new directory to the path, you must append or prepend the directory to the $PATH environment variable within a script included in the shell, and you must export the $PATH environnment variable. The only remaining question is: In which script do you place those two lines of code?

Adding to a Single User’s Path

To add a directory to the path of a single user, place the lines in that user’s .bash_profile file. Typically, .bash_profile already contains changes to the $PATH variable and also contains an export statement, so you can simply add the desired directory to the end or beginning of the existing statement that changes the $PATH variable. However, if .bash_profile doesn’t contain the path changing code, simply add the following two lines to the end of the .bash_profile file:

Читайте также:  Ansible настройка astra linux

PATH=$PATH:/data/myscripts
export PATH

Adding to All Users’ Paths (except root)

You globally set a path in /etc/profile. That setting is global for all users except user root. Typical /etc/profile files extensively modify the $PATH variable, and then export that variable. What that means is you can modify the path by appending or prepending the desired directory(s) in existing statements modifying the path. Or, you can add your own path modification statements anywhere before the existing export statement. In the very unlikely event that there are no path modification or export statements in /etc/profile, you can insert the following 2 lines of code at the bottom of /etc/profile:

PATH=$PATH:/data/myscripts
export PATH

Adding to the Path of User root

User root is a special case, at least on Mandrake systems. Unlike other users, root is not affected by the path settings in /etc/profile. The reason is simple enough. User root‘s path is set from scratch by its .bash_profile script. In order to add to the path of user root, modify its .bash_profile.

Summary

A fundimental administration task is adding directories to the execution paths of one or more users. The basic code to do so is:

PATH=$PATH:/data/myscripts
export PATH

Place that code, or whatever part of that code isn’t already incorporated, in one of the following places:

User Class Script to modify
One user $HOME/.bash_profile
All users except root /etc/profile
root /root/.bash_profile

Источник

How to Add a Path Permanently in Linux

This Linux tutorial explains how to add a path permanently in Linux. It is optimized for both users who are looking for a fast practical answer and for users who are looking for understanding both global and user environment variables.

The tips provided in this article include two methods to add the persistent PATH for both specific and all users, being useful for every Linux distribution.

The first two sections of the content go straight to the point, describing the steps to add a path permanently. After which, you can find a short explanation on environment variables.

All instructions explained in this article contain screenshots, making it easy to understand and execute the examples.

How to Add a PATH Permanently to a Specific User in Linux

The first step before we start is to check our current PATH environment variable.

You can do it in a simple way by executing the echo command followed by a dollar sign ($) and the environment variable whose value you want to see which, in this case is the PATH, as shown in the following screenshot:

Читайте также:  Securing linux step by step

As you can see, there are 6 paths separated by colon. All paths must be separated by colon.

Another way to check all your environment variables including PATH is by running the env command as shown in the following example:

NOTE: While writing this tutorial, I edited my paths several times. That’s why you will see different values in the screenshots.

As you can see in the given figure, all environment variables are listed including the user paths.

To add a permanent path, one of the methods is to edit the hidden file .bashrc. You can print the hidden files by running the ls command followed by the –a (All) flag in the home directory.

To edit the .bashrc file, add a permanent path. Use the text editor of your preference. In my case, I used nano, as shown in the following:

In this first example, I added the fictitious path /home/linuxhint/something/default/bin where linuxhint is the user home.

The syntax is the following, where must be replaced with the actual path that you want to add:

In this case, I added the following line:

Alternatively, you can use the echo command to append the line using the following syntax where ‘Line Content’ must be replaced with the full command. And File must be replaced with the file you are editing (.bashrc).

The practical example is the following, as stated previously, in case I want to add the path $HOME/something/default/bin or /home/linuxhint/something/default/bin (which are the same):

Update your environment variables by running the following command:

To print all environment variables including updated paths, you can use the env command.

To show only the PATH environment variable, run the command shown in the following figure:

Another way to add a path to the user environment variable is by editing the “.profile” file located in the home directory.

This time, let’s use the ls command followed by the –l flag to show all files including the hidden files.

As you can see, there is a file named “.profile”. Edit it using the text editor of your choice. In my case, I used nano.

Find the line similar to the one pointed by the white arrow in the following figure:

In this example, I added the /home/linuxhint/something2/default/bin path.

Below the found line, add a line as shown at the end of the following image, replacing the /home/linuxhint/something2/default/bin with the actual path that you want to add:

Update your environment variable by executing the command shown in the following image:

Check the updated path with the following command:

Or print all the environment variables including PATH using the env command:

Читайте также:  Linux with 32 bit uefi

You can find the instructions to add a permanent path for all users in the following discussion.

How to Globally Add a Path Permanently to All Users in Linux

This section shows how to add a global persistent path environment variable for all users.

This can be done by editing two files, /etc/profile and /etc/bash.bashrc.

In the first example, I will show you how to edit the /etc/profile file.

Use the text editor that you used to modify the /etc/profile.

At the end of the file, add the following line where /opt/something10/bin must be replaced with the path that you want to add.

Update the environment variable PATH using the source command as done in the previous section of this article.

Check if the path was properly added using the echo command as shown in the following image:

Another way to permanently add a path globally is by editing the /etc/bash.bashrc file using the text editor to open it.

In the following example, I added the new path /opt/something20/bin.

Append a similar line as the following, replacing the /opt/something20/bin with the actual path that you want to add.

Update the PATH environment variable using the source command. Then, check it by running the echo command as shown in the following example:

As you can see, both methods worked successfully and the paths were added.

User vs System Wide Environment Variables

The difference between the user specific and the global environment variables is the following:

  • User Environment Variables: User environment variables like PATH are defined in the user home configuration files. They are loaded from the home directory when the user starts a session.
  • System Wide Variables: This type of variables don’t belong to a specific user, but to the whole system affecting all users.

This tutorial deeply explained how to add a PATH variable. To edit the user environment variables, you need to edit the .bashrc or .profile files located in the home directory. The variables exported to this file will load every time the user starts a session.

The system variables are stored in the /etc/bash.bashrc or /etc/profile files.

Conclusion

As you can see, adding the PATH variables both temporarily and permanently is pretty easy and can be done by any Linux user independently of the knowledge level. Every Linux user must understand the PATH variable function and how to manage it. Other variables are also deeply explained at Linux Hint. The content previously explained is valid for almost every Linux distribution.

Thank you for reading this tutorial showing how to permanently add the PATH variable. Keep following us for more professional content.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

Оцените статью
Adblock
detector