Thursday, November 21, 2013

Creating custom SSH welcome messages

Creating custom SSH welcome messages using motd (message of the day) to protect and secure SSH logins by displaying warming message to UN-authorized users or display welcome or informational messages to authorized users.

  • Login to your server as root via SSH. 
  • sudo vim /etc/motd 
  • Now type in the message you want everyone to see.
 ######### Welcome to the Vishal's server #########
 ###         This is the webServer                               ###

Save and Quit,

Logout of SSH then log back in, you will receive your new greeting!
Vishal Vyas

Monday, November 11, 2013

Linux Network Bandwidth check Script.

The following are two such scripts by joe miller. The first script counts the number of packets per second, received (RX) or sent (TX) on an interface, while the latter scripts measures the network bandwidth of incoming (RX) and outgoing (TX) traffic on an interface. For these scripts to work, you do not need to install anything.

Measure Packets per Second on an Interface
INTERVAL="1"  # update interval in seconds
if [ -z "$1" ]; then
        echo usage: $0 [network-interface]
        echo e.g. $0 eth0
        echo shows packets-per-second
while true
        R1=`cat /sys/class/net/$1/statistics/rx_packets`
        T1=`cat /sys/class/net/$1/statistics/tx_packets`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_packets`
        T2=`cat /sys/class/net/$1/statistics/tx_packets`
        TXPPS=`expr $T2 - $T1`
        RXPPS=`expr $R2 - $R1`
        echo "TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s"

Measure Network Bandwidth on an Interface
INTERVAL="1"  # update interval in seconds
if [ -z "$1" ]; then
        echo usage: $0 [network-interface]
        echo e.g. $0 eth0
while true
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep $INTERVAL
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T2 - $T1`
        RBPS=`expr $R2 - $R1`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        echo "TX $1: $TKBPS kb/s RX $1: $RKBPS kb/s"
The following screenshot shows the above two scripts in action.


Another Command to check network Status .

# /sbin/ip -s link 

Friday, July 19, 2013

Block certain file extensions on samba linux

Samba offers an option to block files with certain patterns, like file extensions. This option can be used to prevent dissemination of viruses or to disuade users from wasting space with certain files:

$ vim /etc/samba/smb.conf

