Introduction to Oracle Linux: Shell and Command Line
Introduction
This lab explores introductory tasks for using Oracle Linux.
Objectives
In this lab, you'll:
- Execute commands using a shell and the command line.
- Work with files and directories.
- Edit files with vim.
- Learn about file permissions.
- Monitor system processes.
What Do You Need?
- A system with Oracle Linux installed
Set Up Lab Environment
Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.
Information: The free lab deploys a running Oracle Linux system.
If not already connected, open a terminal and connect via ssh to the ol-server system.
ssh oracle@<ip_address_of_ol-server>
Execute Commands Using a Shell and Command Line
Overview
In this section, you will use shell metacharacters to simplify commands, structure, and output. Bash (/bin/bash
) is the default shell. You will view and set values to manage command-line history.
Verify that the default shell,
/bin/bash
, is running, and use theecho
command on Oracle Linux to display the contents of the SHELL variable.echo $SHELL
Make certain you're in the user’s home directory using the tilde (
~
) metacharacter with thecd
command.cd ~
Ensure the current number of command lines maintained by the
history
command.echo $HISTFILESIZE $HISTSIZE
Set
HISTSIZE
to 20 using the following command.HISTSIZE=20
Confirm that the command-line history for an open terminal window size is set to 20.
echo $HISTSIZE
View the page-wise output of the
history
command.history | less
Press the
q
key to quit the previous command.View the preceding 10 commands from the history database.
history 10
Use the
–c
option to clear previous history.history -c
View the cleared history using the
history
command again.history
Work with Files and Directories
Overview
In this section, you will use file and directory access commands.
Display the present working directory using the
pwd
command.pwd
Display a long list of all the contents of the current working directory running
ls
command.ls -la
Display the file types in your current directory using the
ls
command.ls -Fa
If a
temp
directory doesn't already exist, create a directory calledtemp
using themkdir
command.mkdir temp
Change to the
temp
directory using thecd
command.cd temp
Display the present working directory using the
pwd
command.pwd
Display the files and directories under the root directory using the
ls
command.ls /
Return to your home directory using the
cd
command.cd ~
Change the directory to the parent directory using the
cd
command.cd ..
Display the present working directory using the
pwd
command.pwd
Quickly return to your previous directory using the
cd
command.cd -
Create a new file called
myfile
using theecho
and redirect commands.echo hello world > myfile
Verify the new
myfile
file was created using thels
command.ls
Display the contents of the
myfile
file using thecat
command.cat myfile
Copy the
myfile
file to another file name using thecp
command.cp myfile myfile2
Display a long list of the contents of the current working directory using the
ls
command.ls -l
Display the
myfile2
file type using thefile
command.file myfile2
Display the file contents of
myfile2
using thecat
command.cat myfile2
Copy the
myfile2
file to thetemp
directory using thecp
command.cp myfile2 temp/
Display the contents of the
temp
directory using thels
command.ls -l temp/
Rename the
temp
directory using themv
command.mv temp temp2
Display the file types in your current directory using the
ls
command.ls -F
Copy all files in the
temp2
directory to a new directory,temp3
, usingcp
command.cp -R temp2 temp3
Display all files recursively using the
ls
command.ls -R
Create a new
temp
directory using themkdir
command.mkdir temp
Create a new file called
alpha
using thetouch
command.touch alpha
Display the contents of your current directory using the
ls
command.ls
Delete the
alpha
file using therm
command.rm alpha
Delete the
temp
directory using thermdir
command.rmdir temp
Display the file types in your current directory using the
ls
command.ls -F
Starting in your home directory, find all files named
myfile
using thefind
command.find ~ -name myfile
Starting in your home directory, find all files named
temp2
using thefind
command.find ~ -name temp2
Edit Files with the Vim Text Editor
Overview
In this section, you will use the vim editor which is the default editor for Oracle Linux 8.
Type the
vim
command from your home directory.vim
Press the
i
key to change into insert text mode and type the following text.Hello World What is your Waht id today's date?`
Append text to the line
What is your
.- Press
Esc
to enter normal mode. - Use the
h
,j
,k
,l
or arrow keys to navigate to the last character of the line. - Press the
a
key to append and insert a space with the next string name?.
- Press
Replace the d character with s in the line,
Waht id today’s date?
.- Press
Esc
to return to normal mode. - Move the cursor to the third line by pressing the
j
or down arrow key. This will move the cursor down. - To move the cursor to the left, press
h
or the left arrow key. - Bring the cursor to the d character in the string id.
- Press the
r
key and then insert character s. This will replace the character d with the character s.
- Press
Change the word
Waht
toWhat
.- Press
Esc
and move the cursor to the third line. - Place your cursor on the character a of the word Waht and execute the
cw
command. - Enter the text hat. This will change the whole word Waht to what.
- Press
ESC
when finished modifying the word.
- Press
Copy and paste the line
Hello World
.- Press
ESC
to return to command mode. - Move the cursor to the beginning of the Hello World line.
- Execute the
yy
command to copy the string. - Execute the
p
command to paste the string. The whole line is copied and pasted.
- Press
Delete the additional
Hello World
line.- Press
Esc
to enter command mode. - Move the cursor to the beginning of the second line Hello World and execute the
dd
command. The entire line is deleted.
- Press
To search for the string
What
.- Press
Esc
to enter command mode. - Press the forward slash
/
key. - Enter the text What and press
Enter
. The cursor automatically moves to the first string in the file that it encounters. Notice that /What appears at the bottom of the terminal window screen.
- Press
Search for the next occurrence of the same string by pressing the
n
key.- The cursor will move to the second string in the file.
Customize the session by displaying the line numbers.
- Press
Esc
to enter command mode. - Enter the
:set nu
command and pressEnter
. Notice that:set nu
appears at the bottom of the terminal window screen.
- Press
Remove the line numbers.
- Press
Esc
to enter command mode. - Type the
:set nonu
command and pressEnter
. The line numbers disappear.
- Press
Quit and save the file with the changes.
- Press
Esc
to enter command mode. - Type
:w intro.txt
and pressEnter
to save the file. - Type
:q
to quit. The command prompt returns.
- Press
Learn About File Permissions
Overview
In this section, you will view and change file ownership. You will also view and change permissions on files.
Ensure you are in your home directory using the
pwd
command.pwd
To find the owner of the existing directory, use the
ls
command.ls -ld
The owner of the existing directory is displayed in the third column of the output.
Identify the owner of the contents in the
temp2
directory using thels
command.ls -l temp2
Change the ownership of the
temp2
directory to the root user.- Use
sudo su
command to switch to root user/role - Run the change owner
chown
command.
sudo su chown root temp2
- Use
Confirm the ownership of the contents of the
temp2
directory.ls -l
Change the user and group ownership of its contents to root and root, by running the
chown
command again with the recursive-R
option.chown -R root:root temp2 ls -lR temp2
Exit
su
using theexit
command.exit
Ensure that the umask value is set to 0022 on your system. To verify, run the
umask
command.umask
If the umask is not set to 0022, then set the umask value to 0022 running the
umask
command.umask 0022
Create a new directory called
perm
in thetemp3
directory using themkdir
command.mkdir temp3/perm
Change to the
etc
directory and list these four files –group
,motd
,shadow
,fstab
for Oracle Linux using thels
command.cd /etc ls -l group motd shadow fstab
Note: In Oracle Linux, there are no permissions on the shadow file.
Copy the four files to your
~/temp3/perm
directory. Theshadow
file will fail to copy.cp group motd shadow fstab ~/temp3/perm
Example Output:
cp: cannot open ’shadow’ for reading: Permission denied
Go to your
temp3
directory and verify the contents of its~/temp3/perm
directory.cd ~/temp3 ls -l perm
Change directories to your home directory using the
cd
command.cd
Create a new directory called
test
and a new file calledtest1
.mkdir test touch test/test1
Examine the default permissions of the new file
test1
.ls –l test/test1
Check the default permissions of the new directory
test
.ls –ld test
Using the
chmod
command and symbolic mode, add write (w) permission for the group permission set to themotd
file.chmod g+w temp3/perm/motd ls -l temp3/perm
Note: Symbolic mode uses a combination of letters and symbols to add or remove permissions for each type of user.
Using octal mode, change the permissions on the
motd
file to-rwxrw----
.chmod 760 temp3/perm/motd ls -l temp3/perm
Octal mode is the use of values with a base 8, in this case 0-7.
Using octal mode, add write (w) permission for other on the file named
group
.chmod 646 temp3/perm/group ls -l temp3/perm
Identify the GID and UID for the
motd
file using thels
command.ls -n temp3/perm/motd
Create a new directory called
notes
using themkdir
command.mkdir notes
Create a new file called
memo
in yourdir4
directory.touch notes/memo ls -l notes/memo
Remove the read (r) permission for the owner from the
memo
file in thenotes
directory. You can use symbolic mode to do this.chmod u-r notes/memo ls -l notes/memo
Use the
cat
command to view thememo
file.cat notes/memo
Note: This fails because read permission has been removed from the user. Even though you are part of the group, the permissions are viewed in the order in which they appear.
Monitor System Processes
Overview
In this section, you will determinea the process identifier (PID), view a processes tree, and kill processes.
Use the following
ps
command to list the processes currently running on your system.ps
Use the
–f
option to print a full listing for theps
command.ps -f
Use the
–e
option to print information about every process running. Then use theps
andwc
commands to show the total number of processes.ps -e ps -e | wc -l
Once again, use the
ps
command.ps -f
Note: Observe the TTY column where the controlling terminal is pts/0.
Open a second terminal window and connect via ssh to the ol-server system.
ssh oracle@<ip_address_of_ol-server>
Execute the
ps
command in the new terminal window.ps -f
Note: Observe the TTY column in the second terminal window, where the controlling terminal is pts/1. This is because you now have two separate and concurrent terminal window sessions open at the same time.
In your first terminal window, enter the
sleep 100
command:sleep 100
In the second terminal window, use the
ps
andgrep
commands to identify the PID of the sleep process.ps -ef | grep sleep
The PID can be found under the second column of the output.
From the second terminal window, use the
kill
command with the PID argument to terminate the sleep process.- This example uses a PID of 29987.
- Your PID may differ from the command presented.
kill 29987
Notice the
sleep 100
is terminated in the first terminal window.In the second terminal window, enter the
tty
command to identify the name of this terminal window. The name appears as /dev/pts/, where n is a number (for example, /dev/pts/1). tty
Return to your first terminal window. Use the
pgrep -t
(terminal option) command to find the PID associated with the second terminal window.pgrep -t pts/1
In your first terminal window, use the
kill
command to terminate the ssh session and log off the ol-server system in your second terminal window.- This example uses a PID of 29957.
- Your PID may differ from the command presented.
- Notice you are logged off the ol-server system in the second terminal window
kill 29957
Run the following
kill -l
(list option) commands to identify the signal names and signal values.kill -l 9 kill -l kill kill -l 15 kill -l term
Note: signal value 9, the signal name is KILL, and for the signal name kill, the signal value is 9. For signal value 15, the signal name is TERM, and for the signal name term, the signal value is 15.
In the terminal window, enter the
sleep
command and place it in the background.sleep 600 &
Use the
ps
command to identify the bash shell process running in that window.ps
In the second terminal connect via ssh to the ol-server system.
ssh oracle@<ip_address_of_ol-server>
In the second terminal window, display the process tree and provide the PID of the
sleep
process running in the first terminal window as an argument, using thepstree -p <PID>
command.- This example uses a PID of 1252.
- Your PID may differ from the command presented.
pstree -p 1252
In the second terminal window, terminate the first terminal window using the
kill
command with the bash shell PID.kill -9 1252
In the first terminal window, enter the
ps
command and notice that thesleep 600
process was killed.ps
For More Information
Oracle Linux 8 Documentation Oracle Linux 9 Documentation Oracle Linux Training Oracle Linux Training Station