Category: Bash

Bash tips and tricks

Bash, Linux

Listing all email addresses in a file with grep

The following grep command will list all email addresses from a text file and sort them per names without allowing for repetition(-u unique) The following grep command will list all email addresses from a text file and sort them per domain without allowing for repetition(-u unique)

Bash, Linux

Force reboot a remote Linux server

Introduction: After having tried to do a reboot of a remote Linux server via the command reboot which had no effect, I tried to find a command that would force the server to reboot immediately. I found the commands that do exactly that at: https://major.io/2009/01/29/linux-emergency-reboot-or-shutdown-with-magic-commands/ Commands: echo 1 > /proc/sys/kernel/sysrq echo b > /proc/sysrq-trigger This …

Bash, Linux, MySQL

Display MySQL databases types in bash

Based on the site: http://stackoverflow.com/questions/213543/how-can-i-check-mysql-engine-type-for-a-specific-table Here is a bash command that will display the databases types(innoDB or MyISAM) of all the MySQL databases except for the defaults ones(mysql, etc) for i in $(mysql -u root -p -e “show databases;” | egrep -v ‘Database|information_schema|mysql|performance_schema’); do echo “——————–$i——————–“; mysql -u root -p -e “use $i; show table …

Bash, Linux, Security

Forcing pam users to use only FTPS and block SFTP/SSH

Introduction: I needed to force certain PAM users(configured in /etc/passwd) to use FTPS and block them from using SSH or SFTP. Here is a solution I found in: http://askubuntu.com/questions/93411/simple-easy-way-to-jail-users Solution: Add to /etc/shells a new shell: vim /etc/shells Add one line: /bin/false Save. For each user you want to deny ssh/sftp, change the user’s shell: …

Bash, Linux

Displaying the list of all ‘at’ jobs and their respective commands

Introduction: The command atq gives me the list of at jobs waiting to be executed and their execution times. Each line starting with the job number. The command at -c JobNumber gives me the content of the job including the environment variables. What I wanted is a command that would give me the list of …

Bash, Linux

Useful ps options

Here are some (growing) tricks in order to get the maximum of the PS command: I set the command’s options in an alias to simplify the running of it. alias psa=’ps –headers axf -o pid,ppid,pri,state,user:15,group:15,nlwp,%cpu,%mem,rss,vsz,maj_flt,time,start,comm’ # One advantage of this above command is that it displays the username in its full length even if it’s …

Bash, Linux, Monitoring

Verifying the validity of an NFS mount

Introduction: Every now and then if an NFS mount is no more connected to the server or something goes wrong with the NFS connection, running the command ‘ls mountpoint’ hangs the terminal till I press CTRL-C. So I tried to figure out a script that will be run as cron job and will tell me …

Bash, Linux, Security

Recursively delete files securely: shredding

Description: Every administrator should know that when we delete a file on the hard disk, almost nothing is really deleted. The space used by the files is simply put back on the list of ‘free to use‘ space in the filesystem and will no more appear in the directory listing. This means forensic tools can …

Bash, Linux

Using SS tool for network troubleshooting

Introduction: The following article is been copied completely 1 to 1 (full plagiat!!)from the following site inn order to be able to refer to it here in case the article disappears from Internet access or moves location. http://www.linux-magazine.com/Issues/2015/181/Querying-Sockets-with-ss Linux Magazine. Article from Issue 181/2015 Author(s): Chris Binnie The unassuming ss utility is easy to understand …

Bash, Linux, Monitoring, Security

Preventing a bash script from running concurrently

Introduction: In order to prevent a bash script instance from running more than once concurrently, here is a small tip on how to write the script. Script template: #!/bin/bash # Prevents that an instance of the script starts while another instance of it is still running scriptname=$(basename $0) lockfile=”/tmp/${scriptname}.lock” if [ -e $lockfile ]; then …

Bash, Linux

Finding the absolute path of a running script

