Tuesday, July 21, 2015

Set default browser LXDE (Fedora)

For some reason going to Menu>Preferences>Prefered Applications and choosing Google Chrome didn't work. Midori was still handling all my http/https links.
Solution for this is xdg-mime

To check default browser for http and https type in terminal

xdg-mime query default x-scheme-handler/http

xdg-mime query default x-scheme-handler/https

This command will give you output .desktop file which is used to open those links. To set them to Google Chrome, command is

xdg-mime default google-chrome.desktop x-scheme-handler/https

and for http

xdg-mime default google-chrome.desktop x-scheme-handler/https

Instead of google-chrome.desktop you can use any .desktop file to set default application.

Thursday, July 9, 2015

Fedora 22 LXDE VNC server (x11vnc)

For connecting to your machine over the Internet, Teamviewer is super-easy to use. But for one reason or another it's not always possible. Solution to this is x11vnc.

First, install x11vnc to your machine using

sudo dnf install x11vnc

You don't want anybody to be able to connect to your machine so you should setup password

x11vnc -storepasswd

This command will ask you to type in password for remoting-in to your machine and save it to ~/.vnc/passwd

Now, you should test to see if your VNC server works.

x11vnc -forever -rfbauth ~/.vnc/passwd -rfbport 5900

You should be able to connect to your machine with VNC Viewer.

You could run this command every time you start your machine, but it's not really handy. I wanted to setup my machine so that x11vnc server starts as soon as login screen shows (so even if I'm not logged it I could remote connect).

To do that, open file /etc/lxdm/LoginReady and add following line

x11vnc -display :0 -forever -bg -rfbauth /home/locodog/.vnc/passwd

Please note that here you should put whole path to your VNC password file, do not use ~/.vnc/passwd

Reboot your machine and test it out.

If you don't want to go through all this, after installing x11vnc you could just use x11vnc Gui to start your vnc server.

For VNC client, after testing many I recommend Remmina. It has everything I need, bookmarks because I don't want to type in IP address every time I want to connect to remote machine and also, scaling. It's really no fun having to scroll up-down, and left-right in order to reach every corner of remote display. Keep in mind that Remmina will disconnect (or not connect at all) if color depth on client side is not same as on server side.

This post was very helpful:
http://ubuntuforums.org/showthread.php?t=1868554

Thursday, June 25, 2015

Screenshots in Fedora LXDE

I was just about to make screenshot on my system and I noticed that there is no application for that. Best solution I found is gnome-screenshot, it's easy to use and it has a nice "flash" effect when making screenshot.

Step 1 is install gnome-screenshot

sudo dnf install gnome-screenshot -y

I don't like my screenshots mixing with other pictures I have so I created directory Screenshots

mkdir ~/Pictures/Screenshots

How to make gnome-screenshot put your shots there:

       gsettings set org.gnome.gnome-screenshot auto-save-directory "file:///home/$USER/Pictures/Screenshots/"
 

Now we need to make "shortcut" to taking a screenshot. In my case, button PrintScreen will take full screenshoot and Ctrl+PrintScreen will make only active windows screenshot.

Open ~/.config/openbox/lxde-rc.xml with your favorite editor and insert these lines inside before </keyboard> line.

<keybind key="Print">
        <action name="Execute">
                <command>gnome-screenshot</command>
        </action>
</keybind>
<keybind key="C-Print">
        <action name="Execute">
                <command>gnome-screenshot -w</command>
        </action>
</keybind>

After you saved and closed file, execute:

openbox --reconfigure

Tuesday, June 16, 2015

Mikrotik backup script using SSH/SCP - easy and dirty way

*Disclaimer*
This is not high-end, ultra safe script. It's considered a bad practice to store your passwords in cleartext, especially in script file. I am beginner in scripting and this is just a way to automate task that when done by hand could take very long time. But, if you still want to proceed, read on!

For this script to work we will need to install program called sshpass, install it from your distro repository. This script assumes that all your Mikrotik devices have same username and password and SSH service enabled.
EDIT: After some experimenting it seems that sometimes ConnectTimeout=10 is not enough, if you have troubles connecting, increase to 20.

#!/bin/bash
#Don't just copy and paste this script, adjust all paths to your needs
#You can remove any line starting with hashtag because those are comments and they are not executed

NOW=$(date +%d-%b)
#This line creates variable NOW with day and month, like "03-May"
#I will use this example, "03-May" to make it easier to follow

COUNTER=0
#sets counter to zero

mkdir /home/locodog/backups/mikrotik/$NOW
#creates directory with name according to date "03-May" for example

for i in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
#tiks.txt is file that has list of all Mikrotik devices you want to backup
COUNTER=$((COUNTER + 1))
#adds +1 to counter for every device it attempts to backup

echo -e $COUNTER"." "Backing up device \033[1m$i\033[0m" 
#tells you which device is currently being backed up

sshpass -p "password" ssh -oStrictHostKeyChecking=no -o ConnectTimeout=15 $i -l username system backup save name=$NOW
#Connects to mikrotik device from list and creates backup file with name "03-May"

sshpass -p "password" scp -oStrictHostKeyChecking=no -o ConnectTimeout=10 username@$i:$NOW.backup /home/locodog/backups/mikrotik/$NOW/$i.backup
#Downloads previously created backup file but now that file is downloaded into folder "03-May" 
#and file has name 192.168.1.1.backup (or whatever IP address your Mikrotik has
#keep in mind that backup file inside your Mikrotik will not be deleted and it will have a name 03-May.backup
done

TOTAL=$(echo $COUNTER)
#Creates variable "total" with value equal to total number of devices that were backed up (or attempted)
for f in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
if [ ! -f /home/locodog/backups/mikrotik/$NOW/$f.backup ];
 then
echo -e "Backup of \033[1m$f\033[0m was not done" && COUNTER=$((COUNTER - 1 ))
fi
done
#this section checks is there any backup file missing and
#gives you IP address of device where backup could not be performed for whatever reason

echo -e "Backup was successful on \033[1m$COUNTER\033[0m out of \033[1m$TOTAL\033[0m Mikrotik devices."
#Here you get report, on how many devices did you try to do backup and how many sucessful

After adjusting this script to your needs save it in file for example backup-mikrotik.sh
and make it executable with chmod +x backup-mikrotik.sh

File tiks.txt (or whatever you decide to call it) should have list of IP addresses of your Mikrotik devices like this:
192.168.1.1
192.168.1.2
192.163.25.4
10.25.66.8

And so on. This file should be plain text, that means don't create it with LibreOffice or anything like that. Use plain text editor like Gedit, Kate, Leafpad, Vim.

Wednesday, June 10, 2015

Samsung Printer/Scanner drivers for Linux Fedora

To install Samsung printer drivers go to
http://www.samsung.com/us/support/downloads
Click on Printers and choose your printer model. Most likely it will be tar.gz file. In termial go to folder where you downloaded the file. In most cases it will be ~/Downloads and type

tar xvf uld_v1.00.06.tar.gz

This is name of my downloaded file, you will here type name of file you downloaded. After content of archive is extracted type

sudo ~/Downloads/uld/install.sh

Also, you will here type path where you extracted file from step 1. This command must be executed with elevated privileges (sudo) or it will fail.
Confirm execution of script with "y" and go through EULA with Enter. Be carefull because after EULA you will have another confirmation and if you just keep pressing Enter like maniac default answer will be "No" and you will have to run script again. So, choose yes. Next you will be asked do you want to make firewall rules if you intent do use printer over network, after answering that (I choosed No) script will finish installation of drivers.
Start Printer Settings and turn on you printer. Click Add and select your make and model. You will be asked to install drivers, but since we already did that you can hit Cancel. Confirm friendly name and apply settings. Printer installation should now be completed, if you want you can print test page.

Monday, June 8, 2015

