Showing posts with label Linux-OS. Show all posts
Showing posts with label Linux-OS. Show all posts

Sunday, 29 May 2016

Midnight Commander Key Bindings

To change the default key bindings for Midnight commander on Arch Linux. From the man pages :
  Redefine hotkey bindings

Hotkey bindings may be read from external file (keymap-file).  Initially, Mignight    Commander creates key bindings using keymap defined in the source code. Then,  two  files  /usr/share/mc/mc.keymap  and  /etc/mc/mc.keymap are loaded always, sequentially reassigned key bindings defined earlier.  User-defined keymap-file is searched on the following algorithm (to the first one found):

              1) command line option -K <keymap> or --keymap=<keymap>
              2) Environment variable MC_KEYMAP
              3) Parameter keymap in section [Midnight-Commander] of config file.
              4) File ~/.config/mc/mc.keymap
Thus we can just
$ cp /etc/mc/mc.default.keymap ~/.config/mc/mc.keymap 
Then edit the keymap file as required. Its useful to save the file within a repository such as git when complete.

Monday, 25 April 2016

Using /proc on Linux

/proc on Linux is a pseudo file system that provides an interface to the kernel data structures. It is very useful for troubleshooting and the following highlights some that I have found helpful. The man page can be accessed with full details on Arch Linux with :
# man 5 proc

/proc/cpu

  • A collection of CPU and system architecture dependent items
  • Useful to find the CPU type, number of cores and flags
  • lscpu uses this file to obtain its information
    $ cat /proc/cpu
    

/proc/cmdline

  • Arguments passed to the Linux kernel at boot time
  • Useful to see what options were used by e.g. grub on boot
    $ cat /proc/cmdline
    

/proc/loadavg

  • The first three fields in this file are load average figures giving the number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes.
  • Same numbers used by uptime
    $ uptime && cat /proc/loadavg
     20:37:08 up  1:36,  8 users,  load average: 0.51, 2.17, 2.56
     0.51 2.17 2.56 6/681 11570
    
  • The fourth field consists of two numbers separated by a slash (/)
  • The first of these is the number of currently runnable kernel scheduling entities (processes, threads)
  • The value after the slash is the number of kernel scheduling entities that currently exist on the system.
  • The fifth field is the PID of the process that was most recently created on the system.

/proc/meminfo

  • Reports statistics about memory usage on the system
  • Used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel
     $ cat /proc/meminfo
    

/proc/[pid]/cmdline

  • Everything in Linux is a file and each process has a directory under /proc for its /pid/
  • As pid 1 is the init script we can
    $ cat /proc/1/cmdline
    /sbin/init
    
  • This read-only file holds the complete command line for the process

/proc/[pid]/fd/

  • A subdirectory containing one entry for each file which the process has open, named by its file descriptor
  • Its a symbolic link to the actual file

Sunday, 25 October 2015

How To: Automatically cd to a directory after you create it



The majority of the times that I create a directory I would then change to that directory. To make this happen automatically I add the following to my .bashrc :

mkcd () { mkdir -p "$@" && eval cd "\"\$$#\""; }

Example :

$ mkcd ~/tmp/FOO
$ pwd
/home/cw/tmp/FOO

How To: Display your Top 10 cli commands




Its useful to be able to see which command you use most often on a Linux box. I add the following to my .bashrc :

cli-top-10() {
  history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head
}

Example output :

57 ls
49 cd
26 mv
25 curl
18 wget
17 rm
15 sudo
13 packer
12 docker
12 vim

Tuesday, 8 September 2015

VirtualBox slow on installation of Guest OS

I use VirtualBox for testing in many environments. Normally the defaults are fine for installing Linux but on one environment it was really slow to just install. Installing Debian Jessie from an ISO would normally take 15 minutes but in this environment after an hour it was still only at 35%. The host OS was not over taxed and has lots of spare CPU + RAM resources free.

After some testing I found the issue was resolved by :

1. Open Settings
2. Navigate to System->Motherboard
3. Change the Chipset to ICH9 (see below)
4. Press OK

Now the installation will be fast.




Wednesday, 17 December 2014

How to : Disable BeagleBone USR Heartbeat LED lights


The BeagleBone Black by default has 4 User LED's USR0, USR1, USR2 and USR3 that light up to show the board is alive. While useful it can be annoying.

