(probable) New way to SSH in to modify files in the underlying Home Assistant OS

I got frustrated enough to scratch an itch (editing files while HA is shut down), so here we go.

What this is

  • The ability to ssh in to your OS so that you can edit any file, regardless of whether HA is running

What this is not

  • Shell access to the underlying supervisor OS (for example, you can’t run ha <anything>)
    that access can be gained if you have access to plugging a USB stick in to the machine, but that’s inconvenient for me since I’m running it in a VM)
  • A thoroughly-detailed guide. I’m skipping over some details.

Why bother then?

  • Being able to copy/paste from my computer :chart_with_upwards_trend:
  • nano, jq, and any other file editor you want to use (the container has apk)
  • Editing and removing entities from the entity registry :expressionless:

Executive summary

Run an openssh server inside a docker container

How

I personally added and edited all these files using the VS Code addon, but you’re welcome to use any other method. Nice bonus is that this is this is portable.

Create the folder we’ll use

Folder name openssh-server

This is all done in the config folder, AKA CONFIG(the main window) in VS Code, /mnt/data/supervisor/homeassistant/ from the console, /config, or wherever else it’s stored. This is the folder with your configuration.yaml file.
Double note: if you want access to the console from the CLI, log in to the cli then type login. It (currently) opens an ash shell

Example command: mkdir openssh-server && cd openssh-server

Add the helper files to the folder

run.sh

#!/bin/sh

docker run -d \
  --name=openssh-server \
  --hostname=openssh-server `#optional` \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=<YOUR TIMEZONE> \
  -e USER_NAME="<YOUR USERNAME>" \
  -e PUBLIC_KEY="<YOUR PUBLIC KEY>" \
  -e SUDO_ACCESS=true `#optional` \
  -e PASSWORD_ACCESS=false `#optional` \
  -p 2222:2222 \
  -v /mnt/data/supervisor/homeassistant/openssh-server/config:/config \
  -v /mnt:/mnt \
  -v /mnt/data/supervisor:/hassio-supervisor-folder-mnt-data-supervisor \
  --restart unless-stopped \
  lscr.io/linuxserver/openssh-server

Note: you must specify a username other than root

end.sh

Used if you want to change the options in start.sh: change them, run end.sh, then start.sh

#!/bin/sh

echo "Stopping"
docker stop openssh-server

echo "'rm'ing"
docker rm openssh-server

start.sh

#!/bin/sh

docker start openssh-server

stop.sh

#!/bin/sh

docker stop openssh-server

Set it up

  • Log in to the HA console and cd to the folder
  • Make all the files executable
    • chmod -x *.sh

Get it running

./run.sh

You should see something like this:

Screen Shot 2022-01-20 at 11.39.14 AM

Success

You can now ssh -p 2222 from other machines directly in to the HA’s filesystem. Everything in /mnt/ is exposed in /mnt/ in the ssh server, you can use sudo to edit HA files, and you can access the config files in /mnt/data/supervisor/homeassistant/ (including the .storage folder that is hidden from the VS Code addon)

I’ll consider this post a win if it helps at least one person