Bash Index : H - The 'H' Bash commands : description, flags and examples

htpasswd

Usage :

Manage user files for basic authentication
/usr/bin/htpasswd comes from apache2-utils.

htpasswd [options] passwordFile user

Flags :

Flag Usage
-B use Bcrypt encryption for passwords. This is currently considered to be very secure.
-C cost with -B only : set the computing time (i.e. "cost") used by the bcrypt algorithm (higher is more secure but slower).
Range of values for cost : 4 to 31. Defaults to 5.
-c passwordFile create passwordFile (or overwrites it if already exists)
Conflicts with -n
-D Delete user from the specified passwordFile
-i Read the password from stdin without verification (i.e. entered only once). For script usage.
-m use MD5 encryption for passwords. This is the default since version 2.2.18
-n Display result to standard output rather than updating a file
Conflicts with -c
-p use plaintext passwords

Example :

First basic test, with output to screen :

htpasswd -n bob
New password: taptaptap
Re-type new password: taptaptap
bob:$apr1$Qn/z8Zl5$M7fw22yJmc5RUHUiq91Lo0

Now, we create the password file :

htpasswd -c ./passwordFile kevin
New password: taptaptap
Re-type new password: taptaptap
Adding password for user kevin
cat passwordFile
kevin:$apr1$fwPtbYsp$kT7VBUu8AXa9Pjqaj8dNt/

Append to an existing password file :

htpasswd ./passwordFile stuart
New password: taptaptap
Re-type new password: taptaptap
Adding password for user stuart
cat passwordFile
kevin:$apr1$fwPtbYsp$kT7VBUu8AXa9Pjqaj8dNt/
stuart:$apr1$Qj95O6iq$6pbjcgQYT2ou3TkPmlBVY0

Encrypt password with Bcrypt (and append to password file) :

htpasswd -B ./passwordFile stuart
New password: taptaptap
Re-type new password: taptaptap
Updating password for user stuart
cat passwordFile
kevin:$apr1$fwPtbYsp$kT7VBUu8AXa9Pjqaj8dNt/
stuart:$2y$05$rFegBEShO0WqXg9yeigKNuzygHZJPgAHo64GC3eF24Lv5/tIic3Ii

Plain-text password (i.e. no encryption) :

htpasswd -p ./passwordFile bob
Warning: storing passwords as plain text might just not work on this platform.
New password: secret
Re-type new password: secret
Adding password for user bob
cat passwordFile
kevin:$apr1$fwPtbYsp$kT7VBUu8AXa9Pjqaj8dNt/
stuart:$2y$05$rFegBEShO0WqXg9yeigKNuzygHZJPgAHo64GC3eF24Lv5/tIic3Ii
bob:secret

Get password from standard input :

echo sEcReTpAsSwOrD | htpasswd -pin bob
Warning: storing passwords as plain text might just not work on this platform.
bob:sEcReTpAsSwOrD

host

Usage :

DNS lookup utility

Flags :

Flag Usage
-a query all. Equivalent to -t any -v
-C zoneName Try to display the SOA records of zone zoneName
-t type select records having the specified type, which can be : CNAME, NS, SOA, SIG, KEY, AXFR, ...
-v verbose
-w wait for a reply forever (actually, the maximum number of seconds that can be written in an int)
-W timeoutSeconds timeout after timeoutSeconds

Example :

Basic usage :

Forward DNS lookup (name into IP) :
host www.google.com
www.google.com has address 108.177.119.99
www.google.com has address 108.177.119.104
www.google.com has address 108.177.119.103
www.google.com has address 108.177.119.147
www.google.com has address 108.177.119.106
www.google.com has address 108.177.119.105
www.google.com has IPv6 address 2a00:1450:4013:c00::6a
Reverse DNS lookup (IP to name) :
host 8.8.8.8
8.8.8.8.in-addr.arpa domain name pointer google-public-dns-a.google.com.
List DNS servers of a domain :
host -t ns google.com
google.com name server ns4.google.com.
google.com name server ns3.google.com.
google.com name server ns2.google.com.
google.com name server ns1.google.com.
DNS lookup on a specific nameserver :
host www.google.com ns4.google.com
Using domain server:
Name: ns4.google.com
Address: 216.239.38.10#53
Aliases:

