Showing posts with label Linux Fundamentals. Show all posts
Showing posts with label Linux Fundamentals. Show all posts

Tuesday, January 17, 2017

Working with Process, CPU and Memory in Linux



# ps aux  => List all the processes

# pkill <<process_name>>   => will find all process with the name mentioned and kill

# kill –l  =>  Will list all available signals


# kill -9 <<PID>>   =>  Kills the process corresponding to the PID mentioned

# pkill –t <<ptty_id>>  => ptty_id can be found using the command w. This will return all logged in users and their terminal Ids

# pkill  -u  <<user_name>>  <<process_name>>   => will kill the process specified for the specified user





Working with job



# job  =>  this will list the background jobs of the user with the number

# kill –SIGSTOP  %1   =>  Will kill the process  %1 is the process number

# job  => Will display the job status as stopped




Vi Editor

Keys tips for using VI editor

yy        => Copy the entire line
P          => paste above the cursor
p          => paste below the cursor
dd       => Cut the entire line
ndd     => Cut n number of lines starting from cursor location
G         => Go bottom or last line of the file
A         => End of the line where cursor is located


:/serachword                                              => will search for the word
:/set nu                                                      => set numbers to the file
:%s/wordToFind/WordToReplace              => find and replace


Linux User Adminstration



  
There are two type of users in Linux. They are System users and Normal Users. Each user will have a unique ID called UID.
User Type
UID Range
System Users
0 to 999
Normal User
1,000 to 60,000


The user information is stored in the following files.
/etc/passwd – User information is stored
/etc/shadow – password information of the users is stored in this file
Let us discuss more about each file.

In /etc/passwd file, the user details are present in the below format.


Here UID and GID are auto generated by the system. Every user will have a unique user ID (UID) and group ID (GID).
The default home directory in Linux is /home/<username>.



In /etc/shadow file, the encrypted user password details are present.



There are few tools like DES (64 bits), MD5 (128 bits), SHA (512 bits) which will encrypt the password in linux. In RHEL 7, SHA is the default tool.

The tool used to encrypt the password of a specific user can be found using the command

passwd –S <username>

the output looks like the below.




Below are the basic command for user administration.

Create a user:  useradd <username>

change the user password: passwd <username>

lock the user account: usermod –L <username>

Verification: The !mark before the encrypted password represents the account is locked.

Now let’s check the usermod command to change each property of /etc/passwd file.
1)      Change the user name: usermod -l <new_name> <old_name>
2)      Change the user ID: usermod –u <new_uid> -o <user_name> (-o is applied when the same UID is used for two users).
3)      Change the comments of a user: usermod –c “<new_comments>” <user_name>
4)      Change the user home directory: usermod -m -d <new_directory_fullpath> <user_name>
   (
here –m to move the current home directory content to new home directory)
the default home directory information can be found in the file /etc/default/useradd




5)      Delete a user including home directory and mail spool

userdel –r <<User_Name>>


6)      Change the user login shell: chsh –s <new_shell_name> <user_name>

The available shells on the linux box can be found at /etc/shells


Password information:

chage –l  <<username>>








Change the maximum days of the passwd expiry.



Groups:

The groups information is found in the fail /etc/group

create a new group
# groupadd <<New_Group_Name>>

Append a secondary group to the user

# usermod –aG <<Secondary_Group>> User_Name

List all groups a user belongs to. Here
testacl is the user



Set-GID for directories:

By doing this. The newly created child objects will have the current group of dir1

Top Command in Linux





Top command will give the list of process with nice level, CPU utilization, and memory utilization.

First line contains the uptime, number of users logged in, load average (1min, 5 min, 15 min)




Calculate load on multiple CPU machine.

Step 1) get number of processors from the below file.

cat /proc/cpuinfo | grep “model name” | wc –l




Step 2)     (average load) / (number of processors)


Lines of top command:


Line 1: 
uptime, number of users logged in, load average (1min, 5 min, 15 min)
Line 2:  task information (total, running, sleeping and stopped)
Line 3:  CPU information
Line 4:  Memory information
Line 5:  Swap information

Sort process by Memory usage/CPU

enter shift + m  to sort by memory usage


enter shift + p  to sort by CPU usage



Re-nice a particular process:

Step 1) Before renice the value of PID 12 is 10.



Step 2)
enter r to renice. Then enter new pid (example: 12) when prompted.



 Step 3) provide new nice value and hit enter



Step 4) The nice lave will be set.









Set delay time for refersh

step 1)
type d and enter. It will prompt to give new delay time. Then type new value and hit enter.






Find the full path of command in top result.

Type c and hit enter





Top result with color code

type z




SSH, SCP and SFTP Syntax


  
1)       SSH Syntax





2)       SCP syntax







3)      
SFTP Syntax






sftp> get /path-file-name-from-target-computer


sftp> put /path-file-name-from-our-computer
sftp> bye

Schedule Tasks in Linux




Using at:

# at now +5 days 
= > will schedule a task after 5 days from now
at> reboot  => enter ctl+d to come out of at utility



#atq  => will query active jobs
#atrm <<job_id>>     => will remove the job







Using Cron:













Delete the Logical volume, physical volumes and remove Physical Disk



Step 1) Choose the physical volume to delete. Also determine in which volume group it is added.


Step 2)  Unmounts the file system


Step 3)  remove the logical volume.


Step 4) remove physical volume from volume group


Step 5) Remove physical volume




Step 6) remove partition


delete




Nice level of process




Nice:


It is the method of prioritizing a process in terms of CPU and Memory.

Nice level ranges from -20 to 19. Where -20 is the highest priority and 19 is the least priority.

List the nice level of process

# ps axo pid, comm, nice   =>  Will return the process id, command, nice level


Set nice level to a process

# nice –n -10
<<Process_name>>




set the nice level of a particular running process

# renice –n 12 <<PID>>



set the nice level of all running process

 # renice –n 13 ${pgrep <<process_name>>}


Note: Only privileged user can set the nice level

MBR and GPT partition


Master Boot Record (MBR):

This is the most command and old partition mechanism. The limit is for primary partition can have only 2048 GB of maximum size. This is a 32 bit based and can have only four primary partitions.




Global Partition Table (GPT):

This is a new partition method and 64 bit based partition. This can have 128 primary partitions.

Managing services



 Find the status of a service.




Start the service




 Stop the service



 List all services


 List all dependencies of a service


 

Disable and enable service


Find if a specific service is a dependency for a particular target










Logical Volume Manager (LVM)


 
New we are going to see how to create the new partitions in Linux using LVM.


Below are the steps to configure the new hard disk on Linux:

Step 1) Take a physical disk (PD)

Step 2) Physical Partition (PP) the PD using fdisk for MBR partitioning or gdisk for GPT partitioning.




Step 3) Create physical volume (PV)



validate

Step 4) Create a volume group (VG)

Step 6) Create a logical volume (LV)



Step 7) Create a file system type.


Step 8) Create a mount point. This is a directory where the above formatted logical volume is supposed to point



Step 9) mount the logical volume to the mount point.



 Step 10) to make the file system persistent. Add the entry to fstab.