One of the most commonly used Linux system administration tools is chown, which is part of the coreutils package. It is used to change the user and/or group ownership of a given file or directory. Something to be aware of this tool is, it doesn’t change the ownership of symbolic links, as shown below –

root@linubuvma:/tmp# touch test
root@linubuvma:/tmp# ls -l test
-rw-r--r-- 1 root root 12 Dec 20 08:01 test
root@linubuvma:/tmp# ln -s test sltest
root@linubuvma:/tmp# ls -l sltest
lrwxrwxrwx 1 root root 4 Dec 20 08:01 sltest -> test
root@linubuvma:/tmp# chown daniel:daniel sltest
root@linubuvma:/tmp# ls -l sltest
lrwxrwxrwx 1 root root 4 Dec 20 08:01 sltest -> test

The reason this doesn’t work is in the man page for chown – symbolic links named by arguments are silently left unchanged unless -h is used.” By simply running chown on symbolic link without ‘-h’ option, you are changing the ownership of the target. The ‘-h’ option affects symbolic links instead of any referenced file.

root@linubuvma:/tmp# chown -h daniel:daniel sltest

root@linubuvma:/tmp# ls -l sltest
lrwxrwxrwx 1 daniel daniel 4 Dec 20 08:01 sltest -> test

Though not portable, in some distros

 chown -R 

will recursively change the owernship of all files, including symbolic link files and directories. In my case, ‘chown -R /path/to/file’ works for GNU chown which is part of the ‘GNU coreutils 8.21’ package on Ubuntu 14.04.

How to record your ssh session using screen.

Per the man page – “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)”.
Screen is most commonly used to create multiple sessions to remote hosts within a single terminal window or even run multiple commands locally without leaving your shell terminal. For instance, you could be tailing the log file in one session, then run a long process, then ssh into other machine etc. all within a single window.

Screen is the go to tool when setting up a remote connection, such as ssh, and you want to continue your work at any time or from any other host without worrying of a dropped connection.

In this post, I will show you how you can record your bash session.

Installation –

yum install screen        (Debian/Ubuntu)
apt-get install screen    (Redhat/CentOS)

My local environment and the remote host I am sshing to –

daniel@linubuvma:/tmp$ screen -v
Screen version 4.01.00devel (GNU) 2-May-06
daniel@linubuvma:/tmp$ uname -r
3.13.0-106-generic
daniel@linubuvma:/tmp$ cat /etc/issue
Ubuntu 14.04.5 LTS \n \l

daniel@linubuvma:/tmp$ ssh ns2 'uname -r ; cat /etc/issue'
2.6.32-642.6.1.el6.x86_64
CentOS release 6.8 (Final)
Kernel \r on an \m

The ‘-L’ option of screen is used to record your session, the session log is automatically saved in a file named ‘screenlog.n’ in your current directory.

daniel@linubuvma:/tmp$ ls
config-err-hbzs5e  one          ssh-4yheApHRgMBF  ssh-RK7GpeFuzUB8  VMwareDnD    vmware-root-2347660412
gpg-kZux7q         screenlog.0  ssh-BBblvGtb5284  vmware-daniel     vmware-root
daniel@linubuvma:/tmp$ free -m
             total       used       free     shared    buffers     cached
Mem:          3946       2489       1457          6        547       1031
-/+ buffers/cache:        911       3035
Swap:         4092          0       4092
daniel@linubuvma:/tmp$ exit
[screen is terminating]
daniel@linubuvma:/tmp$ 

The whole bash session will be logged in screenlog.0 in this case –

daniel@linubuvma:/tmp$ cat screenlog.0 
daniel@linubuvma:/tmp$ ls
config-err-hbzs5e  one          ssh-4yheApHRgMBF  ssh-RK7GpeFuzUB8  VMwareDnD    vmware-root-2347660412
gpg-kZux7q         screenlog.0  ssh-BBblvGtb5284  vmware-daniel     vmware-root
daniel@linubuvma:/tmp$ free -m
             total       used       free     shared    buffers     cached