SSH Access Denied and SFTP connection closed Fedora

After having my Fedora 21 messed enough that it wasn't worth fixing, I decided to install new shiny Fedora 22 LXDE spin. Installation went (almost) good. And you will see why I say almost.

Upon finishing installation and enabling ssh server with

systemctl start sshd
systemctl enable sshd

I tried to login from my other computer. After asking for password it would simply return Access Denied. Using verbose mode didn't help me much. With my first google search I came upon this problem with guy who was asking help simply saying that he solved his problem by changing his password. 
I know my password, I just typed it in to log onto my system, and I have saved profile on second computer so I don't even need to type it. (yes, I know it's a security hole).
I never had pleasure to meet with ssh setup so I assumed that there is something in sshd_config that I didn't set correctly. So, instead of going through all settings I just copied sshd_config from co-worker's computer, because it works there.
No go. Another 20 minutes of going through every single line inside config file, no go.
Eventually i thought "hmm, maybe I should try changing the password"...
And I kid you not, it was the solution. I even typed in same exact password and this time it worked.

Now just to restore some files I previously backed up, over sftp, I'm too lazy to copy it over usb drive.
sftp username@hostname...
insert password
Connection Closed
Goddamit!

Let's browse Google some more. Most people were having trouble with key login, but almost nobody with password.

sftp -v myusername@myhost
And there it was
debug1: Exit code 127

To make story short, when I copied sshd_config file from another computer I didn't consider that it was Slackware. In most cases that wouldn't matter, but in mine it did because in sshd_config there is line

Subsystem sftp /usr/libexec/sftp-server

But on my system, location of sftp server was

/usr/libexec/openssh/sftp-server

After changing that line in config file and systemctl restart sshd it worked.

Saturday, June 6, 2015

DNS probe finished no internet problem on Windows 8

One day I fired my laptop with Windows 8.1 on it, connected to home WiFi and started Chrome but no website could be open.
Error was DNS probe finished no internet.

I was able to ping 8.8.8.8 and anything else by IP address, but no hostname could be resolved. Tried setting Google DNS servers, OpenDNS servers, but with same result.

ipconfig /flushdns also didn't do any good.

netsh winsock reset didn't help

Other device on that same WiFi network (my phone) worked just fine. After connecting laptop to cable connection everything worked without any problems. It was clear that problem was wireless related.
Next step was downloading new set of drivers for wireless card. Completely uninstalling and deleting old drivers and installing new (actually it was same version) solved my problem.

One more possible solution I came accross, but I didn't have chance to test because driver reinstallation fixed everything is

netsh int tcp set heuristics disabled
netsh int tcp set global autotuninglevel=disabled
netsh int tcp set global rss=enabled


Creating swap file

One of problems I came across was that I didn't have partition for swap. In modern day, if you have enough RAM, swap partition is not necessary but it is required if you want to use suspend-to-disk. I found out that I could make swap file that will work same as swap partition.

You will need root access:

sudo -s 

Now create actual file

dd if=/dev/zero of=/swapfile bs=1M count=2048

This command will create file named swapfile with size of 2GB (1MB x 2048). You can adjust these numbers to fit your needs.

Let's close huge local vulnerability but giving only root rights to read and write to the file with

chown root:root /swapfile
chmod 0600 /swapfile

After file is created type this command to make it swap

mkswap /swapfile


And to activate swap file

swapon /swapfile


To make your system activate this file after reboot, open /etc/fstab file with

vi /etc/fstab


And insert this line (adjust name of your file accordingly)

/swapfile none swap 0 0 



To check if your file is active or not, type

free -m




Removing Swap file or partition


First find your swap partition with

sudo fdisk -l

And look for Linux Swap file. This is sample output


Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048    206847    204800  100M  7 HPFS/NTFS/exFAT
/dev/sda2          206848 164339711 164132864 78.3G  7 HPFS/NTFS/exFAT
/dev/sda3       164339712 564938406 400598695  191G  7 HPFS/NTFS/exFAT
/dev/sda4       564938750 625139711  60200962 28.7G  5 Extended
/dev/sda5       564938752 621996031  57057280 27.2G 83 Linux
/dev/sda6       621998080 625139711   3141632  1.5G 82 Linux swap / Solaris

