I am using AWS EC2 along with the CodeStar services.
I wanted to add the path to maven's binary to the PATH environment variable.
So I wrote it to the /etc/environment file and executed the command:
source /etc/environment
But it has now changed the default environment of the bash shell and now I am not able to execute any command. Even for a command like ls it gives output -bash: command not found
How do I revert back to the default settings. I tried whatever my brain could think of. But nothing helped. Would be glad to hear from the community.
In order to revert back to the default settings:
As #Cyrus wrote you should update the PATH variable
In the bash shell run the following command:
PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin"
Undo the changes to the /etc/environment
edit the /etc/environment using the same method you used before, and remove the PATH line you added to the file
You can use sudo vi /etc/environment or any other editor you prefer
Note: In my AWS machine, the /etc/environment file is empty.
Related
I am running RHEL 6, and I have exported an environment variable like this:
export DISPLAY=:0
That variable is lost when the terminal is closed. How do I permanently add this so that this variable value always exists with a particular user?
You can add it to your shell configuration file, e.g., $HOME/.bashrc or more globally in /etc/environment.
After adding these lines, the changes won't reflect instantly in GUI-based systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.
You have to edit three files to set a permanent environment variable as follow:
~/.bashrc
When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:
export DISPLAY=0
~/.profile
Same as bashrc you have to put the mentioned command line at the end of this file to have your environment variable in every login of your OS.
/etc/environment
If you want your environment variable in every window or application (not just terminal window) you have to edit this file. Add the following command at the end of this file:
DISPLAY=0
Note that in this file you do not have to write export command
Normally you have to restart your computer to apply these changes. But you can apply changes in bashrc and profile by these commands:
$ source ~/.bashrc
$ source ~/.profile
But for /etc/environment you have no choice but restarting (as far as I know)
A Simple Solution
I've written a simple script for these procedures to do all those work. You just have to set the name and value of your environment variable.
#!/bin/bash
echo "Enter variable name: "
read variable_name
echo "Enter variable value: "
read variable_value
echo "adding " $variable_name " to environment variables: " $variable_value
echo "export "$variable_name"="$variable_value>>~/.bashrc
echo $variable_name"="$variable_value>>~/.profile
echo $variable_name"="$variable_value>>/etc/environment
source ~/.bashrc
source ~/.profile
echo "do you want to restart your computer to apply changes in /etc/environment file? yes(y)no(n)"
read restart
case $restart in
y) sudo shutdown -r 0;;
n) echo "don't forget to restart your computer manually";;
esac
exit
Save these lines in a shfile then make it executable and just run it!
Add the line to your .bashrc file or .profile.
The variables set in file $HOME/.profile are active for the current user, and the ones in /etc/profile are global. The .bashrc file is pulled on each Bash session start.
On Ubuntu systems, use the following locations:
System-wide persistent variables in the format of JAVA_PATH=/usr/local/java store in
/etc/environment
System-wide persistent variables that reference variables such as
export PATH="$JAVA_PATH:$PATH" store in
/etc/.bashrc
User-specific persistent variables in the format of PATH DEFAULT=/usr/bin:usr/local/bin store in
~/.pam_environment
For more details on #2, check this
Ask Ubuntu answer.
NOTE: #3 is the Ubuntu recommendation, but it may have security concerns in the real world.
If it suits anyone, here are some brief guidelines for adding environment variables permanently.
vi ~/.bash_profile
Add the variables to the file:
export DISPLAY=:0
export JAVA_HOME=~/opt/openjdk11
Immediately apply all changes:
source ~/.bash_profile
Source: How to Set Environment Variables in Linux
A particular example:
I have Java 7 and Java 6 installed, I need to run some builds with 6, others with 7. Therefore I need to dynamically alter JAVA_HOME so that Maven picks up what I want for each build. I did the following:
created j6.sh script which simply does export JAVA_HOME=... path to j6 install...
then, as suggested by one of the comments above, whenever I need J6 for a build, I run source j6.sh in that respective command terminal. By default, my JAVA_HOME is set to J7.
I would like to switch to the bash environment after ssh login. Currently I have to type bash every time and then cd to my working directory.
Where can I add some settings so it will run automatically for me.
There's an environment variable SHELL that is set to the current shell. You can set it to your preferred shell by running
$ export SHELL=/path/to/shell
In order to find the path to your preferred shell just run following command
$ whereis bash
Enter the path returned by whereis command as your shell path.
There's a .profile(ls -al) or bash_profile that you can add your setting there. If you can't find such a file then create one using touch .profile. (I did create this file on OS X.)
Open the .profile in order to edit it using whatever text editors that you want
$ vim .profile
Then change the $HOME environment variable in order to change your default home directory path. Enter below line in opened file
export HOME=/your/path
Save the file
:w
I have added HADOOP_INSTALL and it's bin to the PATH variable in my .bash_profile (shown below) and executed it using the command . .bash_profile. I can run the command hadoop version fine but when I close the terminal and run the same command again it gives me error as follows
gsidevas#gsidev-cloudvm ~]$ hadoop version
bash: hadoop: command not found
Current .bash_profile
export HADOOP_INSTALL=/usr/local/hadoop
PATH=$PATH:$HOME/bin:$HADOOP_INSTALL/bin
export PATH
What do I need to do so that this HADOOP_INSTALL and it's bin gets set permanently in my environment?
By default, BASH reads and executes commands in .bash_profile only in login shells. If you're creating a terminal via some X11 or similar software, chances are that terminal is not a login shell by default.
You can achieve this effect for every shell by simply moving the changes you made into your .bashrc file. Please note that this only works properly if your username on the system uses "bash" as its shell and not "sh" since for "sh" the .bashrc file is, by default, ignored.
I am running RHEL 6, and I have exported an environment variable like this:
export DISPLAY=:0
That variable is lost when the terminal is closed. How do I permanently add this so that this variable value always exists with a particular user?
You can add it to your shell configuration file, e.g., $HOME/.bashrc or more globally in /etc/environment.
After adding these lines, the changes won't reflect instantly in GUI-based systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.
You have to edit three files to set a permanent environment variable as follow:
~/.bashrc
When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:
export DISPLAY=0
~/.profile
Same as bashrc you have to put the mentioned command line at the end of this file to have your environment variable in every login of your OS.
/etc/environment
If you want your environment variable in every window or application (not just terminal window) you have to edit this file. Add the following command at the end of this file:
DISPLAY=0
Note that in this file you do not have to write export command
Normally you have to restart your computer to apply these changes. But you can apply changes in bashrc and profile by these commands:
$ source ~/.bashrc
$ source ~/.profile
But for /etc/environment you have no choice but restarting (as far as I know)
A Simple Solution
I've written a simple script for these procedures to do all those work. You just have to set the name and value of your environment variable.
#!/bin/bash
echo "Enter variable name: "
read variable_name
echo "Enter variable value: "
read variable_value
echo "adding " $variable_name " to environment variables: " $variable_value
echo "export "$variable_name"="$variable_value>>~/.bashrc
echo $variable_name"="$variable_value>>~/.profile
echo $variable_name"="$variable_value>>/etc/environment
source ~/.bashrc
source ~/.profile
echo "do you want to restart your computer to apply changes in /etc/environment file? yes(y)no(n)"
read restart
case $restart in
y) sudo shutdown -r 0;;
n) echo "don't forget to restart your computer manually";;
esac
exit
Save these lines in a shfile then make it executable and just run it!
Add the line to your .bashrc file or .profile.
The variables set in file $HOME/.profile are active for the current user, and the ones in /etc/profile are global. The .bashrc file is pulled on each Bash session start.
On Ubuntu systems, use the following locations:
System-wide persistent variables in the format of JAVA_PATH=/usr/local/java store in
/etc/environment
System-wide persistent variables that reference variables such as
export PATH="$JAVA_PATH:$PATH" store in
/etc/.bashrc
User-specific persistent variables in the format of PATH DEFAULT=/usr/bin:usr/local/bin store in
~/.pam_environment
For more details on #2, check this
Ask Ubuntu answer.
NOTE: #3 is the Ubuntu recommendation, but it may have security concerns in the real world.
If it suits anyone, here are some brief guidelines for adding environment variables permanently.
vi ~/.bash_profile
Add the variables to the file:
export DISPLAY=:0
export JAVA_HOME=~/opt/openjdk11
Immediately apply all changes:
source ~/.bash_profile
Source: How to Set Environment Variables in Linux
A particular example:
I have Java 7 and Java 6 installed, I need to run some builds with 6, others with 7. Therefore I need to dynamically alter JAVA_HOME so that Maven picks up what I want for each build. I did the following:
created j6.sh script which simply does export JAVA_HOME=... path to j6 install...
then, as suggested by one of the comments above, whenever I need J6 for a build, I run source j6.sh in that respective command terminal. By default, my JAVA_HOME is set to J7.
I'm using knoppix 7.0.3 and trying to set the PATH environment variable. According to the official Ubuntu documentation, /etc/environment should be the preferred place for this. So I added these lines in the file:
JAVA_HOME="/usr/lib/jvm/java-6-sun"
GRAILS_HOME="/home/knoppix/grails"
PATH="${PATH}:${JAVA_HOME}/bin:${GRAILS_HOME}/bin"
But after rebooting the system, the file just reverted to the original one (I was using persistent storage).
Then after some Googling, I tried to edit ~/.profile like this:
export JAVA_HOME="/usr/lib/jvm/java-6-sun"
export GRAILS_HOME="/home/knoppix/grails"
export PATH=$PATH:$JAVA_HOME/bin:$GRAILS_HOME/bin
This time, the first two variables got set (echoed in console), but the PATH didn't. It was still the default one when I echoed. What's wrong?
Modify /etc/profile on the following line:
PATH=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
The problem is that your PATH is being overwritten during initialization of bash, after reading .profile.
From the manpage of bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
...
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
From your experience, it is apparent that if .bashrc doesn't exist, bash is trying to set PATH to a default value (I would appreciate if someone confirms this).
As we discussed in the comments on your question, adding the export commands to .bashrc (and thus creating the file) solves the problem. Alternatively, you can add
source ~/.profile
to the end of your .bashrc file for the same effect.