<p>I am trying to run kubectl virt commands to manage my virtual machine via PHP. First, I log in to my server with phpseclib with the following code:</p>
<pre><code>$ssh = new SSH2('localhost');
if (!$ssh-&gt;login('root', 'rootPassword')) {
    throw new \Exception('Login failed');
}
</code></pre>
<p>This part works fine, and when I try to run <code>$ssh-&gt;exec('whoami &amp;&amp; echo $PATH')</code>, I get the following output:</p>
<pre><code>root
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
</code></pre>
<p>But, whenever I try to run <code>kubectl virt</code> via PHP, I get the following output:</p>
<pre><code>error: unknown command &quot;virt&quot; for &quot;kubectl&quot;
</code></pre>
<p><code>kubectl</code> and <code>kubectl virt</code> work perfectly fine when I run them via terminal but somehow do not work with PHP exec(). I also tried to check the <code>$PATH</code> via terminal and I get a different output:</p>
<pre><code>/root/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
</code></pre>
<p>I thought that it may be because of <code>$PATH</code> but the interesting part is when I try to run <code>sudo kubectl virt</code> via terminal I also get the same error:</p>
<pre><code>error: unknown command &quot;virt&quot; for &quot;kubectl&quot;
</code></pre>
<p>At that point, I am completely lost and don't even know where to look for a problem. I am thankful for all the answers.</p>

iristhennars
<p>When you are issuing ad-hoc ssh commands, you are not using interactive shell, and depending on your default shell behavior it may or may not load your .bashrc file . See <a href="https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command">https://serverfault.com/questions/936746/bashrc-is-not-sourced-on-ssh-command</a> and <a href="https://stackoverflow.com/questions/16546333/running-command-via-ssh-also-runs-bashrc/16547623#16547623">Running command via ssh also runs .bashrc?</a> for more details.</p>
<p>So by default, krew modifies your <code>PATH</code> variable, and appends it's bin path to it, i.e. my config contains <code>export PATH=&quot;${KREW_ROOT:-$HOME/.krew}/bin:$PATH&quot;</code>. But what exactly is kubectl plugin? Usually it's just a single binary, with kubectl-<em>plugin_name</em> name. So by invoking <code>which kubectl-virt</code> you can easily know where is your virt binary located and invoke it directly, so something like</p>
<pre><code>$ssh-&gt;exec('~/.krew/bin/kubectl-virt')
</code></pre>
<p>should work</p>
<p>The other way is to modify PATH all by yourself, setting <code>PATH=$PATH:~/.krew/bin</code> should make it work, at least in my case</p>
<pre><code>ssh localhost 'PATH=$PATH:~/.krew/bin kubectl virt'
</code></pre>
<p>worked nicely.</p>
<p>You can try to force loading of .bashrc in your shell configuration, but personally i think it's a bad practice, and ssh commands are not usually loading rc files for a reason, command execution speed and consistency between systems are the first things that come to mind.</p>
<p>Regarding sudo, it's actually not that surprising, because without <code>-E</code> or <code>-i</code> flags it won't load your current environment / won't start interactive shell. See <a href="https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc">https://unix.stackexchange.com/questions/228314/sudo-command-doesnt-source-root-bashrc</a> for more info</p>
