Menu-Submenu

Setup on LINUX


Setup on LINUX


  1. Linux Architecture

The GNI/Linux operating system consists of kernel, shell, utilies and application programs.


Kernel

The core of the Linux system is the Kernel - the operating system programs. The user doesn't deal directly with a kernel. Instead, the logon process starts up a separate interactive program, called the Shell, for each user.

Shell

It is a simple user interface which provides the services that a user wants. Some common shells in Linux are bash, sh, tcsh, csh, and ksh.

  1. Bourne Shell
Officially distributed with Unix systems, It is fastest Unix command processor available on all Unix systems. It is installed as executable file '/bin/sh'

  1. C Shell
It resembles the C programming language in syntax. It is not compatible with Bourne shell. Executable file is csh.

  1. Korn Shell
It combines the best features of both Bourne Shell & C Shell. Executable file is ksh.

  1. Bash Shell
It is acronym for 'Bourne Again Shell'. It is an enhancement of Bourne Shell and is the default for most Linux systems. It is capable of storing the history of the commands. Executable is /bin/bash. Also sh is symbolic link to Bash

  1. Tcsh Shell
It stands of Tom's C shell, also known as TC shell. It is an enhancement of C shell.Executable if tcsh. Also csh is the symbolic link for Tcsh shell on Linux.

  1. A Shell
It is lightweight Bourne shell clone. It is usually suitable for computers that have very limited memory. Executable is ash

  1. Z Shell
It has best features of Tcsh shell. It can emulate all the features of Korn shell. It has largest number of utilities. Executable is zsh.


  1. Linux Filesystem


  • All files are stored on the disk under one main directory called the 'root' directory, which further subdivided into directories: bin, boot, home, usr, etc, and dev.
  • /dev directory stores all the device-related files
  • /etc directory stores the operating system-related data
  • /usr directory stores the operating system files that are not involved in the boot process
  • /var directory has information specific to different utilities of Linux

Types of Files in Linux

In Linux, all information is treated as a file.
  • Ordinary files : All files created by user come under this category; user's program files, data files, object files and executable files. A user can make changes to such files.
  • Directory files : Linux automatically create it when a directory is created, which contain information about directory contents. A directory file can't be modified by a user.
  • Special files : Most of system files in Linux are special files, which are typically associated with input/output devices and are found in standard Linux directories such as /dev and /etc. User can't alter special files.


First symbol of 'ls -l' output indicates the files type.

Symbol
File Type
-
Ordinary file
d
Directory
b
Block special file
c
Character special file
l
Symbolic link
p
Named pipe (also called FIFO)


File commands in Linux

  • Create a file by using the command 'cat > filename'. Once the content of file is typed press 'Ctrl+d' to end the creation of file.
  • To display the contents of file page-wise 'more [option] filename' can be used. While viewing file using 'more' command, once scrolled down can't move up.
  • The 'less' command is similar to 'more' command except that it is possible to scroll upwards also while viewing the contents of a file. The 'less' command is also a little faster than the 'more' command.
  • The wildcard character '?' is used for matching exactly one occurrence of any character.
  • The wildcard character '[ ]' can be used to restrict the characters to be matched.

Linux Session : Logged In

  • Linux keeps track of all the usernames and the information about users in the 'shadow' and 'passwd' files under /etc directory.
  • In result of 'who' command, second column specifies the name of the terminal from where the user has logged in.



  1. Managing Documents


  • The file descriptor 0 is assigned to the Standard Input File
  • The file descriptor 1 is assigned to the Standard Output File
  • The file descriptor 2 is assigned to the Standard Error File

Redirection

  • Input to a command can be taken from a file other than the keyboard : Input Redirection
  • Output of a command can be written to a file or printed instead of being displayed on the monitor : Output Redirection
  • Error of a command can be written to a file or printed : Error Redirection

Filters

  • Without filename, grep command expects standard input. Execution stops when a user indicates the end of input by pressing '<Ctrl>+d'
  • grep command complex regular expressions
^ matches pattern with pattern at beginning of line
^ within [ ] matches pattern without character in specified set
$ matches pattern with pattern at end of line
\ ignores special meaning of a character

