Setting up Home Assistant in a virtual environment on Intel NUC (or any other x86 machine)
Setting up Home Assistant in a virtual environment on Intel NUC (with mqtt, ssh, samba, docker…) based on Ubuntu Server 17.10 / Intel NUC7i3BNH
Originally for Taymurf (Taymurf#0781 on DiscordHA) Big thanks to him for beta testing and motivating me to do this!
V 0.1 (Jan 2018) Tested on HomeAssistant Release 0.62.1
V 0.2 (Jun 2018) Migrated to Ubuntu 18.04 LTS with dist upgrade
V 0.3 (Apr 2020) Migrated to Ubuntu 20.04.3 LTS with dist upgrade
The basic steps in this How-To reflect till today my current working setup. I’ve made minor changes to some services (e.g. migrated the Mosquitto Server to Docker). Ubuntu switched from SysV to systemd. If you did aswell, please notice that all commands starting with
/etc/init.d/SERVICE
are now (re)started/stopped viasystemctl (re)start/stop SERVICE
.
Main reason for writing this “How-to” is to give a helping hand to those willing to switch their working appliance to something more powerful. I recently moved from a Raspberry Pi 3 to an Intel NUC and these were the initial steps I took…
I changed some (imho) basic settings in the system to ensure a basic level of minimum security. I am not a Linux Guru nor a nationwide “Security Consultant” so any constructive criticism or advice on how I can improve this guide is greatly appreciated.
I mainly use “vim” for editing files. Feel free to use any other editor of your choice (i.e. nano).
The following wildcards (placeholders) will be used throughout the text:
EDITOR - Replace with whatever editing program you prefer to use.
USER - Replace with your main user account
HAUSER - Replace with your Home Assistant user account
Greetings to the whole community (especially the devs!) and thanks for making this stunning project so vital and awesome.
0.) Preamble
If anything isn’t working as it should, contact me on any of my mentioned channels. I try to respond as soon as possible!
If you need general help visit https://home-assistant.io/help/
PLEASE KEEP IN MIND:
THIS IS NOT AN OFFICIAL DOCUMENT MAINTAINED BY HOME ASSISTANT!
This is a written account of what has worked for me and tested by another user of
Home Assistant making the switch from a Raspberry Pi 3 to an Intel NUC.
1.) Before Install
Update to the latest BIOS Version and adjusting it Download the appropriate Version for your System from https://downloadcenter.intel.com/.
Detailed instructions how to update are found here: https://www.intel.com/content/www/us/en/support/articles/000005636/mini-pcs.html
Initial upgrade failed for me with any USB Stick larger 64GB. Basically, format any USB Stick with exFAT (respectively FAT32) and copy the *.bio file onto it.
Insert the Stick into a USB Port of your choice and shutdown the PC. Reboot into the BIOS Updater by pressing “F7” while starting up the NUC. Select the stick and file you copied earlier and wait for it to complete.
Reboot to the BIOS (Press F2) Changes I made to the BIOS include selecting the “Quiet” model for the fans and disabled any lighting for HDD activity. I prefer a quiet and stealthy device on my shelf.
2.) Installation
Download the Ubuntu Server (https://www.ubuntu.com/download/server)
Use Etcher or rufus (Etcher: https://etcher.io/ / rufus: http://rufus.akeo.ie/) on Windows to flash the image to an USB Stick of your choice.
Any Stick should work.
On Linux: Stick to: https://help.ubuntu.com/community/Installation/FromUSBStick On MacOS: Etcher is also available.
In general any *NIX based system with dd should work.
Connect a Keyboard, a monitor and an ethernet cable to your PC. Initial networking setup is done via DHCP, most routers will assign an IPv4/IPv6. If not, assign a static ip and gateway manually (If DHCP fails, the installer will tell you).
Insert the Stick and reboot, the Ubuntu Installer should come up (if not, enter the Boot Menu from the BIOS and select your stick). Select your prefered Language/Region and choose a username/password of your choice. Later we will set a strong password to this user. Main configuration and maintenance of the system is done by that user via sudo
.
By default, Ubuntu does not allow any direct logins with the “root” user (which makes sense imho). I decided NOT to setup an encrypted home container.
Please keep in mind that all information stored on the harddrive could easily be read by anyone with physical access to the system (I.e. if it gets stolen). Maybe I will add the Setup of LUKS/LVM in a future version or update this how-to if there is some public demand.
Check the determined Timezone and correct the Settings if necessary.
In my setup I select Guided - use entire disk and set up LVM for the server. LVM makes it easy to add some hard drive space in the future if needed. LVM means Logical Volume Manager and is more powerful than playing around with parted and mkfs.X Read more about LVM and why it is good here: https://wiki.ubuntu.com/Lvm
Wait for “apt” to finish. I’ve decided to let the system maintain it’s security updates automatically.
If you are the natural born sysadmin feel free to keep updating in your hand. Install OpenSSH Server and Samba in the next dialogue simply by marking it with an asterisk. SSH will handle our remote connection and Samba is used for editing the HASS config in a Windows share.
Install GRUB to the MBR if you don’t want to setup a multi-OS system (what i guess you don’t want to).
Remove the Stick, hit Continue and watch your server come to life! Login in with the user and password created earlier.
3.) Initial System Config
After logging in for the first time, use sudo -s
and enter your password to become the root user.
Remember this command, you will need it many times… First we will do some updates. Use apt-get update && apt-get full-upgrade -y
to update all packages installed at this point.
sudo:
As seen before working as the root user is vital.
Later, we will change the password for the user to something more complex and secure. The Login to System with your default user will later be strengthened with an openssh key login. I decided to give this user the permission to use “sudo” without typing the password all the time.
PLEASE KEEP IN MIND: Everybody with your password (or ssh key) is able to modify, read or delete anything on that system!
type cd /etc/sudoers.d && EDITOR 010_USER-nopasswd
to change the directory and create an empty config file (replace USER with your own user).
BE VERY CAREFUL ON THIS STEP: if you enter anything wrong in this file and save it (giving you a syntax error) you will most likely have to reinstall your server. It only takes about 10 minutes to do over, but it’s a hassle.
It is possible to regain access with any “Linux Live Media” but that would go beyond the scope of the document.
Enter USER ALL=(ALL) NOPASSWD: ALL
and save the file and after that sudo -k
for applying the changes. Next time any use of the sudo command won’t prompt you for a password. If you want to, logout with logout
(or Ctrl + D ) an re-login. The command sudo whoami
should output “root
” without any password requested.
Sample output:
|
|
Setting up a static IP for the system:
I don’t want to rely on any DHCP Daemon for assigning a IP to my HASS server. If for whatever reason your DHCP server fails or the static route in your router is lost you won’t be able to connect to it. Type ip link show
to find out the device name of your ethernet card.
|
|
In my case the network device is named “eno1”. Yours may differ. My server will listen to the Address “192.168.0.10” and use the Google DNS Servers (8.8.8.8,8.8.4.4).
IMPORTANT! Change those to whatever you want but KEEP IN MIND this address has be unique and should not appear in your DHCP daemons ip pool. Usually every DHCP Daemon reservers a pool for assigning IP Addresses. In my case I configured the DHCP service with a static address 192.168.0.10 for HomeAssistant and a pool range from 192.168.0.200 to 192.168.0.254.
If unsure about your net settings use ifconfig
to display the actual settings of your network card and ip route |grep default
for the gateway address Sample for my network:
|
|
In previous Ubuntu Version static interfaces were configured in “/etc/network/interfaces”. This changed in Ubuntu 17.10. Use “cd /etc/netplan
” to change to config directory as root (sudo -s
if you are not already root).
*NOTE: .yaml files are very sensitive to indenting properly so make sure you follow the spacing of the file and don’t just copy and paste what I have here.
For an example of what it should look like with proper indents,use wget -O 01-netcfg.yaml https://chris.0x711.de/01-netcfg.yaml
to store a local copy of my config.
Use EDITOR 01-netcfg.yaml
to alter the configuration. Change to the following (altered to match your config) and save:
|
|
use netplan apply
to execute the changes.
Check if everything works: type ip addr|grep inet
the output should be something like:
|
|
use ping -c 5 home-assistant.io
to test the internet connectivity:
|
|
The ping command send 5 packets to the Home Assistant homepage. You could use any domain for this test (google.com, nasa.gov, xkcd.com….)
More information about netplan is found here: https://wiki.ubuntu.com/Netplan
Time for the first reboot of your machine using sudo reboot
(Apply the kernel updates and see if the static ip setup works).
After rebooting you could remotely connect to your server. For doing that in my case (replace the IP with the one you’ve set up above):
Linux/MacOS: ssh -l USER 192.168.0.10
Windows: Use PuTTY and enter in hostname your address.
Congratulations!
You’ve just set up a Ubuntu server with a static IP-Address on your NUC!
4.) Setting up sshd and login with key
Login to your box and become the root user with sudo -s
.
Use apt-get install -y pwgen
pwgen is a handy tool for generating passwords.
Use pwgen 40
to generate some random passwords with the length of 40 characters. Default settings use a-z, 0-9 and some capitalized characters. If you want to you could mix in some special characters. See man pwgen.
For those of you how want to learn more about security and complexity of passwords start with this (very popular) comic: https://xkcd.com/936/
|
|
Time to change the installer password (I had chosen password (ultra secure)) to something more complex.
Choose one from your list (NOT MINE!) and type passwd USER
.
After submitting that command, copy and paste a password of your choice and hit Enter.
Ensure you did it right with a new ssh/Putty session and login with your new password.
WARNING: Note down the password in a safe place! You won’t be able to login to your server with the old one anymore!
For Windows I use Keepass (https://keepass.info/).
A Beginner’s guide can be found here.
Continue as USER (if you are still root enter exit or press Ctrl + D).
Use mkdir .ssh && cd .ssh
to create a hidden directory and change to it.
Create a ssh key with ssh-keygen -t rsa -b 4096 -C USER2nuc
and save it to USER2nuc (or any other distinct name of your choice).
You could leave the passphrase password empty. Again it is your personal choice and it would without a doubt be more secure to add one.
My server is only in my local network and I store all of my keys on an encrypted drive, so I left it blank for convenience of logging in.
If you ever going to expose the machine (or to be more precise the ssh port) to the open internet do yourself a favor and increase the security with this simple two factor mechanism. (OT: you could use the autotype function in keepass to launch a putty profile and autotype the password)
Next we add the keyfile to a file called authorized_keys.
To do so: cat USER2nuc.pub >> authorized_keys
Note: The command won’t generate any output
|
|
Use cat USER2nuc
to display the private key and copy & paste it to some secure space.
If you want to connect from a linux shell to the nuc (assumed you saved the key to /path/to/USER2nuc-keyfile and the remote ip is ..0.10) use this command: chmod 600 /path/to/keyfile (makes the file only readable by the owner) and after that ssh -l USER -i path/to/USER2nuc-keyfile
On Windows download and open putty keygen
Use File → Load private key and select the private key you saved before. Click on Save private key and choose again a safe location. Putty keygen will create a .ppk file which is needed.
Next step will be editing the openssh daemon config file: Use sudo -s
to become root, change the directory and edit the config with cd /etc/ssh && EDITOR sshd_config
Changes I made (remove the leading # to activate the options):
|
|
Add the DenyUsers line at the bottom of the document. Save the file and exit the EDITOR.
I changed the port to something not typically sshd-associated. Many bots try to bruteforce into poorly administered remote machines.
Changing the port is filed under Security by Obscurity. Login for the user root is denied in general.
Even if someone exploits your machine and changes the root password the intruder can’t login using ssh. We deny the user homeassistant access to the system.
In a later section we will add this user to run HASS. One more discussable option for the config would be PasswordAuthentication no.
If you decide to do so please remember:
If you lose your openssh/.ppk keyfile you are locked out of the system!
Use /etc/init.d/ssh restart
to load the new settings. Open Putty. Enter the IP Address and your new port.
Enter a session name like HA-nuc in the lower half of Putty window. Click on the left side on Data under Connection and fill in USER under Auto-login username.
Click on Auth under Connection SSH and supply the path to your private keyfile.ppk created earlier. Go back to Session and hit Save on the lower right side.
A double-click onto the profile name will now connect and auto login you to your server!
5.) Setting up the HomeAssistant User and creating a Samba share
Become root with sudo -s
and type adduser HAUSER
Enter a password of your choice. I used homeassistant for both HAUSER and the password because remote login to the machine via ssh is denied.
More secure in a general way would be setting up a strong password (see 4.)).
All fields for name etc. are not needed. Just hit enter.
|
|
To add the HAUSER to more system groups use adduser HAUSER GROUPNAME
In my case i want to use a Z-Wave stick and the HAUSER needs to be a member of the group dialout.
If any special Home Assistant component will need a special group it is normally documented in the official docs.
I assume samba was installed in the initial setup process if not run apt-get install samba -y
as root.
WARNING: I’ve decided to create a public share. Everybody in your home network could read and modify the files inside the share!
If you don’t want a public share stick to this: Ubuntu | Creating Samba from the console
It might be also a good idea to to disable the autostart of samba and start it only up if you need to modify the configuration of HomeAssistant:
To do so enter systemctl disable smbd
as root. systemctl start smbd
is used via ssh to start the service and systemctl stop smbd
to stop it.
Change the Samba config directory and edit the config file with cd /etc/samba && EDITOR smb.conf
I added the following at the end of the file:
|
|
Create the folders with mkdir /home/USER/winshare
and mkdir -p /home/HAUSER/.homeassistant
We need to set the correct file permissions to both folders:
Execute that with chown -R USER:USER /home/USER
and chown -R HAUSER:HAUSER /home/HAUSER
Restart samba with service smbd restart
.
Test your shares from Windows: Press Windowsbutton + R and enter 192.168.X.Y (the static ip of your server).
Two folders should appear and you should be able to create or delete files.
6.) Installing the Virtual Environment and (finally) HomeAssistant
Detailed information is found at: https://home-assistant.io/docs/installation/virtualenv/
Start with sudo apt-get install python3-pip python3-venv -y
Do all of the following commands as the HAUSER.
Use sudo su -s /bin/bash HAUSER
to work as HAUSER Change directory with cd /home
and run python3 -m venv HAUSER
cd HAUSER && source bin/activate
actives the virtual environment.
Run python3 -m pip install wheel
followed by python3 -m pip install homeassistant
For a first test run hass --open-ui
HASS is preparing the first start and you should see a lot of installing in the log. Give it some time.
Check http://your-ip:8123 and hopefully there it is! =)
You should find inside the HA Windows samba share some newly generated files (e.g. the configuration.yaml)
Hit Ctrl + C once inside the terminal window to stop the process and continue with setting up autostart via systemd. Enter exit
to return to your normal user and type sudo -s
to become root.
Change to cd /etc/systemd/system
and run EDITOR home-assistant@HAUSER.service
*NOTE: File will be blank. Copy over the following and save the file:
|
|
Reboot your Machine and wait for HA to come up under http://
Congratulations! You’ve just setup your own Ubuntu Server with HASS!
7.) Updating and managment of the service
dale3h made a great command line tool for controlling and managing HomeAssistant
See https://github.com/dale3h/hassctl for more information.
Install it with: sudo curl -o /usr/local/bin/hassctl https://raw.githubusercontent.com/dale3h/hassctl/master/hassctl && sudo chmod +x /usr/local/bin/hassctl
Run hassctl log
once to auto-generate the config files. The program will terminate with an error but we fix that in the next step. Use sudo EDITOR /etc/hassctl.conf
and change the paths:
VIRTUAL_ENV= to /home/HAUSER HASS_CONFIG=/home/HAUSER/.homeassistant HASS_USER=HAUSER
Example Output:
|
|
Now test it with hassctl restart && hassctl log
.
You should see Home Assistant rebooting. To stop the process use Ctrl + C.
As a backup (if hassctl stops working for whatever reason) you could manually start, stop or update HomeAssistant with those commands:
Stop: sudo systemctl stop home-assistant@HAUSER.service
Start: sudo systemctl start home-assistant@HAUSER.service
Logfile: sudo journalctl -fu home-assistant@HAUSER.service
Update: First stop the service (see above) then become the HAUSER: sudo su -s /bin/bash HAUSER
:
Change to the homedir and activate the VENV: cd /home/HAUSER/ && source bin/activate
Upgrade HASS with: python3 -m pip install --upgrade homeassistant
Use exit
to leave the virtual environment and use the start command.
8.) Other useful services and helpful hints
Services I would personally advise are mosquitto as a mqtt broker and docker-io.
Mosquitto
Mqtt offers a widespread and reliable way for communicating with plenty of devices.
See the official component page for further information: https://home-assistant.io/components/mqtt/
Become root with sudo -s
. Use apt-get install mosquitto mosquitto-clients -y
to install the server and useful tools.
Stop the service with service mosquitto stop
Change to the configuration directory with cd /etc/mosquitto
The configuration file (mosquitto.conf) is mainly self explaining. If you are lazy grab a copy of my config with wget -O mosquitto.conf https://chris.0x711.de/mosquitto.conf
Use mosquitto_passwd -c /etc/mosquitto/pwfile MQTTUSERNAME
to create a user called MQTTUSERNAME with a corresponding password.
Restart the service with service mosquitto restart
Subscribe to your server with mosquitto_sub -h <your-ip> -p 1883 -t '#' -u MQTTUSERNAME -P MQTT_PASSWORD -v -R
Send a test message from another shell (or Putty session) with: mosquitto_pub -h <mosquitto-server-ip> -p 1883 -u MQTTUSERNAME -P MQTT_PASSWORD -t test/hello -m world
The output of the stream should display
|
|
Your server is ready to use! For adding another user use mosquitto_passwd /etc/mosquitto/pwfile ANOTHERUSERNAME
and restart the service.
*Note: You could use the same user on multiple devices but please keep in mind that the client-id has to be different.
Docker
I mainly use docker to generate some neat looking statistics for my sensor data. To install docker use the command apt-get install docker.io -y
as root.
Phil Hawthorne made some great articles on how to and why using docker in combination with Home Assistant is a great idea.
See his blog for more information: https://philhawthorne.com/getting-started-with-grafana-influxdb-for-home-assistant/
Thank you for reading and if this document has helped you in any way let me know it if it was worth the effort and time ;)