Summary of Configuration Methods of Environment Variables under Linux (Difference between. bash_profile and. bashrc)

  • 2021-07-26 09:13:34
  • OfStack

On an linux system, if an application is downloaded and installed, it is very likely that the prompt "command not found" will appear when you type its name at startup. If you go to the installation destination folder every time, find the executable file

It is too cumbersome to operate. In this case, it involves the setting of environment variable PATH, and the setting of PATH is also an integral part of customizing environment variable under linux.

Two ways to configure environment variables:

1) Modify the/etc/profile file

This approach is recommended because all users' shell have the right to use these environment variables, and the disadvantage is that it may cause security problems for the system. This is for all users, all shell;;

[root@test ~]# vim /etc/profile
....
export PATH=$PATH:/usr/local/mysql/bin

Use the source command to make the changes take effect immediately:
[root@test ~]# source /etc/profile

2) Modify. bashrc file, this method is more secure, it can use these environment variables to control the authority to the user level, here is for a specific user, if you need to give a user authority
To use these environment variables, you only need to modify the. bashrc file in their personal user home directory.
[root@test ~]# vim /root/.bashrc
export PATH=$PATH:/usr/local/mysql/bin

[root@test ~]# source /root/.bashrc

It should be noted that:

When setting system environment variables in/etc/profile, the end of the path cannot end with "/", otherwise it will cause an error in the whole PATH variable.

[app@test ~]$ vim ~/.bashrc
......
KETTLE_HOME=/data/nstc/kettle3.2
export KETTLE_HOME

Note: After configuring the environment variable, remember that export outputs this variable, otherwise the following source is invalid!
[app @ test ~] $source. bashrc//Make it effective
[app@test ~]$ echo $KETTLE_HOME
/data/nstc/kettle3.2
[app@test ~]$ env
.........
KETTLE_HOME=/data/nstc/kettle3.2

Difference between. bash_profile and. bashrc:

/etc/profile: This file sets the environment information for each user of the system and is executed when the user logs in for the first time. The settings for shell are collected from the configuration files in the/etc/profile. d directory.
/etc/bashrc: Executes this file for every user running bash shell. When bash shell is opened, the file is read.
~ /.bash_profile: Each user can use this file to enter shell information for their own use, and the file executes only once when the user logs in! By default, he sets 1 environment variable and executes the user's. bashrc file.
~/. bashrc: This file contains bash information specific to your bash shell, which is read when logging in and every time a new shell is opened.
~ /.bash_logout: Executes this file every time you exit the system (exit bash shell).

In addition, the variables set in/etc/profile (global) can act on any user, while the variables set in ~/. bashrc (local) can only inherit the variables in/etc/profile, which are "parent-child" relationship.

==================================================================

Remote login linux server, how to set the terminal failure time (that is, how long after the operation, the terminal is about to fail). The method is as follows:
[root@mq-console-nameserver ~]# vim /etc/profile
......
export TMOUT=600
[root@mq-console-nameserver ~]# source /etc/profile

After the above setting, the terminal logged in to this server does not operate within 10 minutes, then the terminal will be invalid!

Summarize


Related articles: