Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc every time in the terminal.
What is the best way I don't need to provide source every time when I reopen the terminal?
What I did?
I am trying to add source of the .bashrc file in this file but it is a read-only file. I am not able to add the source of the .bashrc file in this profile.
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
sudo chmod u+w /etc/profile
Profile:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
It looks like your terminal emulator is launching bash as a login shell.
If that's the case, it will read /etc/profile for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
~/.bash_profile
~/.bash_login
~/.profile
It will thus ignore your .bashrc file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add the following line to your ~/.bash_profile :
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Here is a link to the documentation about which files are loaded depending of the type of shell you are running
As per #Aserre's answer i have followed this step to solve this issue
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
Start up Terminal
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it
Restart the terminal, It should work
You should write this line source .profile inside your .zshrc file. This is because default shell is zsh. If u don't want to do this solution than u can go for changing the default shell by typing the following command chsh -s /bin/bash then restart your machine or virtual machine. Then no need for source. I hope this will help :) TAKE CARE
If you are using Linux and you want variables set, to persist.
Follow the below steps.
Be the root user -> sudo su
go to etc folder -> cd /etc
open the file bashrc with the editor of your choice -> vi bashrc
set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
Load the bashrc file with command ->
. bashrc
remember to put the dot/period before bashrc.
now JAVA_HOME should be set permanently.
Thanks...
I have a script called foo.sh in my home folder.
When I navigate to this folder, and enter ./foo.sh, I get
-bash: ./foo.sh: Permission denied.
When I use sudo ./foo.sh, I get
sudo: foo.sh: command not found.
Why does this happen and how I can fix it?
Permission denied
In order to run a script the file must have an executable permission bit set.
In order to fully understand Linux file permissions you can study the documentation for the chmod command. chmod, an abbreviation of change mode, is the command that is used to change the permission settings of a file.
To read the chmod documentation for your local system , run man chmod or info chmod from the command line. Once read and understood you should be able to understand the output of running ...
ls -l foo.sh
... which will list the READ, WRITE and EXECUTE permissions for the file owner, the group owner and everyone else who is not the file owner or a member of the group to which the file belongs (that last permission group is sometimes referred to as "world" or "other")
Here's a summary of how to troubleshoot the Permission Denied error in your case.
$ ls -l foo.sh # Check file permissions of foo
-rw-r--r-- 1 rkielty users 0 2012-10-21 14:47 foo.sh
^^^
^^^ | ^^^ ^^^^^^^ ^^^^^
| | | | |
Owner| World | |
| | Name of
Group | Group
Name of
Owner
Owner has read and write access rw but the - indicates that the executable permission is missing
The chmod command fixes that. (Group and other only have read permission set on the file, they cannot write to it or execute it)
$ chmod +x foo.sh # The owner can set the executable permission on foo.sh
$ ls -l foo.sh # Now we see an x after the rw
-rwxr-xr-x 1 rkielty users 0 2012-10-21 14:47 foo.sh
^ ^ ^
foo.sh is now executable as far as Linux is concerned.
Using sudo results in Command not found
When you run a command using sudo you are effectively running it as the superuser or root.
The reason that the root user is not finding your command is likely that the PATH environment variable for root does not include the directory where foo.sh is located. Hence the command is not found.
The PATH environment variable contains a list of directories which are searched for commands. Each user sets their own PATH variable according to their needs.
To see what it is set to run
env | grep ^PATH
Here's some sample output of running the above env command first as an ordinary user and then as the root user using sudo
rkielty#rkielty-laptop:~$ env | grep ^PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
rkielty#rkielty-laptop:~$ sudo env | grep ^PATH
[sudo] password for rkielty:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
Note that, although similar, in this case the directories contained in the PATH the non-privileged user (rkielty) and the super user are not the same.
The directory where foo.sh resides is not present in the PATH variable of the root user, hence the command not found error.
The other solutions I've seen here so far are based on some system definitions, but it's in fact possible to have sudo use the current PATH (with the env command) and/or the rest of the environment (with the -E option) just by invoking it right:
sudo -E env "PATH=$PATH" <command> [arguments]
In fact, one can make an alias out of it:
alias mysudo='sudo -E env "PATH=$PATH"'
(It's also possible to name the alias itself sudo, replacing the original sudo.)
Check for secure_path on sudo
[root#host ~]# sudo -V | grep 'Value to override'
Value to override user's $PATH with: /sbin:/bin:/usr/sbin:/usr/bin
If $PATH is being overridden use visudo and edit /etc/sudoers
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Check that you have execute permission on the script. i.e. chmod +x foo.sh
Check that the first line of that script is #!/bin/sh or some such.
For sudo you are in the wrong directory. check with sudo pwd
You can also create a soft link to your script in one of the directories (/usr/local/bin for example) in the super user PATH. It'll then be available to the sudo.
chmod +x foo.sh
sudo ln -s path-to-foo.sh /usr/local/bin/foo
Have a look at this answer to have an idea of which directory to put soft link in.
It seems that linux will say "command not found" even if you explicitly give the path to the file.
[veeam#jsandbox ~]$ sudo /tmp/uid.sh;echo $?
sudo: /tmp/uid.sh: command not found
1
[veeam#jsandbox ~]$ chmod +x /tmp/uid.sh
[veeam#jsandbox ~]$ sudo /tmp/uid.sh;echo $?
0
It's a somewhat misleading error, however it's probably technically correct. A file is not a command until its executable, and so cannot be found.
Try chmod u+x foo.sh instead of chmod +x foo.sh if you have trouble with the guides above. This worked for me when the other solutions did not.
Regarding "command not found" when using sudo far less hackier way would be to edit secure_path.
It is perfectly described here:
https://superuser.com/questions/927512/how-to-set-path-for-sudo-commands
Ok this is my solution:
in ~/.bash_aliases just add the following:
# ADDS MY PATH WHEN SET AS ROOT
if [ $(id -u) = "0" ]; then
export PATH=$PATH:/home/your_user/bin
fi
Voila!
Now you can execute your own scripts with sudo or set as ROOT without having to do an export PATH=$PATH:/home/your_user/bin everytime.
Notice that I need to be explicit when adding my PATH since HOME for superuser is /root
If you are not so comfortable with the command line and are using Ubuntu you can solve the problem as follows:
Open the folder window where the file is located
Right click on the executable file and choose Properties
Go to the Permissions tab and highlight Allow executing file as program
With this solution you allow the user to execute the file as a program and you don't need sudo (or change the PATH environment variable for root).
It seems sudo command not found
to check whether the sudo package is installed on your system, type sudo , and press Enter . If you have sudo installed the system will display a short help message, otherwise you will see something like sudo: command not found
To install sudo, run one of the following commands using root account:
apt-get install sudo # If your system based on apt package manager
yum install sudo # If your system based on yum package manager
I am new to Linux system and there seem to be too many Java folders.
java -version gives me:
java version "1.7.0_55"
OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
When I am trying to build a Maven project , I am getting error:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java
Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?
find /usr/lib/jvm/java-1.x.x-openjdk
vim /etc/profile
Prepend sudo if logged in as not-privileged user, ie. sudo vim
Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.
You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/java/jdk1.7.0_05/
You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn't change with new Java versions.
None of the other answers were "sticking" for me in RHEL 7, even setting JAVA_HOME and PATH directly in /etc/profile or ~/.bash_profile would not work. Each time I tried to check if JAVA_HOME was set, it would come up blank:
$ echo $JAVA_HOME
(<-- no output)
What I had to do was set up a script in /etc/profile.d/jdk_home.sh:
#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH
I initially neglected the first line (the #!/bin/sh), and it won't work without it.
Now it's working:
$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/
Open terminal and type sudo gedit .bashrc
It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Then save the file and exit from file
Above is for a single user. For all users, you have to follow below steps
gedit /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Copy the bin file path you installed
YOUR PATH
open terminal and edit environment file by typing following command,
sudo nano /etc/environment
In this file, add the following line (replacing YOUR_PATH by the just copied path):
JAVA_HOME="YOUR_PATH"
That should be enough to set the environment variable. Now reload this file:
source /etc/environment
now test it by executing:
echo $JAVA_HOME
Doing what Oracle does (as a former Sun Employee I can't get used to that one)
ln -s latestJavaRelease /usr/java/default
Where latestJavaRelease is the version that you want to use
then export JAVA_HOME=/usr/java/default
The answer is given previous posts is valid. But not one answer is complete with respect to:
Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in
/etc/profile.d/ to make custom changes to your environment, as this
will prevent the need for merging in future updates.*
So as stated above create /etc/profile.d/custom.sh file for custom changes.
Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:
#if making jdk as java home
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
OR
#if making jre as java home
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
And remember to have #! /bin/bash on the custom.sh file
First you need to find out which Java is installed in your PC and which one to use.
For that open terminal with root permission.
sudo su
ls /usr/lib/jvm/
Now it will list the available java versions.
Select the listed version.
Copy the path till there.
Now open bashrc
nano ~/.bashrc
add the following commands to the end
export JAVA_HOME="path that you copied"
export PATH=$JAVA_HOME/bin:$PATH
after that save the file and exit by pressing Ctrl+S followed by Ctrl+X
Now run the below command:
source ~/.bashrc
1...Using the short cut Ctlr + Alt + T to open terminal
2...Execute the below command:
echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null
3...(Recommended) Restart your VM / computer. You can use source /etc/source if don't want to restart computer
4...Using the short cut Ctlr + Alt + T to open terminal
5...Verified JAVA_HOME installment with
echo $JAVA_HOME
One-liner copy from flob, credit to them
This is a very simple script to solve the problem
export JAVA_HOME_BIN=`which java`
export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN`
export JAVA_HOME=`dirname $JAVA_HOME_DIR`
And for testing:
echo $JAVA_HOME
Posting as answer, as I don't have the privilege to comment.
Point to note: follow the accepted answer posted by "That Dave Guy".
After setting the variables, make sure you set the appropriate permissions to the java directory where it's installed.
chmod -R 755 /usr/java
All operational steps(finding java, parent dir, editing file,...) one solution
zFileProfile="/etc/profile"
zJavaHomePath=$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
echo $zJavaHomePath
echo "export JAVA_HOME=\"${zJavaHomePath}\"" >> $zFileProfile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> $zFileProfile
Result:
# tail -2 $zFileProfile
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
export PATH=$PATH:$JAVA_HOME/bin
Explanation:
1) Let's break the full command into pieces
$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
2) Find java path from java command
# $(which java)
"/usr/bin/java"
3) Get relative path from symbolic path
# readlink -ze /usr/bin/java
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java"
4) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java
# readlink -ze /usr/bin/java | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin"
5) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/
# readlink -ze /usr/bin/java | xargs -0 dirname | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
Step 1 - check the current java version by "echo $JAVA_HOME"
Step 2 - vim /etc/profile
Step 3 - At the end of file you will find
export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.
Step 4 - Save and exit :wq
Step 5 - "source /etc/profile/", this would execute the change
Step 6 - Again do a echo $JAVA_HOME - change would have been reflected.
Probably a good idea to source whatever profile you edit to save having to use a fresh login.
either:
source /etc/
or
. /etc/
Where is whatever profile you edited.
On Linux I add this line to my ~/.profile:
export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)
While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:
1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.
2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.
3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.
Read more: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH
Use SDKMAN sdkman.io to switch btw. your sdk's.
It sets the JAVA_HOME for you.
open kafka-run-class.sh with sudo to write
you can find kafka-run-class.sh in your kafka folder : kafka/bin/kafka-run-class.sh
check for these lines
Modify the JAVA variable in the else part to point to the java executable in your java/bin. like JAVA="$JAVA_HOME/java"
In /etc/profile , if you open that will you’ll get to know that IT IS no recommended to write on that file. Instead of that make a script of your commands(suppose test.sh)go to /etc/profile.d folder and Put test.sh there. Every time you instance reboot it’ll be automatically called by /etc/profile.
Using vim might be a bit difficult for new user. We can use gedit text editor instead.
Find /usr/lib/jvm/java-1.x.x-openjdk
Enter "gedit /etc/profile" or use "sudo gedit /etc/profile" if logged in as not-privileged
Add the following at the end of line:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
Enter "source /etc/profile" in your current shell to apply the changes
I use the line:
export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../)
to my ~/.profile so it uses the base of the default java directory at login time. This is for bash.
Try this if doesn't work:
apt install openjdk-8-jdk-headless
First I ran these commands in my terminal to manually install ant.
# Let's get into your downloads folder.
tar -xvzf apache-ant-1.9.4-bin.tar.gz # Extract the folder
sudo mkdir -p /usr/local # Ensure that /usr/local exists
sudo cp -rf apache-ant-1.9.4-bin /usr/local/apache-ant # Copy it into /usr/local
# Add the new version of Ant to current terminal session
export PATH=/usr/local/apache-ant/bin:"$PATH"
# Add the new version of Ant to future terminal sessions
echo 'export PATH=/usr/local/apache-ant/bin:"$PATH"' >> ~/.profile
# Demonstrate new version of ant
ant --version
When I ran the last command, it showed an error. I then used brew to install ant which worked. Can I remove the first set of commands somehow? If so how? or does it not matter?
You need to reload the shell you are using, so .profile will get executed.
If you really want to use the same shell, you can run manually:
source ~/.profile
I know how to install java on linux machine using terminal. But i want to automate the installation using Chef Framework. I have two machines M1 and M2. I am on machine M1 and want to install java on machine M2. This is what i do in using terminal....
first i SSH into machine M2, after i do the following things... (in here i download the java files from third party storage.)
Step1:-
cd setup
step2:-
wget http://downloads.company.com/downloads/DevTools/jdk/6.0/jdk-6u31-linux-i586.bin
Step3:-
chmod +x jdk-6u31-linux-i586.bin
Step4:-
yes | ./jdk-6u31-linux-i586.bin
Step5:-
cd /usr/bin
Step6:-
mv java javaorg
Step7:-
cd /usr
Step8:-
ln -s /home/harish/setup/jdk1.6.0_31 java
Step9:-
echo 'export PATH=$PATH:/usr/java/bin' > /etc/profile.d/alljava.sh;echo 'export JAVA_BINDIR=/usr/java/bin' >> /etc/profile.d/alljava.sh;echo 'export JAVA_ROOT=/usr/java' >> /etc/profile.d/alljava.sh;echo 'export JAVA_HOME=/usr/java' >> /etc/profile.d/alljava.sh;echo 'export JRE_HOME=/usr/java/jre' >> /etc/profile.d/alljava.sh
Step10:-
source /etc/profile.d/alljava.sh
Step11:-
echo $JAVA_HOME
Step12:-
java -version
I have following questions:
what is the category of the problem (chef-solo or shef-server)
do i need to have machine M2 installed Chef.
i have downloaded the cookbook for JAVA from GitHub but i don't know what to do with that.
i have written some code for installing java on SAME machine.. but it also not working..
CODE:
#cookbook/java/recipe/default.rb
*####This will install JAVA on machine M1######*
execute "copy" do
cwd "/home/user/setup"
command "wget http://downloads.company.com/downloads/DevTools/jdk/6.0/jdk-6u31-linux-i586.bin"
end
execute "change_mode" do
command "chmod +x jdk-6u31-linux-i586.bin"
end
execute "dont_know" do
command "yes | ./jdk-6u31-linux-i586.bin"
end
execute "make link" do
command "ln -s /home/harish/setup/jdk1.6.0_31 java"
end
java_home = "export JAVA_HOME=/usr/java/jdk1.6.0_31"
path = "export PATH=$PATH:JAVA_HOME/bin"
file "/etc/profile" do
content "#{java_home}\n#{path}"
owner "root"
end
but this code is giving some big messages..(errors).
can anybody help through this and also how to use the downloaded cookbooks for JAVA.. thanks. :)
This is a long out-of-date question, but I'll include an answer in case someone stumbles in on this one.
Opscode has a Java cookbook that they maintain that can install either OpenJDK or Oracle JDK. It's available at the opscode community site here.
You can use it by adding a dependency to your cookbook's metadata.rb:
depends "java", "~> 1.10.2"
and including the recipe in your cookbook's default.rb:
include_recipe "java"