-v option to print all lines with non-matching pattern
  • Command 'wc [-l or -w or -c]' Filter used count number of lines or words or characters, respectively
  • Command 'cut [-fX or -cY or -d"Z"] [filename(s)]' Filer used to extract specific position/range of columns X or character X respectively from files, with delimiter Z
eg cut -d":" -f1,6 OR cut -d":" -f1-4
  • Command 'tr [option or old-character(s)] [new-character(s)] < filename' Filter can be used to translate one set of characters to another. It translates the characters in the output only and doesn't effect the input file. The -s option is used to squeeze several occurrences of a character into one character. This filter also can be used for case-conversion (tr "[a-z]" "[A-Z]").

Pipes

  • The vertical bar (|) Pipe character indicates to the shell that the output of the command before '|' is to be sent as input to the command after '|'
  • Command 'tee' is used after pipe '|' to redirect the standard output of previous command to another command and also to save in file(s). 'tee' command takes standard input (from output of previous command) and writes to standard output and to file(s). It creates the files if not exist, else overwrite existing files. The -a (append) option can be used to append contents to an existing files.

Locating Files

  • -type option to 'find' command is used to locate a file of a specific type. 'f' for ordinary files, 'd' for directory files.
  • -exec option to 'find' command allows to specify the operations to be performed on located files. {} must be used in operation of -exec option, to replace it with the path names of located files; while backslash followed by a semicolon (\;) is required at the end of the -exec option as part of syntax. -exec options always execute operation on each located files
  • -ok option prompt for user confirmation to operate on located files. It has same syntax as that of -exec option.

File Access Permissions (FAP)

  • Execute permission on a file allows to execute the file provided the file also has the read permission
  • Read permission on a directory only allows to list contents of it
  • Write permission on a directory allows to create, copy, remove, rename/move files and subdirectories within it
  • Execute permission on a directory allows to move to the specified directory using 'cd' command. Also allows to list it's files and copy files in it.


  1. Shell Scripting


  • Executing a shell script creates a child shell (sub-shell) to execute it, so that the current parent shell is not affected by it.
  • The '#!' characters should be the first two characters of the script in case to specify the interpreter to be used for it's execution.
  • '\' is escape character in script, to negate the special meaning of symbols.
  • A variable can be created without a value by leaving the right hand side of assignment operator blank or with value. There must be no space on either side of assignment operator (=).
  • Any variable created within a shell script is lost when the script stops executing. A variable can be referred either as ${variable} or as $variable.
When a new shell is created (using 'sh' command until 'exit' command), it is unaware of the variables of the parent shell.
But using 'export' command, copy of a variable of parent shell can be made global & pass to its child shell to use. The child shell can change the value of the copy, but when it terminates, so does the copy.
  • Using 'read variable' command, shell also allows to enter a value from the keyboard into a variable during the execution
  • 'PATH' is an environmental variable, copy of which is available in each shell to uniquely configure it. Any executable file will be searched in the order specified in the PATH variable.
  • PS1 (Prompt String 1) environmental variable contains the shell prompt '$' symbol. PS1 can also contain certain predefined prompt strings. e.g. '\w' prompt string is used to display the current working directory. PS2 environmental variable sets value for the secondary prompt, which is by default '>'.
  • SHLVL environment variable contains the shell level that currently working in. The login shell is given SHLVL as 1.
  • Command substitution in string can be done either enclosing it in single backward quotes (grave accent) '`date`' or enclosing in as '{date}'
Sam can be used to assign result of a command to a variable. e.g. var1=`expr $var1 + 20`
  • Most shells doesn't support numeric variables. All the variables are treated as character strings. Mathematical manipulation of such variables is done using 'expr' command. (Multiplication operator '*' in shell script should be preceded by a backslash (\), to not to interpret as a wildcard character.

Arithmetic Expansion can also be done using '$((expression))'.
e.g. ((a=$a+1))  or a=(($a+1)) or  a=`expr $a + 1`
  • The 'test' command and '[]' evaluates an expression passed to it and returns true (0) or false (1). There must be a space on either side of '=' in expression; and a space after '[' and before ']'.
  • The string and variable name may or may not be within quotes. However,  if there is a possibility that the variable may not contain a value, variable must be enclosed within quotes. e.g. [ "$var1" == "John" ].
