Saturday, February 7, 2015

Generate Random Passwords Easily

I have added a short function in my ~/.bashrc file that allows me to quickly generate a password of any length containing letters, numbers and symbols. This comes in handy when registering for a new website or similar.

Randomness

 

The command uses /dev/urandom to generate the random string. Evaluating the true randomness and strength of this password is beyond this guide. However, this should be sufficient for any general use.

Step by Step

  1. Edit the .bashrc file to add a new function that can then be called by an alias command.
    nano ~/.bashrc
  2. Add the following lines to the end of the file [1],
    passwdgen()
    {
    tr -dc "a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?=" < /dev/urandom | fold -w $1 | head -n 4
    }
  3. Then add an alias to easily call this function by,
    nano ~/.bash_aliases
  4. Add an alias of your liking.
    alias genpasswd='passwdgen' 
  5. Apply changes by either logging out and back in or,
    sudo source ~/.bashrc

Originally I wanted to call the alias command "passwdgen", however when using TAB to quickly substitute commands this was conflicting with "passwd". Hence I decided to use "genpasswd".

Done. The command can be used as,
genpasswd <password_length>
For example I want a password with 30 characters I would do,

:~$ genpasswd 30
0kA|=yff2RT(LE#|%QVMZVsFq2Tm&|
OPw1x=R9QMt}KhR*Xp?tluvu70+6=M
1-s)%-G19Gucz!fd@!+WXQZQ<U0ZVx
vJg:-Bvx)>>-?1qe_-TvEG8SHnUWXz

Note: You get 4 passwords as an output from the function (head -n 4). This is simply to give a choice for the user to decide which password to use. Also, this adds another increment of randomness.

The list of used symbols can be altered by editing the function contents within the quotes "a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?=". Adding extra symbols will increase the strength of the password, however some website (rarely) have restrictions on what symbols can be used.

Sources

Monday, December 29, 2014

Fixing Left 4 Dead 2 Mouse Lag in Ubuntu 14.04

Issue

I have noticed a slight lag-like thing while playing Left 4 Dead 2 recently, mainly when turning with the character. It wasn't too obvious, but I felt something was wrong. I couldn't aim properly with the weapons and therefore I often missed shots. It seemed too much of a coincidence. I have tried tweaking some mouse sensitivity, acceleration etc. however the issue persisted.

Solution

Long story short, others have experienced the same problem while gaming on Linux. Luckily, def075 mentioned [1] that this may be a mouse polling issue. I did a quick search and found the solution [2]. Do the following,

  1.     gksudo gedit /etc/modules
  2.     And add the following 2 lines to the end of the file,
        -r usbhid
      usbhid mousepoll=1
  3.     And finally reboot.

Where the "mousepoll" tells the operating system how often to query (read data from) the mouse. The faster it does it, the less lag there is in the input. For reference [3],

1=1000 Hz (1ms)
2=500Hz (2ms)
4=250Hz (4ms)
8=125Hz (8ms)
10=100Hz (default, 10ms)

I have set mine to "mousepoll=1" and it works just fine. Some other posts say to set it to 2, I personally just went for the maximum to be on the safe side. Feel free to experiment with this, however I personally don't think that this would cause a great decrease in CPU time/performance on overall.

References


[1] - http://forums.steampowered.com/forums/showthread.php?t=3147537
[2] - http://ubuntuforums.org/showthread.php?t=392494
[3] - https://wiki.archlinux.org/index.php/Mouse_Polling_Rate

Thursday, December 25, 2014

First Video in Steam Not Playable - Ubuntu 14.04 64bit

After upgrading to Ubuntu 14.04 64bit I noticed that Steam was misbehaving and some features were unavailable. The first video of any game on the store was appearing as a single picture and couldn't be played and any other video wouldn't play normally on full screen.

Issue

  1. The first video of any game from the store in Steam under Ubuntu 14.04 64bit is not playable. This is also mentioned by other members of the community.[1]
    First Video in Steam is Not Playable "0:00/NaN/NaN"
  2. (Possibly) Full screen videos in Steam have a glitch where the top part of the screen is flashing and doesn't display video. Also the sides of the screen might develop the same glitch.

Solution

Flash layer is the issue and causes all the problems. The solution is to  download a 32bit version of libflashplayer.so and add it to a local Steam directory. [2] Do as the following,
  • Close Steam
  • In your browser go to http://get.adobe.com/flashplayer/otherversions/
  • Download a tar.gz of the 32bit flash player for Linux.
    32 bit Flash player in tar.gz
  • tar xzvf install_flash_player_11_linux.i386.tar.gz
  • mkdir ~/.local/share/Steam/ubuntu12_32/plugins/
  • cp libflashplayer.so ~/.local/share/Steam/ubuntu12_32/plugins/libflashplayer.so
  • Start Steam
The first videos should be playable now and the screen glitch should also be gone. Worked as a charm for me.

Note:
By default the Steam directory is ~/.local/share/Steam however if you have changed this, please change it in the above commands.

