Thursday, July 17, 2014

Road to RHCE - Day 8

Day 8

Network Configuration

IP classes

Class          Range           Default subnet mask        Prefix

Class A  -> 1 to 126     -> 255.0.0.0                      -> 8
Class B  -> 128 to 191 -> 255.255.0.0                  -> 16
Class C  -> 192 to 223 -> 255.255.255.0              -> 24
Class D  -> 224 to 235
Class E  -> 236 to 254

255 -> broadcast address

127 -> loopback address


Subnetting -> dividing a large network into small networks
Subnet Mask -> unique id for such small networks


To configure network using TUI client

system-config-network

[! there are 2 options available - device & dns configuration]

Device configuration


Name                             : eth0
Device                            : eth0
Use DHCP                      : None
Static IP                         : 192.168.0.109
Netmask                        : 255.255.255.0
Default gateway IP         : 192.168.0.254
Primary DNS server        : 192.168.0.254
Secondary DNS server    : 192.168.0.254

[! this is a sample setup that uses static IP]


To update the changes

service network restart


To check the IP of the system

ifconfig
ip addr show


Associated config files

1. /etc/sysconfig/network-scripts/ifcfg-eth0
[! to configure the ip]

2. /etc/sysconfig/network
[! contains the hostname of the system]

3. /etc/resolv.conf
[! contains the DNS info]



To get the current hostname of the system

hostname


To know the status of all configured devices

service network status



-- End of Day 8 --

-- End of SA1 --

Wednesday, July 16, 2014

Road to RHCE - Day 7

Day 7



Backup and Compression


To check the directory size

du -sh /etc/


To create an archive

tar -cvf [archive_name] [list_of_files]


To know the type of a file

file [filename]


To extract the contents of a tar file to a different path

tar -xvf [archive_name] -C [destination_path]


To compress an archive (or file)

zip [zip_filename] [source_filename]


To decompress a compressed file

unzip [zip_filename]


Using gzip (only for archives)

gzip [archive_filename]

[! gunzip [filename] to decompress]


Using bzip2 (more compression than gzip)

bzip2 [archive_filename]

[! bunzip2 [filename] to decompress]


To compress while archiving

tar -cvfz [archive.tar.gz] [list_of_files]

[! z -> gzip; j -> bzip2]


Process Management

To monitor the processes via gui

gnome-system-monitor


To list the processes running in current session

ps

[! ps -a for all active processes across sessions; ps a for all processes (including inactive ones) across sessions]


To list the parent proces id and the owner info

ps -f


To list the processes in a tree format

ps af


To list all processes including system ones

ps x


To know the session detail

w


To know users login info

lastlog


To run a process in background

[process/service_name] &


To list the background processes started by users

jobs


kill

To list all the available options with kill command

kill -l


Below are few of the important options

9   -> kill
15 -> terminate
18 -> continue
19 -> stop (temporarily)


To stop a job/process temporarily

kill -19 %[job_id | proces_id]



To resume the stopped job/process

kill -18 %[job_id | process_id]


To make a background process run in the foreground

fg %[job_id]

[! Ctrl+z -> to stop temporarily; bg %[job_id] to take that to background]


To terminate a process

kill %[job_id]
kill [process_id]

[! kill command defaults to option 15, if nothing else is specified]


To force kill a process

kill -9 [process_id]


To kill using the process name

pkill -9 [process_name]


top

To list the running processes

top

[! the display gets refreshed every 3 sec; top -d 1 to refresh the display every 1 sec]


To kill the process based on id

k


To sort top output based on memory utilised

Shift+M


To sort based on CPU usage

Shift+C


To sort based on user

U



-- End of Day 7 --

Tuesday, July 15, 2014

Road to RHCE - Day 6

Day 6


bash configuration

bash is one of the login shells available for users in linux & the most commonly used shell as well.

Below are the important configuration files for bash. These files are present under every user's home directory -

.bashrc                           - contains the session configuration of the user
.bash_profile                  - contains the profile info of the user
.bash_logout                  - contains the commands/actions to be executed at end of the session (logout)
.bash_history                 - history of commands used by the user (this file will be populated only during the first successful logout)


There are also global shell configuration that can be set for all users via below file -

/etc/bashrc
/etc/profile


inode

inode -> index node