There are 4 sub directories under /sys/class/leds which allow you to configure the LED behaviour. There is one directory per LED. Within each sub directory a trigger file details what triggers the LED e.g.

# cat /sys/class/leds/beaglebone\:green\:usr0/trigger
none nand-disk mmc0 mmc1 timer oneshot [heartbeat] backlight gpio cpu0 default-on transient

The [heartbeat] is what is showing the board is alive. This can be disabled via :

# echo none > /sys/class/leds/beaglebone\:green\:usr0/trigger
# echo none > /sys/class/leds/beaglebone\:green\:usr1/trigger
# echo none > /sys/class/leds/beaglebone\:green\:usr2/trigger
# echo none > /sys/class/leds/beaglebone\:green\:usr3/trigger

To light up the LED use the brightness file.

Turn USR0 on

# echo 1 > /sys/class/leds/beaglebone\:green\:usr0/brightness

Turn USR0 off

# echo 0 > /sys/class/leds/beaglebone\:green\:usr0/brightness

Reboot

The LEDs will be reset on after reboot which is useful for troubleshooting. The /sys directory on Linux is a RAM based File system. For ease you could create a simple bash script to run as the last file on boot.

# cat DisableUSRLEDs.sh
#!/bin/bash
echo none > /sys/class/leds/beaglebone\:green\:usr0/trigger
echo none > /sys/class/leds/beaglebone\:green\:usr1/trigger
echo none > /sys/class/leds/beaglebone\:green\:usr2/trigger
echo none > /sys/class/leds/beaglebone\:green\:usr3/trigger

Monday, 15 December 2014

How to : Delete files securely using shred from Nemo



The Linux command line tool shred is a fantastic way to securely remove files. It can be used from the command line e.g.

$ shred --force --remove --verbose --zero examplefile3
shred: examplefile3: removing
shred: examplefile3: renamed to 000000000000
shred: 000000000000: renamed to 00000000000
shred: 00000000000: renamed to 0000000000
shred: 0000000000: renamed to 000000000
shred: 000000000: renamed to 00000000
shred: 00000000: renamed to 0000000
shred: 0000000: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: examplefile3: removed

Where

  • --force : change permissions to allow writing if necessary
  • --remove : truncate and remove file after overwriting
  • --verbose : show progress
  • --zero : add a final overwrite with zeros to hide shredding

The default is to overwrite the file 3 times but you can override this with –iterations=N, setting N to be the number of iterations.

Its useful to be able to quickly remove files securely using the File manager for Cinnamon Nemo. First its useful to review the Nemo Actions sample file :

$ vim /usr/share/nemo/actions/sample.nemo_action

Define the settings you require e.g.

$ cat /usr/share/nemo/actions/shred.nemo_action
[Nemo Action]
Name=Shred file
Comment=securely delete a file
Exec=/usr/bin/shred --force --remove --verbose --zero %F
Icon-Name=gtk-execute
Selection=S
Extensions=any;
Quote=double

  • Start nemo, right click on file and select 'Shred file'



    Note – Based on feedback I added 'Quote=double' which will delete files with spaces. 

How to : Password protect a zip file on Linux




Its useful to quickly password either one or a number of files on Linux. For example to transit some files between sites on a USB drive. The encrypt option on the zip command line tool will encrypt the contents of the zip archive using a password which is entered on the terminal in response to a prompt

Note : The Zip encryption is not very strong and could be cracked so use with caution

  • To encrypt a single file :

$ zip --encrypt Example.zip examplefile1
Enter password:
Verify password:

  • To encrypt a multple files :

$ zip --encrypt ExampleMulti.zip examplefile1 examplefile2 examplefile3
Enter password:
Verify password:

  • Tu uncompress the zip file :

$ unzip Example.zip
Archive: Example.zip
[Example.zip] examplefile1 password:

Wednesday, 16 July 2014

Combine bash history across multiple terminals



I use tmux (and many people also use screen) for multiple terminals. On slight down side is that by default the history command does not remember the commands between sessions on the same server. After some research and experimenting I found the following works really well in my .bashrc :

export HISTFILE=~/.bash_history
export HISTFILESIZE=500000
export HISTSIZE=500000

export HISTCONTROL=ignoredups:erasedups
shopt -s histappend
unset PROMPT_COMMAND
export PROMPT_COMMAND="history -n;
history -w;history -c;history -r;$PROMPT_COMMAND"

