Linux

Quick bash script to check if a volume is mounted

A quick and dirty little bash script to check if a mount point is active or not.

if grep -qs '/mnt/drive1' /proc/mounts; then
    echo "It's mounted."
else
    echo "It's not mounted."
fi

You can easily change the code to automatically mount the volume (or unmount) as per your needs.

Installing Glances on Debian

Glances is a cross-platform curses-based system monitoring tool written in Python. It shows a large amount of information such as cpu usage, memory usage, hardware monitoring, battery monitoring and much more.

Install the required dependencies:

sudo apt-get install python-dev python-jinja2 python-psutil python-setuptools hddtemp python-pip lm-sensors
sudo pip install batinfo pysensors

If you are running on actual hardware (ie: not a vps like many of my servers), you should detect the sensors for that part of the monitoring:

sudo sensors-detect

Now install the actual Glances program:

sudo pip install Glances

And finally, you can run it by simply:

sudo glances

To upgrade, you can do

pip install --upgrade glances

There are also a number of other optional dependencies you can install:

bottle (for Web server mode)

py3sensors (for hardware monitoring support) [Linux-only]

hddtemp (for HDD temperature monitoring support) [Linux-only]

batinfo (for battery monitoring support) [Linux-only]

pymdstat (for RAID support) [Linux-only]

pysnmp (for SNMP support)

zeroconf and netifaces (for the auto discoverer mode)

influxdb (for the InfluxDB export module)

statsd (for the StatsD export module)

pystache (for the action script feature)

docker-py (for the Docker monitoring support) [Linux-only]

How to clear DNS cache in various OS

There are a variety of reasons why (updated a DNS record, issues with resolver, etc) but sometimes a good diagnostic step is to clear out the dns cache.

Each operating system (and even versions / distributions in some cases) have a different method

OSX 10.10 > sudo discoveryutil udnsflushcaches

OSX 10.9 > dscacheutil -flushcache; sudo killall -HUP mDNSResponder

OSX 10.7 – 10.8 > sudo killall -HUP mDNSResponder

OSX 10.5 – 10.6 > sudo dscacheutil -flushcache

Windows > ipconfig /flushdns

Linux - There are many different distros and versions here, but one of the following should work > /etc/init.d/named restart

/etc/init.d/nscd restart

service nscd reload

sudo /etc/init.d/dnsmasq restart

service dnsmasq restart

How to configure dropbox for multiple users on a headless Debian / Ubuntu server

There are numerous guides around to configure Dropbox on a headless Debian or Ubuntu server but it seems everytime I go to do one, I can’t find the nice guide I usually use or a website is down when I look for alternatives. So here is my quick guide for myself and anyone else that might stumble upon this page :)

For 32bit:

wget -O dropbox.tar.gz https://www.dropbox.com/download?plat=lnx.x86

tar xzf dropbox.tar.gz

./.dropbox-dist/dropboxd

For 64bit:

wget -O dropbox.tar.gz https://www.dropbox.com/download?plat=lnx.x86_64

tar xzf dropbox.tar.gz

./.dropbox-dist/dropboxd

At this point, dropbox will complain about not being linked to an account and output a web address to follow, do that and login to your account and it will be linked.

Kill the dropbox that you started manually (requires pgrep, if you don’t have it you can use your favorite method):

kill $(pgrep dropbox)

You can repeat the above steps for each user you want to have with their own dropbox.

Edit: note this does not work on newer versions of Debian (aka ones with systemd). I usually just create a crontab for reboot to run dropbox as that user, haven’t gotten around to sorting out a systemd service file yet.

Now as root, either download this startup script or copy & paste the following code to /etc/init.d/dropbox and edit the DROPBOX_USERS line as needed:

# dropbox service
DROPBOX_USERS="user1 user2"

DAEMON=.dropbox-dist/dropboxd

start() {
    echo "Starting dropbox..."
    for dbuser in $DROPBOX_USERS; do
        HOMEDIR=`getent passwd $dbuser | cut -d: -f6`
        if [ -x $HOMEDIR/$DAEMON ]; then
            HOME="$HOMEDIR" start-stop-daemon -b -o -c $dbuser -S -u $dbuser -x $HOMEDIR/$DAEMON
        fi
    done
}

stop() {
    echo "Stopping dropbox..."
    for dbuser in $DROPBOX_USERS; do
        HOMEDIR=`getent passwd $dbuser | cut -d: -f6`
        if [ -x $HOMEDIR/$DAEMON ]; then
            start-stop-daemon -o -c $dbuser -K -u $dbuser -x $HOMEDIR/$DAEMON
        fi
    done
}

status() {
    for dbuser in $DROPBOX_USERS; do
        dbpid=`pgrep -u $dbuser dropbox`
        if [ -z $dbpid ] ; then
            echo "dropboxd for USER $dbuser: not running."
        else
            echo "dropboxd for USER $dbuser: running (pid $dbpid)"
        fi
    done
}


case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  restart|reload|force-reload)
    stop
    start
    ;;

  status)
    status
    ;;

  *)
    echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}"
    exit 1

esac

exit 0

After that, you need to make it executable and add it to system startup:

sudo chmod +x /etc/init.d/dropbox

sudo update-rc.d dropbox defaults

Repeat the starting, linking & killing steps above for all of the accounts you want to link. Once that is done run the dropbox init script you copied above:

sudo /etc/init.d/dropbox start

And now you should be done. If you want to confirm that multiple dropbox accounts are running you can do:

ps aux | grep -i dropbox

Quick guide to UFW - Uncomplicated FireWall

Here is a quick guide on using UFW (Uncomplicated FireWall) under linux

A good first step is to do the following

sudo ufw default deny

which will set the default action to denying everything

If you are connection from remote, you will want to allow ssh otherwise it will create a nasty embarassing situation when you can’t get into your machine :)

sudo ufw allow ssh

then you can enable UFW by:

sudo ufw enable

You will now have a very basic firewall setup to deny everything except ssh. Not overly helpful if you are hosting anything else on the system. The rest of the points will be in quick format and in the future I may update and clean up the info below

Disable UFW

sudo ufw disable

Enable UFW

sudo ufw enable

Get current UFW status

sudo ufw status

Allow port 80 (http)

sudo ufw allow 80

Deny a certain port

sudo ufw deny port <port number>

Deny a certain ip from entire host

sudo ufw deny from <ip address>

Block an ip from a port

sudo ufw deny from <ipaddress> to port <port number>

Advanced blocking of multiple ips from a port

sudo ufw deny from 10.0.0.1/24 to any port 22

To open up port range 64000-65000 on udp

ufw allow proto udp to any port 64000:65000

To open up port range 64000-65000 on tcp

ufw allow proto tcp to any port 64000:65000

The plain HTTP request was sent to HTTPS port - error with nginx and phpmyadmin and others

This error drove me nuts last night and tonight, trying to sort out what the issue was. Specifically in my case, it was with phpmyadmin running on a secure (HTTPS) site. ::

The plain HTTP request was sent to HTTPS port

If you get this error, you are missing the following line from your fastcgi setup for your ssl vhost:

fastcgi_param HTTPS on;

To show that in context, you want something similar to: ::

location ~ \.php$ {
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/securehost$fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}