To check the inode of a file or directory

ls -i [filename]


Links

There are two types of links -

hard link
soft link

hard link

  • points directly the memory location of the file
  • same inode as that of the physical file
  • can be accessible even if the actual file is deleted
  • not possible to create hard links for directories
  • like a backup for the physical file
  • occupies the same space as that of the file


soft link

  • points to the file
  • different inode than the physical file
  • can not be accessible if the physical file is deleted
  • it is possible to create soft link for directories
  • does not occupy additional disk space


To create a hard link

ln [filename] [link_name]


To create a soft link

ln -s [filename] [link_name]


Special Permissions

There are three special permissions that can be set for files & directories -

Special userid (suid)    -> owner
Special groupid (sgid) -> group
Sticky bit                     -> others


Special userid

If set, the file will get executed based on the owner's permission and not based on the executor's permission.


To set special user id

Symbolic Method -> chmod u+s [filename]
Numeric Method  -> chmod 4755 [filename]

[! the first digit (4 for owner, 2 for group, 1 for others) denotes the special permission]


Special groupid

If set for a directory, all the future files/directories that gets created under that directory will have the group owner same as that of the parent directory.


To set special groupid

Symbolic Method -> chmod g+s [directory]
Numeric Method  -> chmod 2755 [directory]


Sticky bit

If set for a directory whose content have permission set as 777, other users can edit the contents of that directory but cannot remove or rename or move them.


To set sticky bit

Symbolic Method -> chmod o+t [directory]
Numeric Method  -> chmod 1777 [directory]


To flush the history of the current logged in user

history -c



-- End of Day 6 --

Monday, July 14, 2014

Road to RHCE - Day 5

Day 5

I/O redirection

To redirect  the output of a command to a file

hwclock > [filename]


To redirect the error from a command to a file

hwclok 2> [filename]


To redirect the standard input to a file

cat > [filename] << [eof_string]
blah blah
blah blah
[eof_string]


A sample shell script to create a text file with contents

#!/bin/bash
cat > test1 << END
This is the first line
This is the second line
END


Use of pipe ( | ) symbol

The pipe ( | ) symbol is used to redirect the output of first command as the input of the second.

date | cat > date.out