Mem:          3946       2489       1457          6        547       1031
-/+ buffers/cache:        911       3035
Swap:         4092          0       4092
daniel@linubuvma:/tmp$ exit
exit
daniel@linubuvma:/tmp$ 

Recording your session of an ssh connection to a remote host is also similar, with ‘-L’ option followed by the command to ssh to remote host.
Option -fn (with no flow-control)
Option -t (title bar name) in this case ‘practice’.

daniel@linubuvma:/tmp$ screen -fn -t practice -L  ssh ns2
Last login: Tue Dec 27 09:46:10 2016 from linubuvma.home.net

[daniel@kauai ~]$ hostname -f
kauai.example.net
[daniel@kauai ~]$ uptime
 10:08:18 up 18 days, 10:02, 14 users,  load average: 0.19, 0.49, 0.64
[daniel@kauai ~]$ exit
[screen is terminating]


daniel@linubuvma:/tmp$ cat screenlog.0 
Last login: Tue Dec 27 09:46:10 2016 from linubuvma.home.net
[daniel@kauai ~]$ hostname -f
kauai.example.net
[daniel@kauai ~]$ uptime
 10:08:18 up 18 days, 10:02, 14 users,  load average: 0.19, 0.49, 0.64
[daniel@kauai ~]$ exit
logout
Connection to ns2 closed.
daniel@linubuvma:/tmp$ 

Additional resources –

https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
https://linux.die.net/man/1/screen

Randomly ordering files in a directory with python

I have a playlist file which contains audio files to play. The audio player unfortunately plays the music files in a sequential order, in whatever order they are listed in the playlist file. So occasionally I have to regenerate the playlist file to randomize the audio files order. Here is a simple script that I had to write for this purpose, the core component is the random.shuffle(list) python function –

Create script file as shuffle_files.py –

#!/usr/bin/env python

import os
import random
import sys

music_files=[]

if len(sys.argv) != 2:
  print "Usage:", sys.argv[0], "/path/directory"
else:
  dir_name=sys.argv[1]
  if os.path.isdir(dir_name):
    for file_name in os.listdir(dir_name):
      music_files.append(file_name)
  else:
    print "Directory", dir_name, "does not exist"
    sys.exit(1)
# shuffle list
random.shuffle(music_files)
for item in music_files:
  print os.path.join(dir_name,item)

Run the script by providing a path to a directory with files. Each iteration should list the files in the directory in a different order.
Note – the script does not recurse into the directories, it can be easily modified with os.walk if necessary.

root@svm1010:/home/daniel/scripts# python shuffle_files.py /opt/iotop/iotop
/opt/iotop/iotop/setup.py
/opt/iotop/iotop/README
/opt/iotop/iotop/iotop
/opt/iotop/iotop/iotop.8
/opt/iotop/iotop/NEWS
/opt/iotop/iotop/iotop.py
/opt/iotop/iotop/PKG-INFO
/opt/iotop/iotop/THANKS
/opt/iotop/iotop/sbin
/opt/iotop/iotop/setup.cfg
/opt/iotop/iotop/ChangeLog
/opt/iotop/iotop/.gitignore
/opt/iotop/iotop/COPYING


root@svm1010:/home/daniel/scripts# python shuffle_files.py /opt/iotop/iotop
/opt/iotop/iotop/PKG-INFO
/opt/iotop/iotop/COPYING
/opt/iotop/iotop/iotop
/opt/iotop/iotop/setup.cfg
/opt/iotop/iotop/NEWS
/opt/iotop/iotop/README
/opt/iotop/iotop/.gitignore
/opt/iotop/iotop/setup.py
/opt/iotop/iotop/THANKS
/opt/iotop/iotop/iotop.py
/opt/iotop/iotop/ChangeLog
/opt/iotop/iotop/iotop.8
/opt/iotop/iotop/sbin


