Category Archives: Ubuntu

i3wm brightness control on t480s

I got a new laptop – Thinkpad t480s – it is a great machine!
But since I like to use i3wm for my work, there was a bit of a problem with the way I control the brightness on my old machine. I had two scripts which were triggering when I press the function keys which were detected as XF86MonBrightnessDown and XF86MonBrightnessUp

The problem is that this newer machine was not detecting these keys with xev and when I was using the function keys for the screen brightness acpi events were triggered, so I needed to find how to catch the key presses.

So bellow is what I used in order to control my brightness.
First of all you need to find out what keys acpi detects, you can do that with acpi_list.
In my case the output of it was the keys video/brightnessup BRTUP 00000086 00000000
and video/brightnessdown BRTDN 00000087 00000000

So the next step is to add new acpi event. This is very straightforward, for ubuntu you just go to the /etc/acpi/events/ folder and create two files there – one for increasing the brightness and one for decreasing it. What you put in these files is simply what command or script to be executed when that key is pressed. I will give example with just the brightness increase scripts, as the brightness decrease are basically the same, just the key and script is different, so you should not have any problems adding the others.

So my /etc/acpi/events/brightness_up looks like this

# /etc/acpi/events/brightness_up
# This is called when the user presses the brightness up button and calls
# /home/ivan/.config/i3/brightness_up.sh which will increase the screen brightness.
# Author: Ivan Denkov

event=video/brightnessup BRTUP 00000086 00000000
action=/etc/acpi/brightness_up.sh

This trigger the script located in /etc/acpi/brightness_up.sh you need to make that script executable, and this are the contents to go there. Here is the script:

#!/bin/bash
CAT="$(which cat)"
current_brightness=`$CAT /sys/class/backlight/intel_backlight/brightness`
new_brightness=$(($current_brightness+20))
echo $new_brightness > /sys/class/backlight/intel_backlight/brightness

You just need to replicate the event and the script for lowering the brightness and you should be good to go.
If for some reason the above does not work, make sure that:
– You have made the bash scripts executable.
– Make sure the bash scripts by itself can change the brightness(It is actually working)
– Double check acpi_listen to make sure you have put the correct keys in the events.

MariaDB master-slave cluster on Ubuntu

This article explains how to run MariaDB SQL server in as master/slave replication cluster on two Ubuntu virtual machines.

master: 192.168.122.25
slave: 192.168.122.26

1. Before anything else you need to update all packages on the two machines:

sudo apt update
sudo apt upgrade

2. First thing is to add the official MariaDB repo for the stable release from here – https://downloads.mariadb.org/mariadb/repositories/
In my case, for Ubuntu 18.04 I had to use this:

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://ams2.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu bionic main'

3. Install it on both servers:

sudo apt install mariadb-server

You will have to provide password for the root user during install. Please note this is not the exisitng Ubuntu root user, but is new password the root user for mysql.

4. On both servers: sudo mysql_secure_installation
This will ask you for the root password you have set up in the previous step. You should remove anonymous users, disable remote root loginand remove test database. Basically answer yes[Y] to all if you are installing this on a machine available from the internet.

5. On both servers:

sudo systemctl enable mariadb.service
sudo systemctl start mariadb

The first command will make the mariadb server start every time the machine is re/started and the second will just the start service right now as it still not running.

6. On the master server create empty database

MariaDB [(none)]> mysql -uroot -p
MariaDB [(none)]> create database database_name;

7. On the master server we need to enable binary logging.
– Backup the original file in /etc/mysql/

cp my.cnf my.cnf.bkp

Add this new lines under the [mysqld] section, and replace the IP address with the one your master machine have.

#Replication settings
log-bin
server_id=1
bind-address=192.168.122.25
binlog-ignore-db = information_schema
binlog-ignore-db = mysql
binlog-ignore-db = performance_schema
binlog-ignore-db = test

This will replicate all new databases to the slave server, if you like to replicate just one specific database you should use

replicate-do-db = 

8. Now we need to login to the master sql server and create replication user and give the necessary grants.

MariaDB [(none)]> CREATE USER 'slave'@'localhost' IDENTIFIED BY 'SomePassword';
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO slave IDENTIFIED BY 'SomePassword' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
MariaDB [(none)]> SHOW MASTER STATUS;

The last command output is important in order the slave to know from which point it should start replicating from.

Unlock the databases and exit:

MariaDB [(none)]> UNLOCK TABLES;
MariaDB [(none)]> exit;

9. Login to the slave and create another empty database with the same name and the slave user.

CREATE DATABASE DATABASE_NAME;
CREATE USER 'slave'@'localhost' IDENTIFIED BY 'SomePassword';
FLUSH PRIVILEGES;

10. Add this the to the [mysqld] section in /etc/mysql/my.cnf in the slave:

