0% found this document useful (0 votes)
37 views

Linux Unit III (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Linux Unit III (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

UNIT III

 FILE MANIPULATIONS
 MANAGING RUNNING PROCESSES- PROCESS
MANAGEMENT
 WRITING SIMPLE SHELL SCRIPTS- UNDERSTANDING
SHELL SCRIPTS
 UNDERSTANDING SERVER MANAGING IN RHEL
 REMOTE SERVER MANAGEMENT
 INITIAL SERVER CONFIGURATION FILE TRANSFER
 INSTALL THE SERVER RHEL
Create, View, and
Edit Text Files
Standard Input, Standard Output, and
Standard Error
Standard Input, Standard Output,
and Standard Error
Redirect Output to a File

 The Input/Output (I/O) redirection changes how the process gets its input or output.
Instead of getting input from the keyboard, or sending output and errors to the terminal, the
process can read from or write to files. With redirection, you can save the messages to a
file instead of displaying the output on the terminal. Alternatively, you can use
redirection to discard output or errors, so they are not displayed on the terminal or saved.

 You can redirect a process stdout to suppress the process output from appearing on
the terminal. If you redirect stdout to a file and the file does not exist, then the file is created.
If the file does exist and the redirection does not append to the file, then the redirection
overwrites the file's contents. To discard the output of a process, you can redirect to the
empty /dev/null special file that discards channel output that is redirected to it.

 As viewed in the following table, redirecting only stdout does not suppress displaying stderr
error messages on the terminal.
Standard Input, Standard Output,
and Standard Error
Standard Input, Standard Output,
and Standard Error
Redirect Output to a File or Program

 > - content overwritten


 >> - content gets appended
Redirect Output to a File or Program

Redirect the success msg to one


file and the failure msg to
another file

Process that is using the most cpu


-b represents batch mode
-n 1 -> implies one iteration
Redirect Output to a File or Program

 To show the properties of the current


system
Redirect Output to a File or Program

 | tee my system  | is the pipe


command. tee command sends
the output to the system file and
at the same time prints the
output
 tee command will overwrite the
existing contents of the file
Redirect Output to a File or Program

 tee –a my_system  -a will


append the contents to the
system file.
Redirect Output to a File or Program

 Finds the file that is greater


than size 100Mb

 Error msg gets directed to the /dev/null.


 Dev/null is like the black hole of Linux
where information gets discarded.
 Only success msg gets printed.
Redirect Output to a File or Program

 &> - stdout and stderror is redirected


to 50MB_plus_files.txt

 less command used to read contents of


text file one page (one screen) per time
Redirect Output to a File or Program

 wc command counts the number of


lines inside the file. The result shows
132 lines
 Count the number of lines excluding the
comment statements.
Redirect Output to a File or Program

 EOF – End Of File command. It is a


condition used to indicate that there
are no more data to read from a file
or input stream. When a program
reaches EOF while reading, it knows
that it has reached the end of the
data.

 > -- indicates output redirection


 < -- indicates input redirection
Edit Text Files from the Shell Prompt
Get Started with Vim

 You can install the Vim editor in Red Hat Enterprise Linux by using either of two packages. These two
packages provide different features and Vim commands for editing text-based files.
 With the vim-minimal package, you might install the vi editor with core features. This lightweight
installation includes only the core features and the basic vi command. You can open a file for editing by
using the vi command:
 [user@host ~]$ vi filename
 Alternatively, you can use the vim-enhanced package to install the Vim editor. This package provides a
more comprehensive set of features, an online help system, and a tutorial program. Use the vim command
to start Vim in this enhanced mode:
 [user@host ~]$ vim filename
 The core features of the Vim editor are available in both commands.
 If vim-enhanced is installed, then a shell alias is set so that if regular users run the vi command, then they
automatically get the vim command instead. This alias does not apply to the root user and to other users
with UIDs below 200 (which system services use).
 If vim-enhanced is installed and a regular user wants to use the vi command, then they might have to use
the \vi command to override the alias temporarily. You can use \vi --version and vim --version to compare
the feature sets of the two commands.
Vim Operating Modes

 When you first open Vim, it starts in command mode, used for
navigation, cut and paste, and other text modifications. Pressing the
required keystroke accesses specific editing functions.
• An i keystroke enters insert mode, where all typed text becomes file
content. Pressing Esc returns to command mode.
• A v keystroke enters visual mode, where multiple characters might be
selected for text manipulation. Use Shift+V for multiline
and Ctrl+V for block selection. To exit the visual mode, use
the v, Shift+V (enters into visual mode), or Ctrl+V keystrokes.
• The : keystroke begins extended command mode for tasks such as
writing the file (to save it) and quitting the Vim editor.
• If you are unsure which mode Vim is using, then press Esc a few times
to get back into command mode. It is safe to press the Esc key in
command mode repeatedly.
The Minimum, Basic Vim Workflow

 Vim has efficient, coordinated keystrokes for advanced editing tasks. Although considered
beneficial with practice, the capabilities of Vim can overwhelm new users.
 Red Hat recommends that you learn the following Vim keys and commands:
 The u key undoes the most recent edit.
 The x key deletes a single character.
 The dd command deletes the line
 The p command pastes the line. 5 p – 5 times pasting the line.
 cw – deletes the word and waits for another word to be inserted.
 The :w command writes (saves) the file and remains in command mode for more editing.
 The :wq command writes (saves) the file and quits Vim.
 The :q! command quits Vim, and discards all file changes since the last write.
 The d$ deletes the sentence from current cursor till the end of the line.
 Shift + v, > -> creates space
 set nu - command will enable numbering of all the lines
Guided Exercise: Edit Text Files from
the Shell Prompt
Guided Exercise: Edit Text Files from
the Shell Prompt
Guided Exercise: Edit Text Files from
the Shell Prompt
Change the Shell Environment
Assign Values to Variables

 Variable names can contain uppercase


or lowercase letters, digits, and the
underscore character (_)
 You can also use a variable to refer to a
long file name for multiple commands.
 To unset and unexport a variable, use
the unset command:
Shell Variable Usage

 With the Bash shell, you can set shell variables to help to run
commands or to modify the behavior of the shell. You can also export
shell variables as environment variables, which are automatically
copied to programs that are run from that shell. You can use variables
for ease of running a command with a long argument, or to apply a
common setting to commands that are run from that shell.

 Shell variables are unique to a particular shell session. If you have two
terminal windows open, or two independent login sessions to the same
remote server, then you are running two shells. Each shell has its own
set of values for its shell variables.
Shell Variable Usage

 $HISFILESIZE - variable that gives the file size.


 Stored in .bash_history file in your home directory
 The scope of the variable is within the particular shell
where it is defined.
 To make the variable applicable across different shell
environment.
 HISTFILESIZE = 10000
 HISTTIMEFORMAT = “%F %T ” – stores the date and
time format.
 Alias sc= “grep ^[^#] $1” – instead of running the
entire grep command, u can use only sc filename.
 mcd() function here makes the directory and changes
into the directory.
Shell Variable Usage

 By default crontab –e will take u to vim editor


 export command will make the variables available to your shell and programs that are started
from the particular shell
 To change the editor, use the export command
 After making changes, make sure you make a new connection
 The commands doesn’t work when the path where
the commands are declared is not properly defined.
 In such cases, we can use the export command.
 $ env – used to check all the environment
variables declared.
 It is important to declare these variables in the
configuration file. For bash shell, it is ~/.bashrc
file. Use appropriate login script suitable for each
shell
Summary

 Running programs, or processes, have three standard communication channels: standard input, standard
output, and standard error.

 You can use I/O redirection to read standard input from a file or to write the output or errors from a process
to a file.

 Pipelines can connect standard output from one process to the standard input of another process, and can
format output or build complex commands.

 Know how to use at least one command-line text editor, and Vim is the recommended option because it is
commonly installed by default in Linux distributions.

 Shell variables can help you to run commands, and are unique to a shell session.

 You can modify the behavior of the shell or the processes with environment variables.
18.MANAGING
RUNNING PROCESSES
Content:
• Introduction to Process
• Process Management
Introduction To Process

Understanding Processes

• A process is a running instance of a command.

• A process is identified on the system by what is referred to as


a process ID (PID).

• That PID is unique for the current system. In other words, no


other process can use that number as its process ID while that
first process is still running.
Introduction To Process

Understanding Processes

• Each process, when it is run, is associated with a particular


user account and group account.

• That account information helps determine what system


resources the process can access.
Process Management

1. Monitoring Process with Process state command

2. Monitoring Process with Process state with -a option to


list all process running on terminal
Process Management
3. Monitoring Process with Process state with -u option to list all
process ran by user.

4. Monitoring Process with Process state with -x option to list all


process on operating system
Process Management

5. Monitoring Process with Process state using the combination of options


Process Management

6. Monitoring Process with Process state with -ef option to list process with PPI
Process Management
7. Monitoring Process with TOP utility for viewing process as task view
Process Management

Killing processes with kill and killall

• Although usually used for ending a running process, the kill


and killall commands can actually be used to send any valid
signal to a running process.

• Besides telling a process to end, a signal might tell a process


to reread configuration files, pause (stop), or continue after
being paused, just to name a few possibilities.
Process Management

Killing processes with kill and killall

• Signals are represented by both numbers and names.

• Different processes respond to different signals. Processes


cannot block SIGKILL and SIGSTOP signals,

• Most x86 and Power architectures, use the middle value. The
first value usually works for Alpha and SPARC, while the last
one is for MIPS architecture.
19.WRITING SIMPLE
SHELL SCRIPTS
Content:
• Understanding Shell Scripts
• Shell Operations
Understanding Shell Scripts
Understanding Processes

Shell scripts are the equivalent of batch files in Windows and can
contain long lists of commands,

• complex flow control,


• arithmetic evaluations,
• user-defined variables,
• user-defined functions,
• and sophisticated condition testing.
Understanding Shell Scripts
Understanding Processes

• Shell scripts are capable of handling everything from simple


one-line commands to something as complex as starting up a
Linux system.

• Although dozens of different shells are available in Linux, the


default shell for most Linux systems is called bash, the
Bourne Again SHell.
Understanding Shell Scripts

Executing and debugging shell scripts

• One of the primary advantages of shell scripts is that they can


be opened in any text editor to see what they do.

• A big disadvantage is that large or complex shell scripts often


execute more slowly than compiled programs.
Understanding Shell Scripts
Executing and debugging shell scripts

You can execute a shell script in two basic ways:

 The filename is used as an argument to the shell (as in bash


myscript). In this method, the file does not need to be
executable; it just contains a list of shell commands.

 The shell script may also have the name of the interpreter
placed in the first line of the script preceded by #! (as in
#!/bin/bash) and have the execute bit of the file containing the
script set (using chmod +x filename).
Understanding Shell Scripts

Understanding shell variables

Variable names within shell scripts are case sensitive and can be
defined in the following manner
NAME=value

The first part of a variable is the variable name.

The second part is the value set for that name.


Understanding Shell Scripts

Special shell positional parameters

There are special variables that the shell assigns for you. One set
of commonly used variables is called positional parameters or
command-line arguments, and it is referenced as $0, $1, $2,
$3. . .$n. $0 is special

#!/bin/bash
# Script to echo out command-line arguments
echo "The first argument is $1, the second is $2."
echo "The command itself is called $0."
echo "There are $# parameters on your command line"
echo "Here are all the arguments: $@"
Shell Operations
Performing arithmetic in shell scripts

Bash uses untyped variables, meaning that you are not required to
specify whether a variable is text or numbers. It normally treats
variables as strings of text.

A variable incrementally is to use $(()) notation with ++I added to


increment the value of I. Try typing the following:

$ I=0
$ echo "The value of I after increment is $((++I))"
The value of I after increment is 1

$ echo "The value of I before and after increment is $((I+


+)) and $I"
The value of I before and after increment is 1 and 2
Repeat either of those commands to continue to increment the
value of $I.
Shell Operations

Performing arithmetic in shell scripts

A variable incrementally is to use $(()) notation with ++I added


to increment the value of I. Try typing the following:

$ I=0
$ echo "The value of I after increment is $((++I))"
The value of I after increment is 1

$ echo "The value of I before and after increment is $((I+


+)) and $I"
The value of I before and after increment is 1 and 2
Shell Operations
Write and execute a simple bash script
1. Use the vim command to create the firstscript.sh file under your
home directory.