You can remove the extra "junk" files that were unpacked from the tar.gz file. Untaring creates a "usr" directory in the download location that can be removed with rm -rf usr and the readme.txt with rm readme.txt.

Refrences


[1] - http://www.reddit.com/r/Steam/comments/2lrbp3/steam_linux_first_video_in_store_page_never/
[2] - http://wiki.gentoo.org/wiki/Steam#Flashplayer_on_amd64 

Sunday, December 21, 2014

Xorg High CPU Usage in Ubuntu 14.04

The Issue

I started having some problems recently with xorg using way too much CPU power on my Linux desktop.

I have recently upgraded to Ubuntu 14.04 on this machine ad got stuck after the login screen. I have tracked down the issue with the proprietary ATI/AMD drivers (fglrx). I have managed to get into tty terminal via CTRL+ALT+F2 and I have removed everything to do with proprietary drivers using this guide.

Hence I switched back to open source drivers that were working just fine. The problem being that xorg used a whole core of my dual-core CPU. This is not normal and had to be addressed. I started looking around and found the issue on many forums and posts.

The output of top looks something like this

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1262 root      20   0  267204  56016  15320 R  55.8  1.1  27:37.97 Xorg
 2641 nargren   20   0 1431604 574100  54096 R  35.9 11.5  11:26.71 firefox
 2435 nargren   20   0 1353536  84540  39284 S  12.6  1.7   1:09.57 compiz

The Solution

I have worked on this for the better part of 2 days and tried multiple methods suggested in different forum threads.

First I have overlooked this thread and tried to look elsewhere, however it has turned out that those methods didn't work. I got curious when I saw that QIII was also having the exact same issue and was using conky, just like me. He claimed using conky on 2 other distros as well, without any issues (on the same machine) so he ruled that one out as the source of the issue. In the end he has upgraded his OS and got rid of the issue (thread from 2012).

I have quickly killed my conky session with
sudo killall conky
to see what happens. And voilĂ ! Checking top immediately revealed what the issue was. After the change, this is how it looked like below.

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2435 nargren   20   0 1352980  84544  39288 R   4.3  1.7   1:20.38 compiz
 1262 root      20   0  267440  56024  15328 R   4.0  1.1  33:18.70 Xorg
 2993 nargren   20   0  654880  26736  12656 S   3.0  0.5   0:08.41 gnome-terminal

Conclusion

The issue was conky indeed. Hence I have to sacrifice my cool, real-time system information for now. However, I will keep on troubleshooting and see if by changing some settings I can get back my conky.

So if you are having the same issue with xorg and are running conky, try killing it and see if the CPU usage drops.

Saturday, November 1, 2014

VPN Autoconnect at Startup in Ubuntu

I am using Ubuntu 12.04 with XFCE4 and the VPN Autoconnect option in the network configuration doesn't do any good. As far as I know this is a bug which causes the VPN autoconnect to malfunction if a new network is joined, be that ethernet or WiFi. This is kind of a big problem as one needs to connect to the internet first before the VPN connection can be established.

VPN Configuration - (Autoconnect checkbox does not work)
The solution I found best working is to create a shell script that manually connects to a VPN with a delay of 15 seconds. This delay allows sufficient time for the operating system to establish an internet connection, after which connecting to the VPN should be smooth. If you do not add a delay, the VPN connection may start before you have an internet connection and hence fail.

Tip: You can play around with the delay time, likely on a faster computer an internet connection may as well be established in under 10 seconds. My system takes a bit longer to boot and I found 15 seconds to be acceptable and working just fine.


List Available VPN Connections & Get UUID

Type,
nmcli con

This will give you an output with all your network connections, including the saved VPN profiles.
Copy the UUID of the VPN connection you want to establish. (I will refer to this as <UUID> further on)

Create a Shell Script that Manually Connects

Create a shell script, lets say call it VPNAutoconnect.sh with the following in it

#!/bin/sh
#Script to automatically connect to VPN
sleep 15;nmcli con up uuid <UUID>
(<UUID> here is of course the UUID you copied in the previous step)
make the script executable by
sudo chmod +x VPNAutoconnect.sh

Adding Script to Startup in rc.local

Add script to /etc/rc.local to have it executed at system start. Your rc local file will look something like this,
nano /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Set screen backlight to 75%
xbacklight -set 75
#Connect to VPN after 15 seconds (allows connecting to the internet first)
sh /route/to/script/VPNAutoconnect.sh
exit 0
Save the file and you are done. The next time the computer starts up, after the delay of 15 seconds set in the script file a VPN connection will be initiated. 

Of course you can also directly add the command to rc.local, however if you have multiple VPNs and plan to change it in the future, a separate file might be better (little longer script) where you can set which VPN you want to connect to. Also, using a separate script can help you keeping your rc.local file more clean and organized.

Important: Place the VPNAutoconnect.sh script somewhere where you will not delete it accidentally or move it. If the file is moved then at the next startup rc.local won't be able to execute it (clearly). The problem arises when you trust the script and go ahead browse the web and find it out 30 minutes later that you are not using a VPN specifically because the file was moved and hence at boot no connection was made. Yeah. I personally like having a "Scripts" directory under my home that contains such small scripts.