www.google.com has address 216.58.208.228
www.google.com has IPv6 address 2a00:1450:4007:80e::2004
Same for CNAME record :
host -t cname mail.google.com
mail.google.com is an alias for googlemail.l.google.com.
Display the SOA records of the specified zone :
host -C google.com
Nameserver 216.239.32.10:
	google.com has SOA record ns3.google.com. dns-admin.google.com. 168998048 900 900 1800 60
Nameserver 216.239.38.10:
	google.com has SOA record ns1.google.com. dns-admin.google.com. 168998048 900 900 1800 60
Nameserver 216.239.34.10:
	google.com has SOA record ns3.google.com. dns-admin.google.com. 168998048 900 900 1800 60
Nameserver 216.239.36.10:
	google.com has SOA record ns1.google.com. dns-admin.google.com. 168998048 900 900 1800 60
Find all about domain records :
host -t any -v google.com
OR : host -a google.com
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13433
;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;google.com.			IN	ANY

;; ANSWER SECTION:
google.com.		157	IN	MX	50 alt4.aspmx.l.google.com.
...

hwclock

Usage :

Query and set the hardware clock (RTC) as root only.

Flags :

Flag Usage
--adjust Add or subtract time from the Hardware Clock to account for systematic drift since the last time the clock was set or adjusted. (details)
-r, --show read the Hardware Clock and print the time on Standard Output. The time shown is always in local time
-w, --systohc write the current System Time into the Hardware Clock

Example :

To set the hw clock manually (effective upon next reboot) :

hwclock --set --date="09/21/2005 14:23:23"

Force system clock to synchronize on the hardware clock :

hwclock --hctosys, then check with date.

Synchronize the hardware clock with the system date (after setting it with date --set="...") :

hwclock --systohc

htop

htop displays a list of hotkeys on the terminal bottom line. If some of these keys can not be used because they have been re-binded (see GNU screen key binding), you can still mouse-click the corresponding menu options .

Memory (source) :