In my case, swap partitions is /dev/sda6, to disable swap on this partition I type

swapoff /dev/sda6

Now go to /etc/fstab and comment out line with swap. This is that line in my fstab file:

UUID=9f0f62d3-44fd-4286-9b90-1faff3b4a44c swap                    swap    defaults        0 0

To comment it, just add # on the beginning of the line. After rebooting, using free -m you could check that your swap is not there anymore (or if you had more than 1 swap partition/file it is reduced).

Saturday, May 30, 2015

Creating custom commands in Linux

One of programs I often use is Winbox, tool for managing Mikrotik routers. So I want to make it fast and easy to run it from anywhere.

Winbox is windows application and requires Wine to run, so you cannot simply type in winbox and run it. We are going to do just that.

First download winbox or any application you want and place it somewhere. I don't like using default Wine location in ~/.wine/drive_c/Program Files/ and I make folder just for my apps like home/locodog/programs

So, you installed Wine with sudo apt-get install wine or  yum install wine or how ever you install applications on your distro.

To run winbox simply by typing winbox into terminal or Alt+F2 Run screen, open console and type this line:

echo "/usr/bin/wine home/locodog/programs/winbox.exe" > winbox

/home/locodog/programs/winbox.exe is actual location of winbox.exe and it can be where ever you want.
Now let's make newly created file winbox executable by doing

chmod +x winbox

At this point you can run winbox by typing complete path to it, but to be able to run it from anywhere copy file to /usr/local/bin/  or move it there if you don't want copy of it with

sudo mv winbox /usr/local/bin

That's it, you can now run your winbox app from anywhere.


Microsoft Natural Keyboard 4000 in Linux Fedora 21 LXDE

Microsoft Natural 4000 is great keyboard, but on Linux, well at least in my case multimedia keys didn't work out of the box. I had to figure out how to make them work.

First, I wanted to use custom shortcuts for buttons like HomePage, Search, Email and those numbered 1-5.

To find out does X detects your keypresses in console start xev.

xev | egrep -o "keycode.*\)"

While program is running press keys you would like to map. Result should be something like this:

keycode 195 (keysym 0x1008ff48, XF86Launch8)
keycode 195 (keysym 0x1008ff48, XF86Launch8)

Keep in mind that xev detects key press and key release as separate events. What we need from here is XF86Launch8.

Open file ~/.config/openbox/lxde-rc.xml in your favorite text editor and find line with </keyboard> in it. You need to write your keybindings between <keyboard>  and </keyboard>. I like to add mine just above </keyboard> because it's easier for me to find it later.
Now insert actual keybinding. This particular key I want to use for starting teamviewer.

<keybind key="XF86Launch8">
    <action name="Execute">
      <command>teamviewer</command>
    </action>
  </keybind> 

 Save file and in console execute openbox --reconfigure

For more info check out http://wiki.lxde.org/en/LXDE:Questions#Modify_mouse.2Fkeyboard_bindings

Zoom slider


Problem with zoom slider is that xev doesn't detect anything when you move it. After reading many posts I found what works for me.
Create file /etc/udev/hwdb.d/70-keyboard.hwdb
You will need to create folder hwdb.d because it doesn't exist.

with this content

# Microsoft Natural Ergonomic Keyboard 4000
keyboard:usb:v045Ep00DB*
 KEYBOARD_KEY_c022d=zoomin
 KEYBOARD_KEY_c022e=zoomout

Instead of "zoomin" or "zoomout" you could give keys different function like pageup and pagedown if you want to use this key for scrolling through page. Or even up and down to scroll line by line.
Save file and run sudo udevadm hwdb --update after that you might need to unplug and plugin your keyboard again or if that doesn't help, reboot.