Using remote SSH command to run code on other machines under Linux

If you want to run a command on machine B remotely from machine a, such as ls on B, you can use the following command:

[email protected]:~$ ssh [email protected] 'ls'

If you want to run multiple commands on machine B remotely from machine a, you can use semicolon to separate multiple commands, such as:

[email protected]:~$ ssh [email protected] 'cd test; ls'

Of course, you can also run the shell script on machine B. For example, if there is a script in the cyf user directory on machine B, you can:

[email protected]:~$ ssh [email protected] './'


[email protected]:~$ ssh [email protected] 'bash'

However, when running the Java program test on machine B remotely, there is a problem:

[email protected]:~$ ssh [email protected] 'java Test'


bash: java: Command not found

And direct ssh [email protected] After that, run java test again, and it will work normally:

If you run

[email protected]:~$ ssh [email protected] ‘env’

You will find that there are no Java environment variables

This is because SSH is a short link when executing the command and does not open the terminal, so it will not load the .bashrc environment variable, resulting in unrecognized Java commands

Solution: modify /etc/environment and add Java path to it


Execute the Java program test again, and you can find it works.



您的电子邮箱地址不会被公开。 必填项已用*标注