Using 'Ctrl + R' will have the previous entries.

Note - if you are on session A run a command and move to session B you must either just press enter or another command before the entry in session A appears

Here is each line with some comments

  • Define where the history file is saved
export HISTFILE=~/.bash_history
  • How many commands to save in the HISTFILE
export HISTFILESIZE=500000
  • How many commands to save in the current session
export HISTSIZE=500000
  • Do not save duplicate commands and erase any previous duplicates
export HISTCONTROL=ignoredups:erasedups
  • How the history list is written to the HISTFILE. The '-s' option sets the option to append the history list of the current session to HISTFILE
shopt -s histappend
  • Clear current value of PROMPT_COMMAND
 unset PROMPT_COMMAND
  • Contents of PROMPT_COMMAND are run just before Bash displays a prompt. This will save and reload the history after every command
  •  -n : Append the new history lines
  •  -w : Write history which will cause 'erasedups' to occur
  •  -c : Clear the history list
  •  -r : read the current history file 
export PROMPT_COMMAND="history -n;history -w;history -c;history -r;$PROMPT_COMMAND"


 Hope you find this as useful as I have :)

Saturday, 12 July 2014

Reasons not to kill -9 a process in Linux





Its something that is very common and we have all done it in our time. You have a process that is not running the way you want it or maybe it has hung. The solution seems obvious :

$ kill -9 <pid>

I had been told in the past this was a bad idea but why is this not good. There are a few reasons I have found out. When you just kill -9 a process it will not :

- Let its children know that it no longer exists

- Will not shut down its socket connections

- Temp files will not be cleaned up

In general kill -9 should only be used as a last resort as you could at some point end up corrupting an important program e.g. a database.

The recommend procedure to use is :

$ kill -15 <pid>

This allows the target process to try and clean up after itself. If that does not work in order :

$ kill -2 <pid>

$ kill -2 <pid>


And finally ;

$ kill -9 <pid>

Like anything it just takes a bit of getting used to and after 6 months on following this I believe this is a good personal practise to follow.

You can get a full list of the signals with :

 
$ kill -l

Sunday, 13 April 2014

Create large files quickly on Linux



For testing its really useful to create files of an exact file size. The following details some quick ways to create files on Linux with related considerations. 


fallocate - preallocate space to a file


  • Recommended on Linux due to speed
  • Example to create 1GB
$ time fallocate -l 1G 1GBFile.tmp

real    0m0.051s
user    0m0.008s
sys    0m0.004s

  • Note : you can remove the 'time' command 
  • -l :  Specifies the length of the allocation, in bytes
  • The file was created in less than 1 second!
  • Consideration - A fast alternative to dd

dd - /dev/zero


  • /dev/zero is a Linux file providing null characters when read
  • Example to create 1GB
$ time dd if=/dev/zero of=1GBFile.tmp bs=1024 count=$((1024 * 1024 ))
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 18.4725 s, 58.1 MB/s

real    0m18.479s
user    0m0.300s
sys    0m8.248s

  • Note : you can remove the 'time' command
  • bs : byte size
  • count : copy only this amount of input blocks
  • The 1GB file was created in 18 seconds 
  • Consideration - file cannot be read from but is fast to create

dd - /dev/urandom

  • /dev/urandom is a Linux file that serves as a random number generator 
  • Uses entropy collected from device drivers and uses the pseudo-random generator when more entropy is needed than has been collected 
$ time dd if=/dev/urandom of=1GBFile.tmp bs=1024 count=$((1024 * 1024 ))
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 132.903 s, 8.1 MB/s

real    2m12.910s
user    0m0.272s
sys    2m0.952s

  • Note : you can remove the 'time' command 
  • This took 2 minutes & 12 seconds significantly longer than using /dev/zero
  • Consideration - No readable lines but some content information. Can take time to create


Command line fun!



If you use the terminal a lot there are a few cool applications you can use to Jazz things up. Here are some of my favourites.

Steam Locomotive (sl)

  • Probably my favourite to get a cool steam locomotive ti run across the screen (shown at top of post)
  • Install with
 $ sudo apt-get install -y sl
  • Run with
$ sl
  • Or to make it 'Fly'

$ sl -F

As a practical joke you could set an alias for 'ls' on someone’s machine :)

 

Cmatrix

  • I think this is really coolto have running :)
  • Install