[! the above command will create the date.out file with the current date as it's content]


Text processing tools

There are many text processing tools in linux, like -

less
head
tail
more
cat
grep
sed
awk
cut
diff
tailf
wc
sort


To display top 'n' number of lines in a file

head -n [filename]


To display bottom 'n' number of lines in a file

tail -n [filename]


To search a string in a file

grep [string] [filename]


sed

The definition of sed is System Editor. It is used to find and replace a string in a file.


To search and replace only the first occurrence of the string of every line temporarily

sed 's/old_value/new_value' [filename]


To search and replace all occurrences of the string temporarily

sed 's/old_value/new_value/g' [filename]


To search and replace all occurrences of the string permanently

sed -i 's/old_value/new_value/g' [filename]


To delete a line (say, 3rd line) using sed

sed -i '3d' [filename]


To extract specific field from a text file

cut -d: -f6 [filename]

[! where : is the delimiter and 6 denotes sixth field to extract]



To find the difference between two files

diff [file1] [file2]


To get the statistics of a text file

wc [filename]

[! this will provide the number lines, characters, words that are present in that file]


To sort the contents of a file

sort [filename]


To find a file

find -name "*.txt"

find /home/ -user testuser

[! this command searches all files owned by testuser under /home]


Executing commands with 'find'

  • Commands must be preceded with either -exec or -ok (-ok prompts before acting on each file).
  • Command must end with ' \;' (a space, a backslash and a semicolon)
  • Can use {} as filename placeholder

find -size +100M -exec mv {} /tmp/ \;

[! the above command will search for any file that is >100MB in the current directory and move all those files to /tmp/]


To create a dummy file of size 1GB

dd if = /dev/null of = [target_filename] bs = 100M count = 10



-- End of Day 5 --

Friday, July 11, 2014

Road to RHCE - Day 4

Day 4


File Management

There are different ways to create a file -

vim [filename]
touch [filename]
cat > [filename]
[! when using 'cat' command, press Ctrl+D to save. '>>' to append to an existing file]


To create a directory

mkdir [directory_name]


To create multiple files using touch

touch filename{1..10}
[! this option will work with mkdir command as well]


To copy a file

cp [source_file] [target]


To copy a directory

cp -r [source_directory] [target]


To move/rename a file or directory

mv [source] [target]
mv [old_name] [new_name]


To delete a file

rm [filename]


To delete a directory

rmdir [directory_name]

 [! this works only on empty directory]

rm -rf [directory_name]

[! this works even on directory with contents]


--End of Day 4--

Thursday, July 10, 2014

Road to RHCE - Day 2 & Day 3

Day 2

I had to skip the second day due to unavoidable reason. Got to know from batch mates that below topics were covered on day 2 -

init levels
editors (gedit, vi, vim)
vim commands
basic file management (ls, cp, cd)
absolute path and relative path


--End of Day 2--


Day 3

Users & Group Management

Every user has an id called user id (same goes for 'group id' for each group)

uid of root user -> 0
uid of system (default) users -> 1 to 499
uid of normal users -> 500 and above

Files and directories that gets created/changed upon every user creation:

/etc/passwd                           (contains profile info of the users)
/etc/shadow                           (contains password info of the users)
/home/${USER}                     (home directory of the user)
/var/spool/mail/${USER}         (email inbox of the user)

Few config files that controls the user creation:

/etc/default/useradd             (base config file)
/etc/skel                             (base structure for every user's home directory)
/etc/login.defs                    (advance config file that contains password expiry period, home directory creation etc)

Understanding /etc/passwd file content

Normally the contents of /etc/passwd file will look like below -

root:x:0:0:root:/root:/bin/bash
   1  |2|3|4|  5  |    6  |      7

1 -> username
2 -> password protection symbol
3 -> userid
4 -> groupid
5 -> description about the user
6 -> home directory of the user
7 -> shell used by the user


To create a user

useradd [username]


To add a description (comment) for already created user

usermod -c "[description]" [username]


To set password for a user

passwd [username]


To create a user with a different shell (other than the default /bin/bash)

useradd -s /sbin/nologin [username]
 
[! the user can not login if his shell is /sbin/nologin]


To assign a new home directory for an existing user and to move his current home content to new location

usermod -md [newhome] [username]


To lock the password of a user

passwd -l [username]


To delete a user along with all his data (home & mail directories)

userdel -r [username]


Groups

There are two type of groups - Primary and Secondary (or Supplementary)

Similar to the user id (uid), there is also an id for each group (gid) and the id range is same as that of uid's.

Files that get changed or created when a group is created

/etc/group                    (contain group info)
/etc/gshadow               (contain group password)


Understanding the contents of /etc/group file

Normally the contents of /etc/group file will look like below -

root:x:0:root,user1
  1   |2|3|     4

1 -> group name
2 -> password protection symbol
3 -> group id
4 -> group members (users) list


To add a group

groupadd [groupname]


To assign a password for group

gpasswd [groupname]


To list the groups in which the currently logged in user is part of

groups


To assign a new group for currently logged in user temporarily

newgrp [groupname]


To assign user to a secondary group permanently

usermod -G [groupname] [username]

[! this will remove the user from his previous group(s) and assign him to the new group]


To assign a user to another secondary group permanently without removing his previous subscriptions

usermod -aG [groupname] [username]


To change the primary group of a user permanently

usermod -g [groupname] [username]


To change the group id of an existing group

groupmod -g [new id] [groupname]


To delete a group

groupdel [groupname]


Permissions
A normal ls -l command will output like below -

drwxr-xr-x. 2 root root  4096 Jul  1 21:59 Documents

In the above output, the first 11 characters define the permissions for the corresponding file.

d  r  w  x  r   -   x  r  -    x    .
1  2  3  4  5  6  7  8  9  10  11

1 -> File type
        -   -> ASCII file
        d  -> directory
        c  -> character device
        b  -> block device
        l   -> symbolic link
        s  -> socket

2,3,4  -> Owner's permission
5,6,7  -> Group owner's permission
8,9,10 -> Other's permission
11  -> selinux context


To change the owner of a directory (or file)

chown [username] [directory/file]


To change the group owner of a directory (or file)

chgrp [groupname] [directory/file]


There are two ways in which the permissions can be set for directories/files - one is Symbolic method and the other one is Numeric method.


Symbolic method glossary

Who
u  -> owner
g  -> group owner
o  -> others
a  -> all



What
r  -> read
w -> write
x  -> execute/accessible
-  -> null

Action
+ -> append/add
-  -> revoke/remove
= -> assign


Example:

chmod o+w [directory/file]

the above command will append write permission to others for the [directory/file]


Numeric method glossary

4  -> read
2  -> write
1  -> execute
0  -> null

Example:

chmod 755 [directory/file]

the above command will set the permissions for [directory/file] as rwxrw-rw-


UMASK

UMASK defines the default permissions that has to be set for each file/directory as soon as it is created by a user.

The default permission for directories        -> 777
The default permission for files                   -> 666
Predefined UMASK value for root user       -> 022
Predefined UMASK value for normal user  -> 002

Which means, if a root user creates a directory, that directory's permission is set to 777-022 = 755. And if the root user creates a file, that file's permission is set to 666-022 = 644.

Similarly, if a normal user creates a directory, that directory's permission is set to 777-002 = 775. And if the normal user created a file, that file's permission is set to 666-002 = 664.

UMASK value for all users is defined in /etc/profile


To change the UMASK value for currently logged in user temporarily

umask [new_value]



Tidbits of the day
  • id command is used to get the id of
  • id command is used to get the id of currently logged in user
  • RedHat uses sha512 algorithm to encrypt the user password. The algorithm is configured in /etc/login.defs

--End of Day 3--

Wednesday, July 9, 2014

Road to RHCE - Day 1

The thought of getting myself RHCE certified started some five to six years back. That remained a thought for a long time before I finally made my mind to make it into a reality.

Picked up a training center at T.Nagar and got myself enrolled on May 1st 2014. It took about one and a half month for the center to form the batch for the timing that I opted for and the classes started on June 18th 2014.

My intention of this blog is just to keep a digital copy of whatever notes that I take in the class. I am planning to blog the topics that are covered everyday.

A big thanks to my friend Shrinivasan, who motivated me to start this series & eventually to return back to blogging.

This might also help someone who is looking for quick help in linux or it might also give a good idea for students about the topics covered in RHCE. Anyway with some hope that my blog would help myself/others as a quick reference, let me get this rolling...


Day 1

First day was slow, as normally it would be for any training course for that matter. Our faculty gave us ample time (really 'a lot of time') to get settled in the classroom and after the formal introduction, he threw an unexpected bomb to us.

"Since this is the first day, you may start now if you wish. Or if you want me to take class, I can do that as well."

We were like "what, are you kidding???"

We finally convinced our faculty to start with the class and so after the regular features of linux stuff, our day 1 started with the default filesystem hierarchy of linux. Here on, I am switching to a text book style to minimize my blabbering and just to focus on the subject rather.

Disclaimer: The information shared in this blog from this point on are direct from the classroom and I am blindly replicating those here. Constructive feedback is always welcome if any inappropriate or misleading information is found.


Linux Filesystem Hierarchy

Below are the default directories that get created when you install RedHat linux operating system -

/              - top level directory
/root       - root user's home directory
/home     - placeholder for normal user(s)'s home directories
/bin         - contains the commands that can be executed by normal users
/sbin       - contains the commands that can be executed by root user
/boot      - boot loader info
/etc         - system config files
/dev        - device(s) info
/usr        - documentation files
/var        - log files
/opt        - 3rd party app data
/proc      - process & system info
/sys        - process & system info
/mnt       - placeholder to mount external media
/media   - placeholder to mount external media
/lib         - library files and modules

Terminal

The default shell that will be used during the training is bash (bourne again shell)

Terminal is the command prompt window which we can invoke in 3 ways from the desktop -

1. Press Alt+F2 -> type gnome-terminal
2. Click Applications -> System Tools -> Terminal
3. Right-click on the desktop -> Open in Terminal

To zoom in when inside the terminal - press Ctrl+Shift+"+"
To zoom out when inside the terminal - press Ctrl+"-"
To open a new tab in the terminal - press Ctrl+Shit+T
To switch between open tabs in the terminal - press Alt+

Tidbits of the day

  • whoami command is used to know the user as whom we logged in currently
  • date command is used to view/set the system date/time
  • hwclock command is used to view the CMOS date/time
  
--End of Day 1--