root@svm1010:/home/daniel/scripts# python shuffle_files.py /opt/iotop/iotop
/opt/iotop/iotop/THANKS
/opt/iotop/iotop/setup.py
/opt/iotop/iotop/NEWS
/opt/iotop/iotop/README
/opt/iotop/iotop/iotop.8
/opt/iotop/iotop/.gitignore
/opt/iotop/iotop/ChangeLog
/opt/iotop/iotop/sbin
/opt/iotop/iotop/PKG-INFO
/opt/iotop/iotop/iotop
/opt/iotop/iotop/COPYING
/opt/iotop/iotop/iotop.py
/opt/iotop/iotop/setup.cfg

Reference – https://docs.python.org/2/library/random.html?highlight=shuffle#random.shuffle

Ngrep is a very user friendly packet sniffer, basically the “grep” equivalent at the network layer.

Here is a quick way of figuring out the http connections your browser is making even if you are browsing to a secure site, make sure that is the only site you are visiting as the command will capture all port 80 connections.

Installation –

apt-get install ngrep

Let us redirect all traffic ngrep captured to a file –

ngrep -d any -W byline port 80 | tee  /tmp/net_output

Now visit a secure site, say https://cnet.com, you will see nicely formated output

root@lindell:~# ngrep -d any -W byline port 80 | tee  /tmp/output
interface: any
filter: (ip or ip6) and ( port 80 )
####
T 17.31.198.19:33954 -> 72.21.91.29:80 [AP]
POST / HTTP/1.1.
Host: ocsp.digicert.com.
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0.
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8.
Accept-Language: en-US,en;q=0.5.
Accept-Encoding: gzip, deflate.
Content-Length: 83.
Content-Type: application/ocsp-request.
Connection: keep-alive.
..

From here, you can parse the /tmp/output file.

Similarly, you can parse the output file for the type of web server your favorite sites are using. Keep the ngrep command running, and visit all your favorite sites. Note, this works for http only, as https traffic is encrypted, for https only destination IP and port are shown.

In this case, I searched for the ‘Server:’ field in the HTTP response header from the web server. Apparently, nginx seems to be most popular, it is also interesting to see that AmazonS3 storage being used for hosting static content –

root@lindell:~# awk '/Server:/ {print $2}' /tmp/output |sort | uniq -c |sort -nr
    155 nginx.
     40 Apache.
     36 Apache-Coyote/1.1.
     20 Apache/2.2.3
     14 nginx/1.8.1.
      7 AmazonS3.
      6 Akamai
      5 ECS
      5 cloudflare-nginx.
      4 Omniture
      4 ESF.
      3 sffe.
      3 nginx/1.10.2.
      2 Microsoft-IIS/7.5.
      2 gws.
      2 AkamaiGHost.
      1 WildFly/8.
      1 Varnish.
      1 openresty.
      1 NetDNA-cache/2.2.
      1 Cowboy.
      1 ATS.
      1 Apache/2.2.14

References –
http://ngrep.sourceforge.net/usage.html
https://wiki.christophchamp.com/index.php?title=Ngrep

In some cases, you might want to block all users from logging in to the system or just after you login, you want to prevent everyone else from connecting to the server. During server maintenance, this could be helpful or there are use cases where only one actively logged in user has to do some work if the username is a shared account.

Solution – create the /etc/nologin file, and put the text notice as the body of the file. If a user attempts to log in to a system where this file exists, the contents of the nologin file is displayed, and the user login is terminated.

[root@kauai ~]# echo 'System is under maintenance till Dec. 24, 2PM EST.' > /etc/nologin

Now try to login to the server under non super user –

daniel@linubuvma:~$ ssh ns2
System is under maintenance till Dec. 24, 2PM EST.
Connection closed by 192.168.10.103

If your ssh configuration allows it, root user can login to the server though, the root user will still be greeted with the contents of /etc/nologin file though –

