Linux add path global

Add a Directory to the PATH on Linux

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Have you ever wondered how certain executables on Linux can be accessed as simple commands from the command line? Have you wanted to be able to run a program on Linux without having to provide the entire path?

These are the problems the PATH variable is designed to solve. In this tutorial, learn more about what the PATH variable is and how it works. Then, see how you can add your own directories to the PATH , allowing you to run programs as simple commands.

Before You Begin

  1. Familiarize yourself with our Getting Started with Linode guide and complete the steps for setting your Linode’s hostname and timezone.
  2. This guide uses sudo wherever possible. Complete the sections of our How to Secure Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.
  3. Update your system.
  4. On Debian and Ubuntu, use the following command:
 sudo apt update && sudo apt upgrade 

The steps in this guide are written for non-root users. Commands that require elevated privileges are prefixed with sudo . If you’re not familiar with the sudo command, see the Linux Users and Groups guide.

What Is the PATH Variable?

The $PATH environmental variable contains a colon-separated list of directories. These directories are where Linux looks for executables, allowing you to run these using only the executable names.

For instance, say you have an executable called program-to-run in the /usr/local/bin directory. Without that directory in your PATH , you would have to run the executable with:

Or by changing into the directory first and then running the executable:

cd /usr/local/bin ./program-to-run 

However, if you have the directory in your PATH , you can run the executable by simply giving its name as a command:

The PATH variable becomes especially useful when running specialized developer or system administrator tools as well as in-development applications. It allows you to run tools and applications efficiently while keeping them stored wherever best fits your needs.

View the PATH Variable

It can be useful to know what directories are already assigned to the PATH on your Linux system. You can do this easily with the echo command, like this:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

As you can see from the output above, Linux systems come by default with several convenient directories already on the PATH .

But sometimes these directories alone do not meet your needs. That is when you should look to customizing the PATH variable’s contents.

How to Add a Directory to the PATH

Linux comes with several directories in the PATH by default, like you can see in the output above. Typically, these are enough. Most programs you install on Linux put their executables in one of the default directories, making them easy to start using immediately.

But sometimes this is not the case. The PATH variable does not cover executables that you download or create and store in non-default directories. But that kind of storage is often the case when using one-off programs, working with particular system administrator tools, or when developing executables.

In such cases and similar ones, you likely want the ability to add additional directories to the PATH variable to make executables easier to work with.

Fortunately, you can do just that using the export command. Here is an example, adding the /etc/custom-directory directory to the PATH :

export PATH="$PATH:/etc/custom-directory" 

After the above command, you can run executables stored in the custom-directory simply by providing the executables’ names as commands.

How exactly does the export command achieve this? Follow along with this breakdown of the command to understand how each part works:

  • export PATH= starts to define the PATH variable.
  • $PATH ensures that the new definition of PATH begins with all of the contents of the existing PATH . In other words, including the PATH variable here is how you expand on the variable’s contents rather than overwrite them.
  • :/etc/custom-directory adds the new directory. Notice the placement of the colon, which is necessary because the PATH list is colon-separated.

You can verify the updated contents of PATH using the echo command as shown above:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/etc/custom-directory

PATH Variable Scope

Executing the export command above only updates the PATH variable for the current session. Logging out and logging back into your Linux system results in the PATH being reset.

There are two methods available should you want to have your directory added to the PATH in a more permanent way.

    You can alter the PATH variable for a given user by adding the export command to that user’s shell configuration file. The location of the configuration file varies depending on the shell program. For Bash, the configuration file is typically ~/.bashrc :

Источник

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:

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:

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.

Источник

Add directory to path globally on ubuntu server

How can I add my own directory to PATH without editing my local profile’s .bash_profile or .bashrc? I need to make the scripts in the directory universally accessible from any user that is logged in.

3 Answers 3

You should modify the file /etc/environment.

Modify it how? I tried using the same as what I would have put in profile or bashrc and it doesn’t do anything.

I tried adding the path to environment file. It didn’t work. However, the Raghuram’s suggestion worked like a charm.

This would be the best solution on systemd machines. It requires a reboot or logout/in to affect processes system wide. The same as bashrc

You could add the following in /etc/bashrc

export PATH="$PATH:/your/directory" 

/etc/bash.bashrc is the location. But refer to /etc/environment as this is the more recent way to achieve what was asked.

Better /etc/profile, so it should work for other shells, as sh

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Читайте также:  Canon mf211 драйвер linux
Оцените статью
Adblock
detector