$ sudo apt-get install -y cmatrix

  • Run 
$ cmatrix


Oneko




  • This is pretty random but worth trying at least once
  •  It changes your cursor into mouse and creates a cat
  • The cat then starts chasing around your mouse cursor
  • Install
$ sudo apt-get install -y oneko

  • Syntax to see the magic happen!
$ oneko

Fortune



  • Fortune give 'fortune cookies' of wisdom when they are run
  • Install the 'classic' version
  $ sudo apt-get install -y fortunes
  • Run
$ fortune
Although the moon is smaller than the earth, it is farther away.

  •  If you search apt there are lots of other versions (including different languages)
  • System admins will like 
  $ sudo apt-get install fortunes-bofh-excuses
  • You can include these words of wisdom in your .bashrc

 

Cowsay



  • A configurable speaking/thinking cow!
  • Install

$ sudo apt-get install -y cowsay

  • Run 

$ cowsay "Enter any text"

  • There are other animals you can use listed at /usr/share/cowsay/cows
  • You can select a different cowfile with '-f'
$ cowsay -f ghostbusters.cow "Moo"


  •  The man file for cowsay is very detailed on the available options

Cowsay with Fortune




  • Its a great combination to use cowsay with fortune and is very simple
  • Examples
$ fortune | cowsay
$ fortune | cowsay -f ghostbusters.cow

  • You can use these in your .bashrc file
  • An option is to script it so the cowfile will be random

Figlet



  • Gives a cool 1980's look for text
  • Install
$ sudo apt-get install -y figlet
  • Example
$ figlet vancouver


Useful examples for job management in Linux


Being able to master the use of background jobs in Linux is very important and is a great way to increase efficiency. Useful commands are outlined below.

When a job is currently running

  • ctrl -z : suspend a job
^Z
[1]+  Stopped                 ./ExampleScript.sh 

  • ctrl -c : terminate a job

Job Management

  • List all jobs e.g.
 $ jobs
[1]+  Running                 ./ExampleScript.sh &


  •  Start a job in the background with <command> & e.g.
 $ ls &
[1] 6460
  • Kill job using kill %jobnumber e.g. 
$ kill %1
$ jobs
[1]+  Terminated              ./ExampleScript.sh

For a suspended job

  • Continue job in background : bg %jobnumber :
 $ bg %1
[1]+ ./ExampleScript.sh &

  • Bring job to foreground : fg %jobnumber e.g.
$ fg %1
./ExampleScript.sh



Empty Trash quickly from Command Line in Linux



I find it useful to be able to quickly delete the Trash on any Linux box I am working on. To do this is straight forward on Ubuntu, Linux Mint or Arch Linux.

Where Trash Resides

  • List your Trash files under :
$ ls -la ~/.local/share/Trash/
  • The 'files' sub-directory is the actual files 
  • The 'info' sub-directory is the metadata for each file
  • The 'expunged' sub-directory can have have content when a folder that belongs to you but contains files that don't is deleted