comment = vishalvyas
Path = /home/vishal
public = yes
writable = yes
browseable = yes
read only = no
valid user = vishal
create mask = 0775
Veto files = /*.exe/*.com/*.dll/*.bat/*.vbs/*.tmp/*.mp3/*.avi/*.mp4/*.wmv/*.wma/

Vishal Vyas

Tuesday, May 14, 2013

How to increase swap size in linux

Swap is a type of filesystem and is a virtual memory. Whenever your RAM is full, your operating system will look for further memory in your swap space. For this reason, you reserve some part of the hard disk to create a swap partition.

Identifying Current Swap Space Usage:

root@vishal-desktop:/# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 4       -1

Alternatively, use the swapon command:

root@vishal-desktop:/# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 4       -1

Finally, the free command may also be used:

root@vishal-desktop:/# free
             total       used       free     shared    buffers     cached
Mem:        895112     721656     173456          0      36592     310156
-/+ buffers/cache:     374908     520204
Swap:      1952736          4    1952732

Adding a Swap File :

Additional swap may be quickly added to the system by creating a file and assigning it as swap. This is achieved as follows.

The following dd command example creates a swap file with the name swap with a size of 1Gb.

Create the swap file using the dd command :

root@vishal-desktop:/# dd if=/dev/zero of=/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 17.4283 s, 61.6 MB/s

Configure the file as swap:

Change the permission of the swap file so that only root can access it

root@vishal-desktop:/# chmod 600 /root/swap

root@vishal-desktop:/# mkswap /swap
Setting up swapspace version 1, size = 1048572 KiB

Enable the newly created swapfile :

root@vishal-desktop:/#  swapon /swap

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:

# cat /etc/fstab
/swap  none  swap  sw  0 0

Once the swap space has been activated, verify that it is in use using the swapon –s command:

root@vishal-desktop:/#  swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 142884  -1
/swap                                   file            1048572 0       -2

root@vishal-desktop:/#  free -k
                     total       used       free     shared    buffers     cached
Mem:        895112     828484      66628          0       2144     539552
-/+ buffers/cache:     286788     608324
Swap:      3000312     142876    2857436

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, use following to enable or disable swap.

root@vishal-desktop:/#  swapoff -a

root@vishal-desktop:/#  swapon -a

De-activate the additional swap space at any time using the swapoff command as follows:

root@vishal-desktop:/# swapoff /newswap

Vishal Vyas

Monday, March 4, 2013

Monitoring User Activity with psacct On Linux

One of the big advantages of using psacct on your server is that it provides excellent logging for activities of applications and users.

Installation :-

for Redhat, Fedora, CentOs

yum install psacct
service psacct start

For Ubuntu Debian

apt-get install acct
service acct start

Following is the list of utilities it includes:
The ac command displays statistics about how long users have been logged on.
The lastcomm command displays information about previous executed commands.
The sa command summarizes information about previously executed commmands.
The accton command turns process accounting on or off.

How to use psacct :

The connect time in hours is based on logins and logouts. 

root@Vishalvyas:~#  ac -p
        vishal                        9.12
        Ashish                      20.60
        Vipul                        15.80
        Anil                          17.33
        Akshay                     10.92
        pritesh                      4.10
        chirag                       8.75
        total                         168.95

which user has executed what command on system :

root@Vishalvyas:~# lastcomm vishal

Process        Flag    Username  Terminal    Time

vim                          X vishal        pts/2         0.01 secs Tue Mar  5 10:16
su               S           vishal            pts/2         0.00 secs Tue Mar  5 10:16
bash                        vishal            pts/2        0.10 secs Tue Mar  5 10:16
bash             F         vishal            pts/2        0.00 secs Tue Mar  5 10:16
python                     vishal            pts/2       0.05 secs Tue Mar  5 10:16
crontab                    vishal            pts/2       0.00 secs Tue Mar  5 10:16
bash             F         vishal            pts/2       0.00 secs Tue Mar  5 10:16
python                     vishal            pts/2       0.04 secs Tue Mar  5 10:16
bash             F         vishal            pts/2       0.00 secs Tue Mar  5 10:16
python                     vishal            pts/2       0.04 secs Tue Mar  5 10:16
ssh                          vishal            pts/2       0.00 secs Tue Mar  5 10:16
ifconfig                    vishal            pts/2       0.00 secs Tue Mar  5 10:16

Search the accounting logs by command name:
root@Vishalvyas:~#  lastcomm vim
vim                    root     pts/1      0.02 secs Tue Mar  5 10:28
vim                    root     pts/1      0.02 secs Tue Mar  5 10:18
vim                  X vishal   pts/2      0.01 secs Tue Mar  5 10:16

Pribt All Account Activity :
The “sa” command is used to print the summary of commands that were executed by user.
 root@Vishalvyas:~# sa
    3178    4679.96re       0.80cp         0avio      4435k
     176    4586.25re        0.69cp         0avio     19371k   httpd*
      35       0.15re           0.04cp          0avio     23363k   /usr/share/webm*
      15       0.04re           0.02cp          0avio     17296k   landscape-sysin
      12       0.04re           0.02cp          0avio      6346k
      13       5.80re           0.01cp          0avio     26052k   svn

S - executed as super-user
F - executed after  but not following exec
D - terminated with core file
X - terminated with signal SIGTERM

Vishal Vyas

Wednesday, January 16, 2013

Vim Advanced Text Editor

Vim is a text editor written by Bram Moolenaar and first released publicly in 1991. Based on the vi editor common to Unix-like systems, Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface. Vim is free and open source software,The name "Vim" is an acronym for "Vi IMproved"[6] because Vim is an extended version of the vi editor, with many additional features designed to be helpful in editing program source code.


Vim also has a built-in help facility (using the :help command) that allows users to query and navigate through commands and features.

VIM An Advanced Text Editor:-
=> Newer Virsion of VI, the standard Unix text editor

=> gvim :         Graphical Version of vim


SPEED            : Do more with fewer keystrokes
Simplicity        : No dependence on mouse/GUI
Availability      : Included with most Unix-like OSes

Difficulty         : Stteper learning curve than simpler editors

Three Main Mode :-
Command Mode     : Move cursor, cut/paste text, change mode
Insert Mode            : Modify text
Ex Mode                 : Save, quit, etc

'Esc' key is used to exits current mode.

'EscEsc' always returns to command mode.

=> vim abc.txt        : can edit the text file if existing or not
=> vim /etc/passwd
=> vim /tmp/file

       : begins insert end of line
    : Append to end of line
     : Insert at beginning of line
(small)    o    insert new a line (below of current line)
(caps)    O    insert new line ( above of current line )

EX MODE with ( : ) :-
    :w       : writes (saves) the file to disk
    :wq     : writes and quits
    :q!      : quits, even if changes are lost ( do not save )

COMMAND MODE ( Default Mode ) :-
    Right Arrow         ( -> )     moves right one character
    5, Right Arrow     ( 5 ->) moves right five (5) character

    h    move cursor to left
    j     move cursor to down
    k    move cursor to up
        move cursor to right

    w    move cursor by word ( forward )
      move cursor by word ( backward )   
    (     move cursor by sentense ( forward )
    )     move cursor by sentense ( backward )

    /xyz    search the 'xyz' into the file  (n,N) use for next,prev

This command will run in file --------------- {vi /xyz (filename)}
    :%s/macho/dud/gi    search & replace all macho to dud (gi- globely)
    :1,5s/sam/sandy/      search & replace from line-1 to line-5

    :5        move cursor to line no. 5
    :G       move cursor to the end of document
    dd       to delete current line
    4dd     to delete four line
    yy       to copy the line
    5yy     to copy five line
    p/P      paste the copied line or text 
    u         undo 
   ctrl r    redo

    :set number    set the line number in file
    :set nonu    remove the line number in file

NOTE ( try must / read must / do must )

if you have any query about vim    please try 'vimtutor' command in terminal not in file

Vishal Vyas

Wednesday, January 9, 2013

Browsing linux filesystem

Browsing The Filesystem

Some Important Directories
Red hat root (main)directory: /
Home Directories:                /root                       ( home directory of root only )
                                            /home/username      ( for all users )
User Excecution :                /bin, /usr/bin, /usr/local/bin
system Excecution :             /sbin, /user/sbin, /usr/local/sbin
Other Mountpoints :             /media, /mnt
Configuration :                     /etc
Temporary files :                  /tmp
Kernels and Bootloader      /boot
Server Data :                      /var, /sys
System Information :           /proc, /sys
Shared Libraries :               /lib, /usr/lib, /usr/local/lib

=> pwd                                 to see current working directory
Note :   1) Names may be up to 255 characters
                2) All characters are valid, except the forward-slash
                3) Names are case-sensitive

=> mkdir dirname             to make a dir

=> mkdir 1 2 3 4              to make multipal dir

=> mv o-name n-name      to rename dir

=> touch filename             to make a empty file

=> touch {a,s,d,f}.{jpg,doc,mp3}

=> mv o-name n-name     to rename file

=> vi filename                   to see or edit the file

=> cat filename                 to see the contant of file
(cat > , cat >>)

=> nano filename              to edit in text file
=> cd dirname                  to change dir
=> cd ..                            to a directory one level up
=> cd                               to goto home directory
=> cd -                             to your previous directory
=> ls -a                             to list of file and dir with hidden file 
=> ls -l                              to see long listing of file and dir (ll)

=> cp sourcefile destination             ( cp file1 /home/macho )
                to copy file

=> cp -r sourcedir destination          ( cp -r dir1 /home/macho )
                to copy dir recursively   

=> cp -r -v sourcedir destination     ( cp -r -v dir1 /home/macho )
                to copy dir & show process         

=> mv sourcefile destination            ( mv file1 /home/macho )
                to move file into elesewhere

=> mv dirname destination               ( mv dir1 /home/macho )

=> rm filename                                 (to remove any file)

=> rm -rf *.mp3                               (to remove all file which extension .mp3)

=> rmdir directoryname                    ( to remove dir )

=> Backing-up your system configuration
       -mkdir /root/backups-20100415
      -cp -r -v /etc/sysconfig /root/backup-20100415

Vishal Vyas
Linux guru.

Basic linux commands

==>>>   Command


                - date                          : to see the date and time
                - date 112503451982 : month,date,hour,minut,year                        
                - cal                            : to see the calendar
                - cal 2010                   : to see the calendar of 2010
                - cal 5 2010                : display the calendar of May 2010                                        
                - clear                         : to clear the screen
                - ls                              : to see the list of dir and file                   
                - useradd vishal           : to create user with name 'vishal'
                - passwd vishal            : to set & also change password of user 'vishal'
                - passwd                     : to root only

                - passwd -d vishal                  : create user without password
                - usermod -l n-name o-name  : to rename user
                - usermod -g group user         : to add user in group
                - useradd -n vishal                  : create user without duplicate directory
                - userdel vishal                        : to delet user
                - userdel -r vishal                    : Delet user with their directory
                - rm -rf directory                    : forcefuly remove directory

                - groups vishal                         : to see membarship of user
                - vi /etc/passwd                       : see user directory
                - groupadd sels                        : create group
                - vi /etc/group                          : see group directory

                - groupmod -n n-name o-name  : rename group

                - su macho           : to login one user to other user (sudo passwd vishal)
                - system-config-   : to see graphycaly any configuration
                - ctrl+shift-t          : create a new Tab
                - ctrl+PgUp/Dn    : Switch to next/prev
                - /usr/share/doc    : to see file of any command
                 (cp,cd & ls -l    /usr/share/doc/HTML/index.html)

                - whatis cal           : to see help about any calendar
                - date --help         : to see detailed help about date
                - man ls                : to see manual page about ls
                - info history         : to information of history
                - which dir            : to find path of any directory & command

=> Tilde ( ~ )     (May refer to your home directory)
                           cat ~/.bash_profile
                           ls ~sam/public_html


=> system-config-date                  to set date & time graphicaly

=> history     (this command is used to see recently used command)

=> ping
=> ping -w4
=> ping -c4 > test
=> ll >>test

=> renice 5 PID          reduce the priority

=> kill 3428                used to kill particular process with pid

=> top (CLI)              used to see process list, memory and cpu uses

=> gnome-system-monitor    ( GUI ) 

=> ctrl+c                     fully stop

=> ctrl+d                    stop terminal

=> ctrl+z                     send the program into background or temp. halt (ping,firefox)

=> jobs                       used to check the program running into back.

=> fg 1 or 2 or 3         used to bring halt program to rerun

=> at 0630                  to schedule these all jobs at 06:30

   > mkdir /root/Desktop/macho
   > useradd macho
   > ls -l
   > ctrl+d
Note : at command runs programme only ones at particular time

=> atq                         to see pending jobs

=> atrm 4                    to remove job-no. 4

=> crontab -e              this command is used to execute the job
            manytime as you want (chep-4)

=> who | wc -l > abc.txt    example of grouping multi command

=> date; who | wc -l >> abc.txt     grouping command

=> $?                used to check the last command status
            0     for     success
            1-255     for     error  

 => date 010101012010

                month  : MM
                date     :  DD
                hours   :  hh
                minute  : mm
                year     : YY
above example will set the date

 1st of JAn, at 01:01:01am. and the Year 2010

                - startx  : initialize an X session (if ur mode in text through this command u can get Graphical mode).

Vishal Vyas

Install Darkstat - a network traffic analyzer

Darkstat is a opensource network monitoring tool, It is a packet sniffer which runs as a background process and serves its statistics to a ...