2. . Insert the following text, and save the file. The number of hash signs
(#) is arbitrary.
Shell Operations

3. Use the sh command to execute the script.

4. Review the output file that the script generated.


Shell Operations
Redirect the output of a simple Bash script to a file.

1. Use the Vim text editor to edit the firstscript.sh script.

2. Insert the following text inside the firstscript.sh file:


Shell Operations
3. Make the firstscript.sh file executable by using the chmod command.

4. Execute the firstscript.sh script

5. Review the output file that the script generated.


Shell Operations

Create a for loop to execute the hostname command on


the servera and serverb machines.

Create a shell script to execute the same for loop

1. Create a shell script named as ‘printhostname.sh’


Shell Operations
2. Insert the following text inside the ‘printhostname.sh’ file.
Shell Operations

3. Make the ‘printhostname.sh’ file executable by using


the chmod command.

4. Executes the ‘printhostname.sh’ file:


Chapter 9
20.UNDERSTANDING
SERVER MANAGING IN
Content:
RHEL
• Initial Server Configuration
• Remote Server Management
• File Transfer
• Log Management
• Server Monitoring
Initial Server
Configuration

Using configuration files

• Linux servers have been configured by editing plain-text files in the


/etc directory (or subdirectories).

• The one downside to plain-text configuration files is that you don’t get
the kind of immediate error checking you get when you use graphical
administration tools.
Initial Server
Configuration

Checking the default configuration

• Most server software packages in Fedora and RHEL are installed with
minimal configuration and lean more toward being secure than totally
useful out of the box.

• Because RPM packages are designed to be installed unattended, the


person installing the package has no choice on how it is installed.

• The files are installed in set locations, specific user accounts are
enabled to manage it, and when you start the service.
Initial Server
Configuration
Start the server

Most services that you install in Linux are configured to start up when
the system boots and then run continuously, listening for requests,
until the system is shut down.

Most, but not all, services are implemented as daemon processes.

Here are a few things that you should know about those processes:

• User and group permissions


• Daemon configuration files
• Port numbers
Initial Server
Configuration
Secure the server

Opening your system to allow remote users to access it over the


network is not a decision to be taken lightly.

Crackers all over the world run programs to scan for vulnerable
servers that they can take over for their data or their processing
power.

Password protection

Good passwords and password policies are the first line of defense
in protecting a Linux system.

A good technique is to disallow direct login by root and require


every user to log in as a regular user and then use su or sudo to
become root.
Initial Server
Configuration

Firewalls

The iptables firewall service can track and respond to every packet
coming from and going to network interfaces on your computer.

Using iptables, you can drop or reject every packet making requests for
services on your system except for those few that you have enabled.
Initial Server
Configuration
TCP Wrappers

TCP Wrappers, which uses /etc/hosts.allow and /etc/hosts.deny files to


allow and deny access in a variety of ways to selected services, was
used primarily to secure older UNIX services, and it is no longer
considered to be very secure.

SELinux

Red Hat Enterprise Linux, and other Linux distributions come with the
Security Enhanced Linux (SELinux) feature included and in Enforcing
mode.

A major function of SELinux is to protect the contents of your Linux


system from the processes running on the system
Remote Server
Management
Managing Remote Access with the Secure Shell Service

The Secure Shell tools are a set of client and server applications that allow
you to do basic communications between client computers and your Linux
server.

The tools include ssh, scp, sftp, and many others.

Most Linux systems include secure shell clients, and many include the
secure shell server as well.
# yum list installed | grep openssh
...
openssh.x86_64 7.9p1-5.fc30 @anaconda
openssh-clients.x86_64 7.9p1-5.fc30 @anaconda
openssh-server.x86_64 7.9p1-5.fc30 @anaconda
Remote Server
Management

Starting the openssh-server service

Linux systems that come with the openssh-server package already


installed sometimes are not configured for it to start automatically.

systemctl status sshd.service


systemctl start sshd.service
systemctl enable sshd.service
Remote Server
Management

Using SSH client tools

Many tools for accessing remote Linux systems have been


created to make use of the SSH service.

The most frequently used of those tools is the ssh command,


which can be used for remote login, remote execution, and
other tasks.
Remote Server
Management

Using ssh for remote login

Use the ssh command from another Linux computer to test that you can
log in to the Linux system running your sshd service.

The following is an example of remotely logging in to johndoe’s account


on 10.140.67.23:

$ ssh [email protected]
[email protected]'s password: *********

When you are finished, type exit to end the remote connection.
File Transfer

Copying files between systems with scp and rsync

SCP:

• OpenSSH is useful for securely running shell commands on remote


systems.

• The Secure Copy command, scp, copies files from a remote system to
the local system.

[user@host ~]$ scp /etc/yum.conf /etc/hosts


remoteuser@remotehost:/home/remoteuser
remoteuser@remotehost's password: password
yum.conf 100% 813 0.8KB/s 00:00
hosts 100% 227 0.2KB/s 00:00
File Transfer

Rsync

• The rsync command is another way to securely copy files from one
system to another.

• The tool uses an algorithm that minimizes the amount of data copied
by synchronizing only the changed portions of files.

For example, to synchronize contents of the /var/log directory to the /tmp


directory:

[root@host ~]# rsync -av /var/log /tmp


[user@host ~]$ ls /tmp
log ssh-RLjDdarkKiW1
File
Transfer
Interactive copying with sftp

• To interactively upload or download files from a SSH server, use the


Secure File Transfer Program, sftp.

• The command will attempt to log in using your local user name as the
remote user name.

• You will then be presented with an sftp> prompt.

[user@host ~]$ sftp remoteuser@remotehost


remoteuser@remotehost's password: password
Connected to remotehost.
sftp>
Log Management

Configuring System Logging

• System logging is one of the basic services configured for Linux


to keep track of what is happening on the system.

• The rsyslog service (rsyslogd daemon) provides the features to


gather log messages from software running on the Linux
system
Log Management

Enabling system logging with rsyslog

• Configuring rsyslogd to record varying levels of detail in the log


files is possible. It can be told to ignore all but the most critical
messages.

• The rsyslogd daemon can even accept messages from other


computers on your network.

• This remote logging feature is particularly handy because it


enables you to centralize the management and review of the
log files from many systems.
Server Monitoring

Checking System Resources with sar

The sar command is part of the sysstat package.

When you install sysstat and enable the sysstat service, your system
immediately begins gathering system activity data that can be reviewed
later using certain options to the sar command.

# systemclt enable sysstat


# systemctl start sysstat
Shell Operations

Creating a Menu using Shell Programming.

You might also like