Linux Unit III (1)
Linux Unit III (1)
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
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
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
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
Understanding Processes
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
• 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,
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
Variable names within shell scripts are case sensitive and can be
defined in the following manner
NAME=value
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.
$ I=0
$ echo "The value of I after increment is $((++I))"
The value of I after increment is 1
$ I=0
$ echo "The value of I after increment is $((++I))"
The value of I after increment is 1
2. . Insert the following text, and save the file. The number of hash signs
(#) is arbitrary.
Shell Operations
• 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
• 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.
• 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.
Here are a few things that you should know about those processes:
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.
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
SELinux
Red Hat Enterprise Linux, and other Linux distributions come with the
Security Enhanced Linux (SELinux) feature included and in Enforcing
mode.
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.
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
Use the ssh command from another Linux computer to test that you can
log in to the Linux system running your sshd service.
$ ssh [email protected]
[email protected]'s password: *********
When you are finished, type exit to end the remote connection.
File Transfer
SCP:
• The Secure Copy command, scp, copies files from a remote system to
the local system.
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.
• The command will attempt to log in using your local user name as the
remote user name.
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.