Else it must be checked that the variable is not blank. '-z <string>' to check for NULL string or zero length string, while '-n <string>' or '<string>' to check non-zero length string.
  • Multiple conditions in expression can be combined using options -a (for AND) and -o (for OR).
  • 'if...then..else...fi' can be used for 'test' expression.
e.g. if<space>[<space>"$var1"<space>=<space>"y"<space>]
       then
       ...
       elif<space><CONDITION2>
       then
       ...
       else
       ...
       fi
  • Files Status can be checked using 'test <!> <option> <filename>' or [ <option> <filename> ]' command. Options are -e existence, -f ordinary file, -d directory, -r readable by owner, -w writable by owner, -x executable by owner, -s non empty, -b block special, -L symbolic link, -O owned by current user, -G owned by group of current user.
test file1 -nt file2 => if file1 is newer than file2
test file1 -ot file2 => if file1 is older than file2
  • The case...esac Construct
case $var1 in
    value1)
        <commands>
        ;;
    value2)
        <commands>
        ;;
    *)
        <default case commands>
        ;;
esac
  • The while Iteration / Loop Construct
while [ <conditions> ]        OR while true
do
    <commands>
done
    => The break and continue commands are used with the while loop.
  • The until Iteration / Loop Construct
The evaluation pattern of the until loop construct is opposite to that of while loop.
until [ <conditions> ]        OR while false
do
    <commands>
done
  • The for Iteration / Loop Construct
for var1 in value1<space>value2<space>value3<space>value4....
do
    <commands>
done
        => List of values should be separated by one or more blanks or spaces.

OR

for var1 in `<command>`
do
    <commands>
done

OR

for ((start_expr;check_expr;incre_expr))
do
    <commands>
done

OR

for var1 in `seq <FIRST> <INCREMENT> <LAST>`
do
    <commands>
done
    => seq command can omit FIRST & INCREMENT values, default is 1.



Parameter Handling
  • Parameters or arguments to a shell script are numbered as $0, $1, $2 etc.
  • Command name is put into variable $0, further arguments in $1, $2, etc are positional parameters of command line.
  • Linux shell creates maximum of nine variables other than $0 (upto $9).
  • $* contains entire string of arguments (expect $0 command name)
  • $# contains number of arguments passed to command (expect $0)
  • 'shift' command is used to move positional parameters one position up (i.e. $2, $3, $4... in $1, $2, $3). It is more useful when there are more than 9 arguments (as limited only till $9) or to key in variable number of arguments.
As a result of it, value of '$#' will be reduced by 1 and the first word in '$#' will be deleted.

Controlling Processes

  • Background processing can be done by passing '&' at the end of the execution of process or command.
  • It prints result as '[job_ID] process_ID'. job_ID is job number in queue for background processing.
  • Foreground process can be sent to background by pressing 'Ctrl+Z'. While background process can be brought to foreground using 'bg' command.
'bg %<job_ID>' command is used to start the stopped process.
'fg %<job_ID>' command is used to execute the process in foreground.
  • Terminating a process (or background process) is done using command 'kill <process_ID>' or 'kill <job_ID>'. Kill command sends 'TERM' signal to process
  • 'kill -9 <process_ID>' is used to send 'KILL' signal which can't be caught, a sure way of killing a process. This is used in case some processes are reading 'TERM' signal of 'kill' command and ignoring it.
  • 'time <other_commands>' used to find time elapsed between start and end of passed command(s). It displays the 'real' time, 'user' CPU time and 'sys' system CPU time. It is used to know performance of passed command or shell script.


  1. TFTP Server setup on Ubuntu


Checking existence of TFTP

  1. Check if TFTP is running
$ netstat -anu | grep ":69 "
Output of this should list anything listening on udp port 69 (as follows), if TFTP is running
udp      0 0 0.0.0.0:69     0.0.0.0:* <-- IPv4 case
udp6     0 0 :::69          :::* <-- IPv6 case

Installing and Configuring TFTPD Server

  1. Install following packages
$ sudo apt-get install xinetd tftpd tftp

  1. Create /etc/xinetd.d/tftp and put this entry
service tftp
{
protocol        = udp
port            = 69
socket_type     = dgram
wait            = yes
user            = nobody
server          = /usr/sbin/in.tftpd
server_args     = /tftpboot
disable         = no
}

  1. Create a folder /tftpboot (whatever given in server_args)