server_id=2

Note that the master have server_id=1 so you should have different IDs on the different servers.

11. Log in to the slave database and run the following commands in the MariaDB prompt. Double check the MASTER_LOG_FILE and MASTER_LOG_POS variables, which should be the same as the values returned by SHOW MASTER STATUS above.

MariaDB [(none)]> CHANGE MASTER TO
MASTER_HOST='192.168.122.25',
MASTER_USER='slave',
MASTER_PASSWORD='SomePassword',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000001',
MASTER_LOG_POS=314,
MASTER_CONNECT_RETRY=10,
MASTER_USE_GTID=current_pos;

Now start the slave and check the status without exiting the MariaDB prompt:

MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;

12. Test the replication:
login in the master server and create table in our empty database:

CREATE TABLE IF NOT EXISTS names (
task_id INT AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
start_date DATE,
due_date DATE,
status TINYINT NOT NULL,
priority TINYINT NOT NULL,
description TEXT,
PRIMARY KEY (task_id)
) ENGINE=INNODB;

You should see the new table created on the slave server too.

13. Debug: If there is something wrong with the slave replication it should show with when you run

SHOW SLAVE STATUS\G;

Most of the time problems are easily resolved with updating the slave configuration with the

CHANGE MASTER

query, stopping and then starting the slave. Watch for log position and the log file name.

Remap print key to Super(windows) in i3wm

My laptop keyboard is little annoying – it have a Print Screen(PrtSc) button between my right control and alt keys – usually around that area you will find the windows(super) key, so I wanted to remap it, when i am using i3wm.

So first of all you need to make sure what is your key “called”, you can to that with the xev program.

Then you need to get your modifier map with: xmodmap -pm
In my case my output was this:

xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3      
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

I use mod4 for my i3 config, so I needed to add the Print key to the mod4 modifier with this command:

xmodmap -e "add mod4 = Print"

After that we see that Print is added to the mod4:

mod4        Print (0x6b),  Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf),  Print (0xda)

And you will probably want to add this command to your i3 config so it get excuted on each boot:

exec --no-startup-id /usr/bin/xmodmap -e "add mod4 = Print"

Mount directory into the RAM

Needed to mount WordPress cache folder into the RAM of one VPS, to get that little bit of extra speed.
To mount it temporary and see how it works for you, you can use:

mount -t tmpfs -o size=64M tmpfs /absolute/path/to/your/folder/

To make it permanent you need to add this in the /etc/fstab file:

tmpfs /absolute/path/to/your/folder tmpfs defaults,size=64M 0 0

Systemd simple service

This a template for simple sysmtemd service to change the ownership of a file, since in my case the file is in /sys and it is generated on boot, so using acl didn’t help me. I had to use this hack to change the ownership of a file on each boot.
What I need is a write permissions to a file in order to change the brightness on my laptop with i3wm.

The file is /etc/systemd/system/brightness.service
but there is symlink from the /etc/systemd/system/multi-user.target.wants directory.

This are the contents of the service file:

[Unit]
Description=Alter permissionsfor brightness

[Service]
ExecStart=/bin/chmod go+rw /sys/class/backlight/intel_backlight/brightness
ExecStop=/bin/chmod go+rw /sys/class/backlight/intel_backlight/brightness

[Install]
WantedBy=multi-user.target

You will also want to enable and start the service with:

systemctl enable brightness.service
systemctl start brightness.service

All of this action is happening on Ubuntu 18.04

Fix i3wm tearing in ubuntu

i3 is great window manger, but for some time I had struggles fixing some tearing it had, until I found recently this answer, which fixed it for me.

You will have to install the comptom composite manager with:

sudo apt install compton

then use the following config in ~./config/compton.conf or wherever you prefer to keep you config files. Then place this in that config file:

# basic configuration
backend = "glx";
vsync = "opengl-swc";

glx-copy-from-front = true;
glx-swap-method = 2;
xrender-sync = true;
xrender-sync-fence = true;

# transparancy settings for i3
opacity-rule = [
    "0:_NET_WM_STATE@:32a *= '_NET_WM_STATE_HIDDEN'"
];

Or this variation:

backend = "glx";
glx-no-stencil = true;
paint-on-overlay = true;
vsync = "opengl-swc";

Now you can start compton with that config file to test if it solves the problem for you:

compton --config ~/.config/compton.conf -b 

If that work for you, you can place it in your i3wm config file, so it will be loaded on startup –

  exec --no-startup-id compton --config ~/.config/compton.conf -b

No space left on device.

Sometimes we can be fooled by error messages. For example one sunny day you see that for some reason your web or mail server doesn’t work. So you go to check the logs and find something similar to this:

2016/12/28 09:02:37 [crit] 24668#24668: *472674 open() "/var/cache/nginx/client_temp/0020878597" failed (28: No space left on device), client: 192.168.1.1, server: www.domain.com, request: "GET /cart/add/uenc/aHR0cDovL3d3dy5hYmNob21lLmNvbS9zaG9wL2xvdi1vcmdhbmljLWxvdi1pcy1iZWF1dGlmdWwtdGVh/product/19471/form_key/N8l3OyVkC1el9T8q/?product=19471&related_product=&send_to_friend=%2F%2Fwww.domain.com%2Fshop%2Fsendfriend%2Fproduct%2Fsend%2Fid%2F19471%2F&form_key=N8l3OyVkC1el9T8q&super_group%5B19425%5D=1&super_group%5B19424%5D= HTTP/1.1", host: "www.domain.com", referrer: "http://www.domain.com/shop/organic-tea"

Then when you check the free space you see that you have more than enough, and all kind of irrational thoughts start flowing into your mind, when it is the simple inodes space.

Usually it is just that there is not enough inodes left free on your files system, simple as that, but is easy to overlook as for some people this doesn’t happen often (and it shouldn’t).

[root@hostname client_temp]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/os-root 1703936 1703103 833 100% /
tmpfs 1524264 4 1524260 1% /dev/shm
/dev/sda1 51000 50 50950 1% /boot
/dev/mapper/os-tmp 131072 2155 128917 2% /tmp
/dev/mapper/data-data
19660800 578302 19082498 3% /data

WordPress white page with Nginx and php-fpm

One of the reasons for this and nothing in the logs might be newer version of Nginx which and you will have to replace in your configuration

include fastcgi_params;

with

include fastcgi.conf;

Another problem is that you might need to add

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

in /etc/nginx/fastcgi_params , might be called also /etc/nginx/fastcgi.conf
It can also be added where your php setting block in Nginx is.

Might expand the post in the future with other possible reasons.

Ubuntu 16.04 – Install Apache2 and php7

I have experimented some time ago, with php7, as described in this post. At that time that wasn’t official realease, however the link seems to pick up on some searches in google, and there was some confusion for people expecting this to be copy/paste guide.

So I would try to fix these and outline the steps to install Apache2 with php7 on the latest server LTS Ubuntu – 16.04.01 – freshly downloaded from the official site, at the time of the writing.

First, the two most repeated commands:


sudo apt-get update

sudo apt-get upgrade

That will update any software installed on the server distro.

Then install apache2 –

sudo apt-get install apache2

Open the IP of the server you have installed it on and you should be presented with the default apache web page, if nothing is showing, you need to check if apache2 is running and if the firewall/ufw is blocking requests.

It comes the turn to install mysql –

sudo apt-get install mysql-server

You will be asked to provide password for the MySQL root user, you should be aware this is not the same user as the Linux root user, it is different one having rights to do everything with every database in MySQL, so it is a good idea to pick a different password then the ones you are using currently in the system.

After installation is finished, we will have to run buil-in script to tighten some of the security for MySQL and clean up some things –

sudo mysql_secure_installation

You will be asked for you the root password (MySQL root user), and then to choose a level for password validation:


There are three levels of password validation policy:

LOW Length >= 8

MEDIUM Length >= 8, numeric, mixed case, and special characters

STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

They are self explainatory, but I don’t really like password validators – I find them stupid security measure, especially on the level of MySQL server/users. So I would set that to 0/low for machines I am using.

You should read the questions and answer then with ‘y’ or ‘no’, in the past it was fine to answer all with ‘y/yes’ however, now I am noticing that the first question is “Change the password for root” – you might not really want to do that, so the best thing is to read what are you actually asked.

 

Ok, so we are getting there, let’s install php7 with the apache mod –

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-mcrypt php7.0-mysql

Then you could check the php version in the terminal with –

php -v

And the final step is to be sure apache is interpreting php in the browser.

First, become root with –

sudo -i

Then will add php info page to the server web root directory, so we could open it in our browser after that to verify it is running properly on check all the configuration details for php –

echo '<?php phpinfo(); ?> > /var/www/html/info.php'

After that you should navigate in your browser to the IP your server is listening to, and add /info.php after it, so it would look something like this –

http://192.168.122.113/info.php

That’s pretty much for it, but this where the complicated things starts from.

Enable Wake On Lan (WoL) on Ubuntu 16.04

If you have bought crappy motherboard from gigabyte like me that doesn’t even have support for WoL in the BIOS, and no-brand ethernet for which even the manufacturer can’t tell you if it support WoL(such unique feature!), then you will have to buy additional NIC, and add few line to the file:

/etc/network/interfaces

and add the following

auto enp3s0
iface enp3s0 inet dhcp
	up ethtool -s enp3s0 wol g

where enp3s0 is your interface name.

Or you can just buy yourself something decent.