Manual Method 

  • You can manually remove :
 $ rm -rf ~/.local/share/Trash/files/*
 $ rm -rf ~/.local/share/Trash/info/*
 $ rm -rf ~/.local/share/Trash/expunged/*

trash-cli package

  • Recommended method
  • Install
$ sudo apt-get install -y trash-cli
  • Foe arch install from the AUR 
  • List trash contents
$ trash-list
2013-07-20 15:06:23 /home/lclark/tmp/experimental

  • Empty trash
 $ trash-empty
  • List contents and its empty
 $ trash-list
  • Enjoy !

Shuf - generate random permutations on Linux



Its always great to find a new Linux command that I have never heard of but is really useful. The command 'shuf' comes in to this category. It should be installed by default on most Linux systems as its part of the GNU Coreutils.

I wanted to generate random MAC address's from a text file which was simple with :

$ shuf -n 2 MACList | sort | uniq
00:A0:C8:14:C2:22
00:A0:C9:14:C8:29


Examples of using 'Shuf'

  • Generate 10 random numbers between 1 and 999

$ shuf -i 1-999 -n 10
182
588
696
841
574
204
173
641
958
289

  • Output results to a file
$ shuf -i 1-999 -n 10 -o results.txt
$ head -n 1 results.txt
749


  • Sort results and ensure numbers are unique

$ shuf -i 1-999 -n 10 | sort | uniq
101
142
298
396
508
564
793
870
923
95

  • Generate random words from Linux dictionary
$ shuf -n 2 /usr/share/dict/words
universe's
phosphorus


























Useful bashrc setup for command line efficiency



I find the following config items in my .bashrc file to use any system efficiency when I set it up.

Include home bin within PATH


  • I create a bin directory in the home directory to add in all my executable programs

# Include bin dir to PATH
export PATH=/home/cwishaw/bin:$PATH


Password Generator

 

  • I find it useful to be able to generate random passwords quickly :

GeneratePwd(){ < /dev/urandom tr -dc A-Z-a-z-0-9 | head -c${1:-12};echo;}
  • This will generate a random password of 12 chacacters :
$ GeneratePwd
JTRKjlXyAIGS

 

Extract files quickly


  • The following command will extract the most common file types without having to memorize all the syntax
Extract () {
   if [ -f $1 ] ; then
       case $1 in
           *.tar.bz2)   tar xvjf $1    ;;
           *.tar.gz)    tar xvzf $1    ;;
           *.bz2)       bunzip2 $1     ;;
           *.rar)       unrar x $1       ;;
           *.gz)        gunzip $1      ;;
           *.tar)       tar xvf $1     ;;
           *.tbz2)      tar xvjf $1    ;;
           *.tgz)       tar xvzf $1    ;;
           *.zip)       unzip $1       ;;
           *.Z)         uncompress $1  ;;
           *.7z)        7z x $1        ;;
           *)           echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }



Move up directories


  • The following command enabled you to quickly move up directories

Up(){
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}


Include bash_aliases if exists

 

  • I prefer to manage all my aliases in a separate file as its easier to maintain.

if [ -f ~/.bash_aliases ];
 then
        . ~/.bash_aliases
fi


  •  The bash_aliases can have all your alias definitions of the format :

alias pacman='sudo pacman'
alias vi='vim'




Remove comments from a file on Linux with sed


You can quickly remove comments from a file on Linux using sed. For example if the comment starts with #.

$ sed '/^\#/d' filewithcomments.txt > filewithcomments.tmp
$ mv filewithcomments.tmp filewithcomments.txt
$ rm filewithcomments.tmp

Linux Script utility to record output on terminal


To record the output on the terminal you can use the linux command line tool 'script'.

Example

  • Note : I use 'f' to force the write

$ script -f
Script started, file is typescript


$ ls -lh
total 12K
-rw-r--r-- 1 cwishaw users   99 Sep 15 21:36 hdmi.txt
-rw-r--r-- 1 
cwishaw users  223 Oct 20 16:13 typescript
 

$ exit
Script done, file is typescript

  • Now you can view the output from a file called 'typescript'
$ cat typescript
Script started on Sun 20 Oct 2013 04:13:49 PM PDT
 $ ls -lh
total 12K
-rw-r--r-- 1 
cwishaw users   99 Sep 15 21:36 hdmi.txt
-rw-r--r-- 1 
cwishaw users  223 Oct 20 16:13 typescript

  • This can be useful in many  situations where is great to have a record of what you have typed

Binary Characters

  • The typeset file _can contain binary characters for example from a telnet session
  • We can use the Linux 'tr' command to delete these characters :

$ tr -cd '\11\12\15\40-\176' < typescript > typescript_ASCII.txt

  • The command allows the following Octal characters to remain from the ASCII table :
    • TAB - 11
    • LF (linefeed) - 12
    • CR (carriage return) - 15
    • ASCII printable characters : 40 -> 176

CrunchBang Linux #! avoid password for sudo




To avoid changing the password for the sudo user on CrunchBang Linux #! the first step is to Enable the #! root account

Once you are logged on as root run the following command :

# visudo

At the bottom of the file enter :

cwishaw    ALL=(ALL:ALL) NOPASSWD: ALL

Note - change 'cwishaw' with your user account.

Save the file and exit. Login again as the user. test sudo and you should be password free. Have fun!

How to enable the default password on #! CrunchBang Linux



I really like #! CrunchBang Linux but one of the things I could not find out initially was what the root password was set to. During installation the root password is set to something random and the recommended approach would be to use sudo. That said I like to have root access!

To enable the root password :

$ sudo passwd root

Type the password twice when prompted. Then change to root :

$ su root

Have fun :)