In bash scripts we often need to know in which directory the running script is found especially when the script is in the $PATH and may be occurring in multiple places: Here is a reliable way to find it out: Based on this site: http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself # Absolute path to this script. /home/user/bin/foo.sh SCRIPT=$(readlink -f $0) …

Bash, Linux, Security

Encrypt a password with different encryption methods

Situation: I happen to have configured a Linux system with MySQL database and wanted to enter a password in the password field in the DB. Here is a way I found. This will output in the terminal the encrypted string you can then enter directly in the password field via phpmyadmin or mysql client. Note: …

Bash, Linux

Downloading tar.gz files from the Linux command line

Sometimes we need to download a file from Internet using the bash command line. here are some suggestions: Using WGET: wget {URL} eg. wget https://my.server.com/downloads/file.tar.gz Using CURL: (Fancy progress info given as the download progresses and will unpack it at the same time) curl -L –progress {URL} | tar xz eg. curl -L –progress https://my.server.com/downloads/file.tar.gz …

Bash, Linux, VMWare, XEN

Extending dynamically Linux RAMs in VMWare VM without rebooting

Situation: Need to raise the amount of RAM in a VMWare VM without rebooting. Solution: – In VMWare interface: Raise the amount of RAM for the VM – In the Linux VM: Run the following script: #!/bin/bash # This script enables in system the unrecognized RAMs deleteline () { echo -ne $dellineup } ### check …

Bash, Linux

Copying all files including hidden files in Linux command

PROBLEM: If you use the command cp or mv on hidden files you will notice that the hidden files won’t get ‘seen’ and therefore not copied or moved. The problem doesn’t belong to cp or mv but to bash. Bash doesn’t include the hidden files in the globbing expansion. for example: mkdir ~/temp1 ~/temp2 touch …

Apache, Bash, Linux, Monitoring, NGinX

Monitoring latency time of http requests

Here is a simple but useful command which shows the latency time of http requests. You can adjust the delay between repeats as well as the URL being queried. Reference: http://www.shellhacks.com/en/Check-a-Website-Response-Time-from-the-Linux-Command-Line host=”www.google.de”; delay=5; while true ; do echo -n “Response time for http://$host:” ;curl -s -w %{time_total}\\n -o /dev/null http://$host ;sleep $delay; done Results: Response …

Bash, Linux

Resolve the IP from a hostname

As a Linux administrator I often need to resolve hostnames into IPs specially in scripts. Using the command ‘host’ the sytem will resolve the IP only through the resolver libraries omitting to look into the /etc/hosts file. The same is true for the command ‘nslookup’ and ‘dig’. A good trick is to use the command …

Bash, Linux

Changing keyboard layout in Ubuntu/Debian Linux comand line

Ref: http://askubuntu.com/questions/209597/how-do-i-change-keyboards-from-the-command-line You can find all the different keymaps in the following location: /usr/share/keymaps/i386/ or /usr/share/kbd/keymaps/i386/ To change the keyboard layout (e.g. to German) in the Linux command line, type the following command: loadkeys de For X: setxkbmap de To make these changes system wide, assuming you’re using Ubuntu, you can use the following: sudo …

Bash, Linux

Unlocking dpkg database

Under Debian Squeeze I ran the command dpkg -i bash_4.1-3+deb6u1_amd64.deb and got the following error message: dpkg: status database area is locked by another process Solution: 1 – make sure you are not already runnning any package administration program in another bash session like with dpkg or apt-get or aptitude 2 – If no other …

Bash, Linux

Bash bug ‘Shellshock’ Debian Squeeze packages

For those who still have Debian Squeeze and wonder where to find the fixed Bash Debian package for the dangerous bash bug (http://www.bbc.co.uk/news/technology-29361794)you can find it here: 64 Bit: ftp://ftp.fr.debian.org/debian/pool/main/b/bash/bash_4.1-3+deb6u2_amd64.deb 32 Bit: ftp://ftp.fr.debian.org/debian/pool/main/b/bash/bash_4.1-3+deb6u2_i386.deb Want to test your Bash to see if it is fixed? Run the command: test=”() { echo Hello; }; echo Buggy” bash …