Posts for Tag: linux

Mysql access denied for user root

Clean installation of mysql-server on Ubuntu (also happened to me on Debian), i can't connect with root and no password (even though on installation i set a blank password). This is not MariaDB, it's the proper 5.7 version installed via package repository after using the Mysql APT tool to set it up.

Here's the shit i had to do to make this work:

sudo emacs /lib/systemd/system/mysql.service

In this file, add --skip-grant-tables to the Exec command. After that run:

sudo systemctl daemon-reload
sudo service mysql restart

Now login into Mysql:

mysql -uroot

And run these commands to reset the root user's credentials:

use mysql;
update user set authentication_string=PASSWORD("") where User='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;

Now, just revert what you did to the startup script, don't forget to run systemctl reload and restart the service and logging in with root should work. 

I curse you, Oracle.

Lock and unlock Ubuntu using your iPhone

If, like me, you suffer from lazius extremis (lazy bastard, for the layman), you probably hate having to lock your computer when you leave and entering a password when you come back.

Fear not, young padawan, UDEV and xdotool to the rescue!

Here's how it works: UDEV (a linux mechanism that detects device events (including USB)) can be configured with rules that fire whenever something changes (i.e. a device is connected or disconnect from the system). When that happens, you want to run a script to lock or unlock your system, based on the type of rule that was fired. So, let's get started:

1. Create the UDEV rules:
1.1. Create a new file at /etc/udev/rules.d/100-lock-unlock-with-iphone.rules, with the following contents:

SUBSYSTEM=="usb", ENV{PRODUCT}=="2bc/12c8/520", ENV{DEVTYPE}=="usb_device", ACTION=="add",    RUN+="/home/youruser/bin/unlock"
SUBSYSTEM=="usb", ENV{PRODUCT}=="2bc/12c8/520", ENV{DEVTYPE}=="usb_device", ACTION=="remove", RUN+="/home/youruser/bin/lock"

1.2. In those lines, replace 2bc/12c8/520 with your device product id, which you can find running the following command in the terminal:

udevadm monitor --environment -u | grep PRODUCT

1.3. Disconnect or connect your iphone and you should see something like this:

PRODUCT=2bc/12c8/520

1.4. That's the value you put in the rules.
1.5. Replace youruser with your own user name, by the way.

2. Which leaves us with the /home/youruser/bin/lock and unlock scripts to create. Here is the lock script:

#!/bin/bash
export XAUTHORITY=/home/youruser/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=`ps -u youruser e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35`
log=/home/youruser/lock-unlock.log
echo `date` "-" `whoami` "- Locking system..." >> $log
su youruser -c "DISPLAY=:0 gnome-screensaver-command -a"
echo `date` "-" `whoami` "- System locked!" >> $log

2.1. Again, replace youruser with your own.
2.2. You might be wondering what those lines about DBUS and the X server are all about. The reason for those is that UDEV runs stuff as root. And we want to lock and unlock the screen as our own user. Those lines make sure of that.
2.3. For the unlock script we need to install a tool called xdotool. It's basically an automation command that allows you to make the computer type stuff and control the mouse automatically. The reason to use this is because there is no way in hell you can unlock a locked screensaver (believe me, i've tried). This tool simulates user input (moves the mouse a little bit and the enters your password and presses enter). I know it's not the most secure thing in the world but it's the only way i could make it work. Install it with:

sudo apt-get install xdotool

2.4. Here's the unlock script:

#!/bin/bash
 export XAUTHORITY=/home/youruser/.Xauthority
 export DBUS_SESSION_BUS_ADDRESS=`ps -u youruser e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35`
 log=/home/youruser/lock-unlock.log
 echo `date` "-" `whoami` "- Unlocking system..." >> $log
 export DISPLAY=:0
 xdotool mousemove 0 0 && xdotool type yourpassword && xdotool key Return
 echo `date` "-" `whoami` "- System unlocked!" >> $log

2.5. Replace youruser with your own user and yourpassword with your user password.

3. Reload the UDEV rules with:

sudo udevadm control --reload-rules

4. And now when you disconnect your phone from the computer, it should lock it. When you plug it back in it should unlock.

5. Happy hacking!

FISH Shell - How to set tmux window/pane title based on current command and directory

Here's a neat trick: Setting the current tmux window/pane title to the current running command. In case there is no command running, show the current working directory. But trim it a little, so we don't end up with huge window titles :)

In your config.fish file add the following functions:

# Set the tmux window title, depending on whether we are running something, or just prompting function fish_title if [ "fish" != $_ ] tmux rename-window "$_ $argv" else tmux_directory_title end end

function tmux_directory_title if [ "$PWD" != "$LPWD" ] set LPWD "$PWD" set INPUT $PWD set SUBSTRING (eval echo $INPUT| awk '{ print substr( $0, length($0) - 19, length($0) ) }') tmux rename-window "..$SUBSTRING" end end

The first one is a special function called by fish on every command execution. I'm using it to see what is being ran ($_ gives you the command in execution). If it's fish itself, then call tmux\_directory\_title to set PWD as the title.

The second function - as explained - sets the title as grabbed from the current working directory (PWD). Except it performs a little AWKing to trim it down to 20 chars max (the last ones).

Enjoy.

Change file modified and creation date/hour

Here's how to change a file's modified date:

touch -mt 201102260000 <file>

Where "201102260000" is year, month, day, hours, minutes all glued together (so 2011-02-26 at 00:00).

Also, if you want to change both the modified and creation date, then:

touch -t 201102260000 <file>

This works on OSX and (i'm assuming, since touch also exists there) Linux/Unix.

Change windows Administrator password

Recently I had some problems on my windows box and, somehow, I lost the administrator password. While trying to recover it from Linux, I managed to erase my entire partition table information. I thought everything was lost, but Linux (and google)once again came to my rescue: First, I managed to change the Administrator password using this: 1. sudo apt-get install chntpw 2. Mount the windows partition somewhere (say /media/win) 3. Head to /media/win/windows/system32/config 4. Execute this: chntpw -u administrator SAM. Then, after performing the stupid dd command that erased my MBR (and in the process deleted my entire partition table information), I managed to recover the partition table by using a command called "testdisk" (google for it ;)).