VIRT
virtual size of a process : the memory it is actually using + memory it has mapped into itself (for instance the video card's RAM for the X server) + files on disk that have been mapped into it (most notably shared libraries) + memory shared with other processes. VIRT represents how much memory the program is able to access at the present moment.
RES
resident size : an accurate representation of how much actual physical memory a process is consuming. (This also corresponds directly to the %MEM column)
SHR
this indicates how much of the VIRT size is actually shareable memory or libraries. In the case of libraries, it does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES

Colors (source) :

F1 or h will show you the key. But for reference, the default colors are :
CPU Memory
blue Low priority threads Buffers
green Normal priority threads Used memory
red Kernel threads
yellow Cache

Usage :

Output the first part of files
head (options...) myFile

Flags :

Flag Usage
-c n --bytes=n Print the first n bytes of myFile.
If n has a leading -, print all except the last n bytes of myFile.
-n n --lines=n Print the first n lines of myFile.
If n has a leading -, print all except the last n lines of myFile.

Example :

Be careful with the short versions of commands :

head -n numberOfLines myFile can be shortened into head -numberOfLines myFile. Let's test this :

tmpFile=$(mktemp tmp.XXXXXXXX); for i in {1..5}; do echo "This is line #$i" >> $tmpFile; done; head -n 2 $tmpFile; rm $tmpFile
Outputs the 2 first lines :
This is line #1
This is line #2
tmpFile=$(mktemp tmp.XXXXXXXX); for i in {1..5}; do echo "This is line #$i" >> $tmpFile; done; head -2 $tmpFile; rm $tmpFile
Outputs the 2 first lines as well :
This is line #1
This is line #2
tmpFile=$(mktemp tmp.XXXXXXXX); for i in {1..5}; do echo "This is line #$i" >> $tmpFile; done; head -n -2 $tmpFile; rm $tmpFile
Outputs everything but the 2 last lines :
This is line #1
This is line #2
This is line #3
There is no short version for this one.

history

Usage :

Display or alter the command history list.
For inline help on this command :
Other usages :
!n
re-launch the command number n returned by history
!!
re-launch the last shell command (i.e. the previous command). It's particularly useful when you forgot to sudo a command :
ls -l /root
ls: cannot open directory '/root': Permission denied
sudo !!
sudo ls -l /root
[sudo] password for stuart: taptaptap 
...
[contents of /root]
...
!foo
re-launch the most recent command starting with foo (parsing history list from bottom to top, be sure to give enough characters in foo)
!xyz:p
print the command instead of executing it
!?foo
re-launch the most recent command containing foo

Flags :

Flag Usage
(none) Display the command history list with line numbers
Lines listed with a * have been modified.
n Display the n latest lines of history
-d n delete the history entry at line n
  • can delete only 1 entry at a time
  • this command itself will be recorded in the history
  • editing $HISTFILE may work but keep in mind commands are written there upon session closing

Example :

How to display the timestamp of each history entry ? (source)

Simply add to ~/.bash_profile (or ~/.bashrc) :
HISTTIMEFORMAT="%F %T "
Depending on system settings :
  • Either commands are ALREADY recorded with their own timestamp, and this only enables displaying it
  • Or, all previous commands are displayed with a timestamp being the date/time when this was enabled
timestamp specification (details : man 3 strftime) :
Flag Usage Example
%F Date in ISO 8601 (1, 2, 3) format, equivalent to %Y-%m-%d 2015-06-03
%T Time in 24-hour notation, equivalent to %H:%M:%S 16:34:51

hd

Usage :

hexdump : ASCII, decimal, hexadecimal, octal dump

Flags :

Flag Usage
-n length Return only length bytes of input
-s offset Skip offset bytes from the beginning of the input

hdparm

Usage :

Get or set SATA / IDE device parameters. This comes with the hdparm package.

Flags :

Flag Usage
-I (capital "i") Request Identification info directly from the drive.
-i Display the device information retrieved by the kernel at boot time. This information may have been altered since then.
-M Get/set Automatic Acoustic Management (AAM) setting. This is not supported anymore by Seagate and Western Digital drives.
-r Get/set read-only flag for the device. When set, Linux disallows write operations on the device.
This command takes place at the hardware level, it instructs the hard drive itself to refuse write operations (not supported by all drives). Due to data buffering, after performing a write operation, the kernel will assume data has been written, whereas it is not, which may lead to data loss.
(not been able to make this work so far ...) Details : 1, 2
-T Perform timings of cache reads for benchmark and comparison purposes.
This displays the speed of reading directly from the Linux buffer cache without disk access. This measurement is essentially an indication of the throughput of the processor, cache, and memory of the system under test.
For meaningful results, this test should be executed on an inactive system (no other process) and repeated several times.
-t Perform timings of device reads for benchmark and comparison purposes.
This displays the speed of reading through the buffer cache to the disk without any prior caching of data (the buffer cache is flushed prior to measuring). This measurement is an indication of how fast the drive can sustain sequential data reads under Linux, without any filesystem overhead.
For meaningful results, this test should be executed on an inactive system (no other process) and repeated several times.

To get / set / unset a parameter :

  • Get : hdparm -X device
  • Set (enable) : hdparm -X1 device
  • Unset (disable) : hdparm -X0 device

Example :

Read drive information (manufacturer, model, S/N, firmware, ...) :

hdparm -I /dev/sda

This is a capital i.