Linux command via HA

Hello,

I have read and researched the various post on this forum related to that topic without much success.

My scenario: I have a server PC on Proxmox running two VMs: Ubuntu server and HA. I also have a NAS which is mounted on the Ubuntu where I run Plex reading media files on the NAS via the mount. I noticed constant CPU load on the NAS so I wanted a button in HA where I can mount only when I am going to use Plex or else unmount it.

working linux command:
Mounting: sudo mount -a
Unmounting: sudo umount -a -t cifs -l

Steps followed:

  1. Install SSH & Web Terminal Addon

  2. generated keygen
    ssh-keygen -t rsa -b 4096

  3. copying keys for ubuntu server
    ssh-copy-id [email protected]

  4. copying keys to config folder to avoid override during HA update
    cp /root/.ssh/id_rsa /config

  5. Created the HA button:

- platform: command_line
  switches:
    mount_media_nas:
      command_on: "ssh -i /config/ssh_keys/ubntsrv -o 'StrictHostKeyChecking=no' [email protected] sudo mount -a'"
      command_off: "ssh -i /config/ssh_keys/ubntsrv -o 'StrictHostKeyChecking=no' [email protected] sudo umount -a -t cifs -l'"

I get the following error:

Logger: homeassistant.components.command_line.switch
Source: components/command_line/switch.py:109
Integration: command_line (documentation, issues)
First occurred: 11:04:00 AM (1 occurrences)
Last logged: 11:04:00 AM

Command failed: ssh -i /config/ssh_keys/ubntsrv -o 'StrictHostKeyChecking=no' [email protected] sudo mount -a'

My conclusion is the ssh keys are not being found in the right place? Any help please.

At first sight, the key is actually in /config, not /config/ssh_keys

But if just an error in the post, first try:

ssh -i /config/ubntsrv -o 'StrictHostKeyChecking=no' [email protected] 

from the HA terminal (after login to HA) to check whether the ssh part is working.

If so, inside the remote shell, do the

sudo mount -a

to test

corrected to /config. I checked on the ubuntu server and the keys generated from HA has been added to authorized keys. Any ideas?

Well, which step of my suggested procedure is not working?

EDIT: I also assume you renamed id_rsa to ubntsrv, ofc

Here is the output:

image

So, you have to:

  1. Actually put your keys in a subdirectory of /config, e.g. /config/ssh_keys
  2. Do a
chmod -R 600 /config/ssh_keys
1 Like

using below command works via SSH terminal:

ssh -i /config/ssh_keys/id_rsa -o 'StrictHostKeyChecking=no' [email protected] ls /hdd/media

However mounting is an issue because of the sudo. I tried the sudo -S but it ask for a password then.

2 choices:

  1. Add the public key to the root user authorized_keys and do a “shh root@…”, then you can drop the “sudo”
  2. Adapt the /etc/sudoers to allow ubntsrv to run “sudo mount” without password (NOPASSWD). See, e.g.
ubntsrv ALL=(ALL)    NOPASSWD: /bin/mount, /bin/umount