Category Archives: IT

Proxmox rotating network interfaces

It sometimes happens that when I shutdown Proxmox for some hours, that after restart, the network interface does not work. I figured that Proxmox is rotating the network interface numbers which leads to the Linux network bridges to not map to the correct network interface and thus leads to no network connectivity. The only thing that helps is to adapt the /etc/network/interfaces file.

Some commands that can help:

brctl show
ifconfig vmbr0
dmesg
lspci | grep -i ethernet

Increase size of disk partition in Ubuntu

If you try to upgrade your system and you lack disk space, here is a solution that might help.

Run “df -h” to check how much space your system is using.

df -h

If it is at 100%, check if the partition still has some spare space left to be used.

lsblk

If the size of the partition your Ubuntu system is living on (f.ex. sda3) is bigger thant the allocated space for “ubuntu–vg-ubuntu–lv”, then you can add space to the system partition.

Result of lsblk (15GB remaining to be added)

You need to extend the logical volume to the maximum free space available:

sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

Then you need to expand the filesystem:

sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

Check with df -h ig the additional space has been allocated.

Set up SSH keys on Ubuntu 24.04

Login to your Ubuntu client with the username for which you want to create a key pair. Create the key with the following command:

ssh-keygen

The keys will be saved in your home directory under: /.ssh/id_rsa

Now copy your public key to the server that you want to connect to crom your client without using a password:

ssh-copy-id myusername@remote_host

That’s it. Now try to connect to your server with that username. If all works fine, no password will be asked for.

For a more comprehensive tutorial with more explanations, please consult this excellent page.

Install and configure AWSTATS

General configuration

Using awstats to analyse your web usage statistics if your Apache webserver is running behind an Nginx reverse proxy server, needs some special configuration steps.

Install awstats on your system:

sudo apt install awstats

Enable the awstats apache configuration (and cgi if it is not enabled yet):

sudo a2enmod cgi
sudo a2enconf awstats

Restart the apache service:

sudo systemctl restart apache2

Edit the configuration awstats configuration file or create a configuration file for each virtual host. We are using combined log formats and need to configure this.

sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.mysite.com.conf 
sudo nano /etc/awstats/awstats.www.mysite.com.conf

Configure the virtual host log file path in your awstats configuration file. Make sure you select the correct one (might be the SSL log file).

LogFile="/var/log/apache2/www.mysite.com.access_log"
LogFormat=1

Configure the domain name and domain aliases:

SiteDomain="www.mysite.com"
HostAliases="mysite.com XYZ.mysite.com"

Optionally, if you want DNS lookup (conversing of IP’s into hostnames):

DNSLookup=1

Now update the awstats statistics folder by running the following command:

sudo /usr/lib/cgi-bin/awstats.pl -config=www.mysite.com -update 

Go to your awstats page:

https://www.mysite.com/cgi-bin/awstats.pl?config=www.mysite.com

Depending on your configuration, cgi-bin might need to be replaced by awstats.

Note on security: Make sure that your awstats statistics page is not visible from outside, even more if you are allowing your statistics to be updated through the web interface. I am forcing an htaccess realm through the Nginx reverse proxy on these directory.

If you want to update your statistics through the web interface, set the corresponding flag.

AllowToUpdateStatsFromBrowser=1

The following files have to be readable or writable by the apache service (www-data) user:

/var/lib/awstats (read & write)
/var/log/apache2/www.mysite.com.access_log (read)

Alternatively, add a crontab (edit /etc/crontab) entry:

* */15 * * * root /usr/lib/cgi-bin/awstats.pl -config=www.mysite.com -update > /dev/null

Configuration when running your web server behind an NGINX reverse proxy

Configure your reverse proxy correctly

In the NGINX reverse proxy configuration for your website, make sure you send the real IP-address of the client requesting your page through the header information to the web server. Add the following lines to the root location of your NGINX reverse proxy configuration for your web site (sudo nano /etc/nginx/sites-available/www.mysite.com). For more information on this, check here.

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;

Configure your web server to write the correct IP of the client into the log file (instead of the reverse proxy IP). Add the following to your apache virtual host file (sudo nano /etc/apache2/sites-available/www.mysite.com.conf):

RemoteIPHeader X-Real-IP
RemoteIPInternalProxy IP.PROXY.SERVER
RemoteIPTrustedProxy IP.PROXY.SERVER
RemoteIPHeader X-Forwarded-For
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

%{X-Forwarded-For}i replaces the first parameter in the combined log format, which normally is %h or %a. So if the LogFormat is set in your configuration file, make sure it has the above format.

Apache needs the remoteip module to be enabled in order to treat this information:

sudo a2enmod remoteip

