Tuesday, April 30, 2013

Inode

An inode is a data structure in Unix/Linux file systems that stores information (metadata) about a file system object, except file content and file name. The command stat allows us to retrieve most information stored in an inode. Here is an example of system file bash on a Linux box:

  File: `/usr/bin/bash'
  Size: 902036        Blocks: 1768       IO Block: 4096   regular file
Device: 808h/2056d    Inode: 279026      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:shell_exec_t:s0
Access: 2013-05-01 13:59:45.680608666 +0800
Modify: 2013-01-31 22:47:10.000000000 +0800
Change: 2013-03-21 10:29:27.135450340 +0800
 Birth: -


We can see that regular files have the following attributes:
  • Size in bytes
  • Device ID where the file is stored
  • Inode number
  • Link count
  • User ID of the file
  • Group ID of the file
  • Permissions (access rights) of the file
  • Timestamps on last access (atime), modify (mtime), and change (ctime)
To get the file content, we need to consult to the inode pointer structure that is also part of information stored in inode. The inode pointer structrure consists of four different pointers:
  • direct pointer,
  • singly indirect pointer,
  • doubly indirect pointer, and
  • triply indirect pointer
These pointers point directly or indirectly to block locations where pieces of file content are stored.


One question remains: where is the file name stored? The answer is that it is stored in the content of the directory that contains the file. Unix/Linux directories are lists of association structures, each of which consists of one file name and one inode number for that file. That is why we need to specify (implicitly or explicitly) the path whenever we want to access a file in the file systems.

Monday, April 22, 2013

Create Multiple User Accounts on Linux Systems

adduser is a system command on FreeBSD that allows root to create new user accounts in interactive or batch modes.

useradd is a system command on Linux systems that enable root to create a new user account; to create multiple user accounts on Linux systems in batch mode, we may use the newusers system command.

Each input line for newusers is in the same format as the standard password file with some exceptions. To use command newusers, we need to specify the password for user or leave it blank as for null password. There are times that we need to create multiple user accounts with random passwords. The following bash script allows us to accomplish this with few more options:

#!/bin/bash
function mkpw()
{
    head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-10;
}

SAVEFILE=newAccounts
chmod 0400 $SAVEFILE
IFS=':'
while  read USER PASSWORD uUID GID FULLNAME HOMEDIR SHELL
do
    PASSWORD=${PASSWORD:-`mkpw`}
    GID=${GID:-"users"}
    SHELL=${SHELL:-"/bin/bash"}

    echo "Adding user '$USER, $FULLNAME'"
    useradd -m $USER -p `mkpasswd -p $PASSWORD` \
        -c "$FULLNAME" -s $SHELL -g $GID
    echo "$USER:$PASSWORD" >> $SAVEFILE
done


where mkpasswd is the command that encrypts the given password. The input file for this script is in the same format as for command newusers. The output file, named newAccounts, contains all newly created account ids and passwords in plain text; you need to take proper action on it.

poor men's PGP

I just finished the pmPGP, a CLI for sending/receiving openPGP mime messages.

The pmPGP is based on python and gnupg; it supports sending emails in the following formats:
  1. plain -- regular email
  2. sign -- RFC3156
  3. encrypt -- RFC3156
  4. sign-encrypt -- RFC3156
  5. Sencrypt -- Symmetric encryption (for fun and personal usage)
  6. sign-Sencrypt -- (for fun and personal usage)
Poor man may use pmPGP to store/backup files on email servers.
Sounds interesting? Get it from:

Monday, April 8, 2013

random password generator

Here is a code fragment to use /dev/urandom to generate a random password of length 10 using bash; it should suffice for most needs.

head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-10


If openssl is installed, we may use:

openssl rand -base64 10 | cut -c1-10

Wednesday, April 3, 2013

How to test Django development server remotely?

Django development server is intended only for use while developing. There are times that we need to test Django applications remotely through development server. Thought we may bind the development server to some public IPs to achieve this, it is not a good practice in general for various reasons. Ssh port forwarding provides a much secure way for remote testing of Django development server. Let serv be the server where Django development server resides, test the remote host where you want to conduct the tests. Here is a how-to.

1. On host serv, start your development server:
python manage.py runserver 8080
where 8080 is any port number of your choice.

2. On host test, start ssh port forwarding:
ssh -L 9090:localhost:8080 user@serv
where 9090 is the port number providing remote service.

3. On host test, point your browser to localhost:9090 to perform tests.


If you want to test the development server through other hosts, say  third, you should enable gateway forwarding on host test by issuing:

ssh -g -L 9090:localhost:8080 user@serv

Then you may perform tests through host third by pointing your browser to test:9090.