Count the Occurence of All Words in a Number of Text Files

Recently I was given the task to analyse a range of files (around 300) and count the occurrence of all the words in each file. So the aim was to put together a piece of code that goes through all files in a directory, reads in a file, lists all the words occurring in it and counting how many time each word has occurred.

I quickly found out that in case of a single file the process is rather simple, the following code does a fine job,

for w in `cat FILE.txt`; do echo $w;done|sort|uniq -c >> results.out
This code reads in FILE.txt and for each word in it counts its occurrence and the creates a list from it.

However putting this into a recursive script was a little more complicated. So I took another direction and found a piece of code using sed to do the same job on a single file. With this and some scripting knowledge I was able to put together just what I needed.

Additionally, I used the command basename to output the name of the file so I know which file was which.

The final piece of code looks like this,

for file in `ls /PATH/TO/DIRECTORY/`
do
basename /PATH/TO/DIRECTORY/FILE >>
results.out        sed s/' '/\\n/g /PATH/TO/DIRECTORY/FILE | sort | uniq -c | sort -nr >>  results.out
echo "" >>  results.out
done

This does a perfect job and creates a single file with the output containing,
  • File name of each file
  • Occurrence of each word in the file, sorted from high to low
  • Empty line to separate data from each other
If anyone has any other suggestion or comment I am happy to hear it!



Sunday, September 21, 2014

How to Encrypt Files and Directories in Ubuntu - EncFS

After the Truecrypt.org went down recebtly and visitors were informed that Truecrypt contains vulnerabilities and it is discontinued I had to search for an alternative. I have known about EncFS for a good while, however the convenience of Truecrypt's cross-platform property has won me. Now however, I have moved to use EncFS on my linux machine.

Advantages

There are some advantages of using EncFS as per explained at arg0.net/encfsintro. Just to sum it up:
  • Size grows without need of reformatting
  • Backing up is easier (with e.g. rsync or GUI version grsync) as files can be individually backed up
  • Can be layered on other file systems
SO in general, one doesn't need to specify a fixed size for the encrypted file system, which simply means there will be no "unused encrypted space". Also, the fact that files are individually available, allows backing them up one-by-one, which makes it, well possible at least to say. With Truecrypt previously, if one wanted to back up a container, the entire encrypted volume had to be uploaded and re-uploaded any time something was changed in the original document. At the cost of some "anonymity" (described below) this is no linger an issue.

Disadvantages


Again as per explained in the above mentioned link:
  • Meta Data is available in the encrypted directory
    • File size
    • Permissions
    • Number of files
    • Approximate name length (but not the name itself)
Unfortunately, this allows some guessing what the files might be. Not their actual name, or content but definitely what they are. Even though file types are not shown, a couple KB document is probably some sort of text file, whereas a few MB one might be a picture, pdf and so on. Modification dates are also shown in plain text, in some scenarios this might be an issue.

Everyone has t decide, whether or not leaking this kind of information is acceptable or not. It is definitely better than not encrypting the file sin the first place, however it might not be enough in other cases. 

How Does it Work?


Check out the code below. This would create en encrypted directory and mount it to another directory, effectively a mount point.

$ mkdir /tmp/crypt-raw
$ mkdir /tmp/crypt
$ encfs /tmp/crypt-raw /tmp/crypt
Volume key not found, creating new encrypted volume.
Password: [password entered here]
Verify: [password entered here]
 

This would  do the following


mkdir /tmp/crypt-raw - Create a directory at the specified location. This will be the actual directory holding the encrypted files.

mkdir /tmp/crypt - Create a directory at the specified location. This will be the mount point for your encrypted directory. This directory will not contain any actual file, it will simply link to the encrypted directory. If you have it at /tmp it will be deleted on every restart. You can choose it to be at any other location too, of course.

encfs /tmp/crypt-raw /tmp/crypt - Command to mount the encrypted directory (crypt-raw) to the location of the directory crypt. If running the command for the first time it will ask you to give a password and specify the type of encryption.

 

How to use this


You are technically done. Any files you put in the crypt directory will actually be stored in crypt-raw (encrypted). As long as the directory crypt is mounted, the encrypted files are visible. If you unmount it, the files are only visible in crypt-raw and if you check them you only see scrambled text/data, that is ciphertext.

Example of some files stored in the encrypted directory
As you can see in the example above, file size and modification date is shown as clear text, but file names are encrypted.

Unmount


To unmount the directory run
$ fusermount -u /tmp/crypt

After this the both the mount point and encrypted directories will still be present, but crypt will be "empty" while crypt-raw will only contain unreadable ciphertext.

Note: /tmp is flushed on restart hence crypt directory will be removed. This is, however no problem, running the code will recreate and mount this directory again upon new reboot.

Tip


Create a small script that will mount, unmount and open the encrypted directory you use the most! Take a look at http://pastebin.com/CXRPQUsK for what I am using. This way is much faster and more convenient to use encryption, it only takes a few seconds to open and close the container.