To load the following module in the main Apache configuration file (sudo nano /etc/apache2/apache2.conf), delete the hash sign (#) before the following line:

LoadModule remoteip_module modules/mod_remoteip.so

Test the configuration and reload the apache service.

apache2ctl configtest
service apache2 restart

If you believe you have header issues and don’t know if the bug is on the level of the reverse proxy or the web server, check what header information is arriving at the web server.

Display the received headers (from reverse proxy) in html file

If you are running a reverse proxy server in front of your webserver, it might sometimes be useful to see which header variables are reaching your webserver. To achieve this, create the following headers.php file in the root of your webserver documents folder:

<?php
echo "<h1>Received Headers</h1>";
$headers =  getallheaders();
foreach($headers as $key=>$val){
  echo $key . ': ' . $val . '<br>';
}
echo "======================"
//echo '<pre>'; print_r($_SERVER); echo '</pre>';

?>

openHAB gauge for car fuel tank status

I need a gauge style widget that shows the percentage of fuel left in the tank. The item delivers a number between 0 and 1 and I wanted to display a percentage number between 0 and 100. Here is the functioning code that modifies an item value in the valueText component.

component: oh-gauge-card
config:
  action: navigate
  actionPage: page:cars
  actionPageTransition: f7-dive
  borderColor: =(items.myCar_FuelLevel.state<0.26)?"orange":"red"
  item: myCar_FuelLevel
  max: 1
  min: 0
  title: My car
  type: semicircle
  valueText: =(items.myCar_FuelLevel.state*100) + " %"
  valueTextColor: white
slots: null

renders to:

It might be more interesting to show the remaining mileage in the text though:

component: oh-gauge-card
config:
  action: navigate
  actionPage: page:cars
  actionPageTransition: f7-dive
  borderColor: =(items.myCar_FuelLevel.state<0.26)?"orange":"red"
  item: myCar_FuelLevel
  max: 1
  min: 0
  title: My car
  type: semicircle
  valueText: =(items.MyCar_DistanceLeft.state)
  valueTextColor: white
slots: null

which renders to:

openHAB Command Line Interface commands

  backup [--full] [filename]   -- Stores the current configuration of openHAB.
  clean-cache                  -- Cleans the openHAB temporary folders.
  console                      -- Opens the openHAB console.
  info                         -- Displays distribution information.
  reset-ownership              -- Gives openHAB control of its own directories.
  restore [--textconfig] [--uiconfig] filename
                               -- Restores openHAB configuration from a backup.
  showlogs                     -- Displays the log messages of openHAB.
  start [--debug]              -- Starts openHAB in the terminal.
  status                       -- Checks to see if openHAB is running.
  stop                         -- Stops any running instance of openHAB.

Command a Philips Hue light from a KNX switch through openHAB

I am using openHAB as my main home automation server. I want to be able to switch on/off Philips Hue lights with the Gira KNX TS3 sensors installed in my rooms.

Basically, you need to create a group address, map it to the TS switch channel, create a virtual KNX device in openHAB and create two rules to link both objects (one rule for switching ON and one for switching OFF).

In the ETS software, create a group address (e.g. 1/9/9) and map it to the TS button’s channel that should allow switching the Hue light ON and OFF.

In openHAB, create a KNX device thing called Virtual KNX switch. Add a channel with type Switch Control and configure it to address 1/9/9. Map an item of type Contact to this channel (let’s name it KitchenVirtualSwitch).

Assuming that your Hue light is called HueLight, create a rule to switch the light ON:

configuration: {}
triggers:
  - id: "1"
    configuration:
      command: ON
      itemName: KitchenVirtualSwitch
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: "

        \          HueLight.sendCommand(ON)

        \          logInfo( \"openHAB.Rules\",\"KitchenVirtualSwitch TS => Hue ON\"
        )

        \ "
    type: script.ScriptAction

The logInfo entry is optional, but very helpful when debugging. Let’s create a second rule for switching the light OFF:

configuration: {}
triggers:
  - id: "1"
    configuration:
      command: OFF
      itemName: KitchenVirtualSwitch
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: "

        \          HueLight.sendCommand(OFF)

        \          logInfo( \"openHAB.Rules\",\"KitchenVirtualSwitch TS => Hue OFF\"
        )

        \ "
    type: script.ScriptAction

Home Assistant Nginx Proxy configuration

Here is a working NGINX proxy configuration for Home Assistant:

server {

    server_name HOST.DOMAIN.NAME;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        satisfy any;
        allow 10.X.X.0/24;
        allow 10.X.X.0/24;
        deny all;
        auth_basic              "Restricted access";
        auth_basic_user_file    /etc/passwords/passwords;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_headers_hash_max_size 512;
        proxy_headers_hash_bucket_size 128;
        proxy_pass http://INTERNAL_HOME_ASSISTANT_IP:8123;
        include proxy_params;
     }


    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/HOST.DOMAIN.NAME/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/HOST.DOMAIN.NAME/privkey.pem; # managed by Certbot
}

server {
    if ($host = HOST.DOMAIN.NAME) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name HOST.DOMAIN.NAME;

}