daniel@linubuvma:~$ ssh root@ns2
root@ns2's password:
System is under maintenance till Dec. 24, 2PM EST.
Last login: Sat Dec 12 01:11:35 2015 from linubuvma.home.net
[root@kauai ~]# 

Reference – https://docs.oracle.com/cd/E19683-01/806-4078/6jd6cjs3v/index.html

During user login, a Linux box might show message of the day(motd), new email, or package updates information. This is particularly common in Ubuntu boxes. In some cases, you want to prevent all these messages from being displayed as it could be delaying your login for instance.

Solution – Create a file named .hushlogin in the user’s home directory.

A typical login to an Ubuntu box might look like this –

[daniel@kauai etc]$ ssh practice
daniel@practice's password: 
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan 10 11:37:24 EST 2015

  System load:  0.0                Processes:           290
  Usage of /:   46.1% of 45.15GB   Users logged in:     1
  Memory usage: 13%                IP address for eth0: 192.168.10.206
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

168 packages can be updated.
63 updates are security updates.

You have new mail.
Last login: Sat Jan 10 11:37:26 2015 from linux.local

To suppress all this information, create a .hushlogin file in the users home directory and log out and login back –

daniel@linubuvma:~$ touch ~/.hushlogin

daniel@linubuvma:~$ exit
logout
Connection to practice closed.

[daniel@kauai etc]$ ssh practice
daniel@practice's password: 

daniel@linubuvma:~$ 