$ sudo mkdir /tftpboot
$ sudo chmod -R 777 /tftpboot
$ sudo chown -R nobody /tftpboot

  1. Restart the xinetd service
$ sudo /etc/init.d/xinetd stop
$ sudo /etc/init.d/xinetd start

This makes tftp server up and running.


Testing TFTP Server

  1. Create a test file with some content in /tftpboot path of the tftp server
$ ls / > /tftpboot/test
$ sudo chmod -R 777 /tftpboot
$ ls /tftpboot/test -lh
-rw-r--r-- 1 thalib thalib 159 2010-03-05 20:48 test

  1. Perform tftp operation
Obtain the ip address of the tftp server using ifconfig command (for example ip address as 192.168.1.2)

Now in some other system follow the following steps.
$ tftp 192.168.1.2
tftp> get test
Sent 159 bytes in 0.0 seconds
tftp> quit
OR
$ tftp -r test -g 192.168.1.2

$ ls test -lh
-rw-r--r-- 1 thalib thalib 159 2010-03-05 20:48 test

This confirms tftp server is working fine.

Additional TFTP Server configuration

  1. Updating blocksize
Default blocksize is 512 bytes
To change blocksize, pass '--blocksize <size>' in "server_args" within /etc/xinetd.d/tftp
And then restart the xinetd service as mentioned in step 5

References:
  1. "How do I check if TFTP is running in Linux?" https://www.quora.com/How-do-I-check-if-TFTP-is-running-in-Linux
  2. "Installing and Testing TFTP Server in Ubuntu/Debian" https://mohammadthalif.wordpress.com/2010/03/05/installing-and-testing-tftpd-in-ubuntudebian/



  1. tftpd-hpa


tftpd-hpa is the better tftp server with enhanced features


  1. 'screen' command on Linux


  1. List running screens
$ screen -list OR
$ screen -ls
There are screens on:
        4711.pts-4.sysname       (11/14/2018 00:11:00 PM)       (Detached)
1 Sockets in /run/screen/S-sysuser.

If empty screen list
$ screen -list
"No Sockets found in /run/screen/S-sysuser." OR
"No Sockets found in /var/run/screen/S-sysuser.

  1. Creating new screen (without specifying name)
$ screen
$ screen -list
There are screens on:
        40540.pts-8.sysname      (11/14/2018 00:22:00 PM)       (Attached)
        4711.pts-4.sysname       (11/14/2018 00:11:00 PM)       (Detached)
2 Sockets in /run/screen/S-sysuser.

  1. Creating new screen with specifying name
$ screen -S ANIKET_DEBUG
$ screen -list
There are screens on:
        27674.ANIKET_DEBUG      (11/14/2018 00:33:00 PM)        (Attached)
        40540.pts-8.sysname      (11/14/2018 00:22:00 PM)       (Attached)
        4711.pts-4.sysname       (11/14/2018 00:11:00 PM)       (Detached)
3 Sockets in /run/screen/S-sysuser.

  1. Detaching from running screen
Ctrl+a --> d
[screen is terminating]

  1. Reattaching any detached screen
$ screen -r 27674.ANIKET_DEBUG
$ screen -r 40540.pts-8.sysname

  1. Reattaching any detached screen using name
$ screen -r ANIKET_DEBUG
$ screen -r pts-8.sysname
$ screen -r pts-8

  1. Reattaching any attached screen (with or without name) ALWAYS FAILS
$ screen -r pts-8
There is a screen on:
        40540.pts-8.sysname      (11/14/2018 00:22:00 PM)       (Attached)
There is no screen to be resumed matching pts-8

  1. "Remote Detached" of attached screen (with or without name)
$ screen -d pts-8
[40540.pts-8.sysname detached.]

  1. Remotely sending command to a named session
screen -S <name> -X <command>
$ screen -S pts-0 -X kill To "kill" only one screen screen window
$ screen -S pts-8 -X quit To "quit" (or kill) the complete session

  1. "Ultimate Attach"
$ screen -dRR

Attaches to a screen session.
If the session is attached elsewhere, detaches that other display.
If no session exists, creates one.
If multiple sessions exist, uses the first one.