Requirements

    Ubuntu 10.04 32-Requirements

      Ubuntu 10.04 32-bit or 64-bit server
      2 GB of memory
      5G of free disk space

      Install BBB apt repository

    wget http://ubuntu.bigbluebutton.org/bigbluebutton.asc -O- |  apt-key add -
    
    echo 'deb http://ubuntu.bigbluebutton.org/lucid/ bigbluebutton-lucid main' | tee /etc/apt/sources.list.d/bigbluebutton.list
    
    echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse' | tee -a /etc/apt/sources.list
    

    Install Asterisk

      apt-get update 
      apt-get install bbb-voice-conference
      

      Install BBB

      apt-get install bigbluebutton
      

      Restart service and check for any errors in configuration

      bbb-conf --clean
      bbb-conf --check
      

      Access BBB at http://yourip
      Make sure port 80 is not blocked by your server and no other service except nginx
      is listening on port 80.

      Create meeting rooms with BBB wordpress plugin

    Add php support to nginx

    apt-get install python-software-properties
    add-apt-repository ppa:brianmercer/php
    apt-get update
    apt-get install php5-fpm
    

    Edit /etc/php5/fpm/php5-fpm to include

    listen = /var/run/php5-fpm.socket
    

    Add the following line to /etc/nginx/sites-enabled/bigbluebutton

    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   unix:/var/run/php5-fpm.socket;
        fastcgi_param  SCRIPT_FILENAME  /var/www/bigbluebutton-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
      }
    

    Restart nginx and php5-fpm

    /etc/init.d/php5-fpm restart
    /etc/init.d/nginx restart
    

    Install wordpress

      cd /usr/local/src
      wget -c http://wordpress.org/latest.tgz
      tar xzvf latest.tgz
      mv latest /var/www/bigbluebutton-default/bbbwordpress
      cd /var/www/bigbluebutton-default/bbbwordpress
      cp wp-config-sample.php wp-config.php
      

      Create mysql database for worpress
      mysql -u root -p (enter your password, or if there is no password set,just type
      mysql -u root)

      create database bbbwordpress;

      grant all privileges on *.bbbwordpress to bbbuser@localhost identified by 'yourpass';
      exit;
      

      Then edit wp-config.php

      define('DB_NAME', 'bbbwordpress');
      define('DB_USER', 'bbbuser');
      define('DB_PASSWORD', 'yourpass');
      

      Go to http://yourip/wordpress and set administrative user and password. Then login to the administrative
      dashboard and click on the plugins in the left hand pane. In the ‘search plugin’ box, type “bigbluebutton”
      and install/activate it.

      To integrate it with BBB click on Appearance–>Widgets–>BigBlueButton and drag/drop it to the content area.
      GO to Settings–> put the URL to your BBB link http://yourip/bigbluebutton and insert the salt from the
      setting page.

      Within the administrative wordpress dashboard you can create meetings and the passwords for participants/moderator.
      You should be able to access the rooms in http://yourip/wordpress/.

        Sources

      http://code.google.com/p/bigbluebutton/wiki/InstallationUbuntu
      http://cbanta.wordpress.com/2010/12/18/enable-php-on-bigbluebutton/
      http://wordpress.org/extend/plugins/bigbluebutton/bit or 64-bit server
      2 GB of memory
      5G of free disk space

      Install BBB apt repository

    wget http://ubuntu.bigbluebutton.org/bigbluebutton.asc -O- |  apt-key add -
    
    echo "deb http://ubuntu.bigbluebutton.org/lucid/ bigbluebutton-lucid main" | tee /etc/apt/sources.list.d/bigbluebutton.list
    
    echo "deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse" | tee -a /etc/apt/sources.list
    

    Install Asterisk

      apt-get update 
      apt-get install bbb-voice-conference
      

      Install BBB

      apt-get install bigbluebutton
      

      Restart service and check for any errors in configuration

      bbb-conf --clean
      bbb-conf --check
      

      Access BBB at http://yourip
      Make sure port 80 is not blocked by your server and no other service except nginx
      is listening on port 80.

      Create meeting rooms with BBB wordpress plugin

    Add php support to nginx

    apt-get install python-software-properties
    add-apt-repository ppa:brianmercer/php
    apt-get update
    apt-get install php5-fpm
    

    Edit /etc/php5/fpm/php5-fpm to include

    listen = /var/run/php5-fpm.socket
    

    Add the following line to /etc/nginx/sites-enabled/bigbluebutton

    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   unix:/var/run/php5-fpm.socket;
        fastcgi_param  SCRIPT_FILENAME  /var/www/bigbluebutton-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
      }
    

    Restart nginx and php5-fpm

    /etc/init.d/php5-fpm restart
    /etc/init.d/nginx restart
    

    Install wordpress

      cd /usr/local/src
      wget -c http://wordpress.org/latest.tgz
      tar xzvf latest.tgz
      mv latest /var/www/bigbluebutton-default/bbbwordpress
      cd /var/www/bigbluebutton-default/bbbwordpress
      cp wp-config-sample.php wp-config.php
      

      Create mysql database for worpress
      mysql -u root -p (enter your password, or if there is no password set,just type
      mysql -u root)

      create database bbbwordpress;

      grant all privileges on *.bbbwordpress to bbbuser@localhost identified by 'yourpass';
      exit;
      

      Then edit wp-config.php

      define('DB_NAME', 'bbbwordpress');
      define('DB_USER', 'bbbuser');
      define('DB_PASSWORD', 'yourpass');
      

      Go to http://yourip/wordpress and set administrative user and password. Then login to the administrative
      dashboard and click on the plugins in the left hand pane. In the ‘search plugin’ box, type “bigbluebutton”
      and install/activate it.

      To integrate it with BBB click on Appearance–>Widgets–>BigBlueButton and drag/drop it to the content area.
      GO to Settings–> put the URL to your BBB link http://yourip/bigbluebutton and insert the salt from the
      setting page.

      Within the administrative wordpress dashboard you can create meetings and the passwords for participants/moderator.
      You should be able to access the rooms in http://yourip/wordpress/.

        Sources

      http://code.google.com/p/bigbluebutton/wiki/InstallationUbuntu
      Enable PHP on BigBlueButton .71 Server
      http://wordpress.org/extend/plugins/bigbluebutton/