Introduction to the Shell and Command Line on Oracle Linux
Introduction
This tutorial explores introductory tasks for administring Oracle Linux from the command line using the Bash shell.
Objectives
In this tutorial, you'll:
- Run commands using a shell and the command line.
- Work with files and directories.
- Edit files with vim.
- Learn about file permissions.
- Monitor system processes.
Prerequisites
Minimum of a single Oracle Linux system
Each system should have Oracle Linux installed and configured with:
- A non-root user account with sudo access
Deploy Oracle Linux
Note: If running in your own tenancy, read the linux-virt-labs
GitHub project README.md and complete the prerequisites before deploying the lab environment.
Open a terminal on the Luna Desktop.
Clone the
linux-virt-labs
GitHub project.git clone https://github.com/oracle-devrel/linux-virt-labs.git
Change into the working directory.
cd linux-virt-labs/ol
Install the required collections.
ansible-galaxy collection install -r requirements.yml
Deploy the lab environment.
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6"
The free lab environment requires the extra variable
local_python_interpreter
, which setsansible_python_interpreter
for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add
-e instance_shape="VM.Standard3.Flex"
or-e os_version="9"
to the deployment command.Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.
Run Commands Using a Shell and Command Line
Bash (/bin/bash
) is an application and the default shell in Oracle Linux. When opening a terminal, Oracle Linux gives you a prompt ($
). We'll use the terminal to enter shell metacharacters to simplify commands, structure, and output. One example is to view and set values to manage command-line history.
Open a terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>
Verify that the default shell,
/bin/bash
, is running, and display the contents of the SHELL variable.echo $SHELL
Ensure you're in the user’s home directory using the tilde (
~
) metacharacter with thecd
command.cd ~
Check the current number of command lines maintained by the
history
command.echo $HISTFILESIZE $HISTSIZE
Set
HISTSIZE
by setting the variable's value to 20.HISTSIZE=20
Confirm the updated size of the command-line history for an open terminal window.
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.
history
Work with Files and Directories
Display the present working directory.
pwd
Display a long list of all the contents of the current working directory.
ls -la
Display the file types in your current directory.
ls -Fa
The
-a
shows all entries, including those starting with.
. The-F
appends an indicator such as\
for a directory or*
for an executable to each entry.Create a directory called
temp
if one does not already exist.mkdir temp
Change to the
temp
directory.cd temp
Display the current working directory.
pwd
Display the files and directories under the root directory of the Oracle Linux filesystem.
ls /
Return to your home directory.
cd ~
Change the directory to the current directory's parent directory.
cd ..
Display the present working directory.
pwd
Return to your previous directory.
cd -
Create a new file called
myfile
containing the contentshello world
.echo hello world > myfile
Verify the creation of the new
myfile
file.ls
Display the contents of the
myfile
file.cat myfile
Copy the
myfile
file to another file name.cp myfile myfile2
Display a long list of the contents of the current working directory.
ls -l
Display the
myfile2
file type.file myfile2
Display the file contents of
myfile2
.cat myfile2
Copy the
myfile2
file to thetemp
directory.cp myfile2 temp/
Display the contents of the
temp
directory.ls -l temp/
Rename the
temp
directory.mv temp temp2
Display the file types in the current working directory.
ls -F
Recursively copy all files in the
temp2
directory to a new directory,temp3
.cp -R temp2 temp3
Display a list of all files recursively starting from the current working directory.
ls -R
Create a new
temp
directory.mkdir temp
Create a new empty file called
alpha
.touch alpha
Display the contents of your current directory.
ls
Delete the
alpha
file.rm alpha
Delete the
temp
directory.rmdir temp
Display the file types in your current directory.
ls -F
Starting in your home directory, search for all files named
myfile
.find ~ -name myfile
Start in your home directory and search for all files named
temp2
.find ~ -name temp2
Edit Files with the Vim Text Editor
Vim is the default editor for Oracle Linux, and users control it using only their keyboard without the need for menus or a mouse.
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 following 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 action will move the cursor down. - Press
h
or the left arrow key to move the cursor to the left. - Bring the cursor to the d character in the string id.
- Press the
r
key and insert character s. That replaces 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, which changes 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 type the
dd
command, which deletes the entire line.
- 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 it encounters in the file. 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
File permissions on Oracle Linux are essential for controlling access to files and directories and securing the data stored in them.
Ensure you are in your home directory.
cd ~
To find the owner of the existing directory.
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.ls -l temp2
Change the ownership of the
temp2
directory to the root user.- Use
sudo su
command to switch to the root user/role - Run the change owner command.
sudo su chown root temp2
- Use
Confirm the ownership change of the contents of the
temp2
directory.ls -l
Change the user and group ownership of its contents recursively to root and root.
chown -R root:root temp2 ls -lR temp2
Exit
su
using theexit
command.exit
Ensure your system has the umask value set to 0022.
umask
Set the umask value to 0022 if that is not its current value.
umask 0022
Create a new directory called
perm
in thetemp3
directory.mkdir temp3/perm
Change to the
/etc
directory and list these four files:group
,motd
,shadow
, andfstab
.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.
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 symbolic mode, add write (w) permission for the group permission set to the
motd
file.chmod g+w temp3/perm/motd ls -l temp3/perm
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 uses values with a base 8, in this case 0-7.
Using octal mode, add write (w) permission for
other
on the file namedgroup
.chmod 646 temp3/perm/group ls -l temp3/perm
Identify the GID and UID for the
motd
file.ls -n temp3/perm/motd
Create a new directory called
notes
.mkdir notes
Create a new
memo
file in yournotes
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
View the contents of the
memo
file.cat notes/memo
This command fails after removing the user's read permissions. Even though you are part of the group, the commands run in the terminal apply permissions in the order they appear.
Monitor System Processes
Knowing how to determine a process identifier (PID), view a process tree, and kill processes is vital for running an Oracle Linux system.
List the processes currently running on your system.
ps
Print a complete listing of the currently running processes.
ps -f
Display information about every process running and then show a count of the total number of processes.
ps -e ps -e | wc -l
Again, run the
ps
command and observe the TTY column where the controlling terminal ispts/0
.ps -f
Open a second terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>
Run the
ps
command in the new terminal window.ps -f
Observe the TTY column in the second terminal window, where the controlling terminal is pts/1. This behavior is because you now have two separate and concurrent terminal window sessions open simultaneously.
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 thesleep
process.ps -ef | grep sleep
You can find the PID under the second column of the output.
To terminate the sleep process, use the
kill
command with the PID argument from the second terminal window.- 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/<n>
, wheren
is a number (for example,/dev/pts/1
).tty
Return to your first terminal window.
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-node-01 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-node-01 system in the second terminal window
kill 29957
Run the following
kill -l
(list option) commands to identify the signal names and values.kill -l 9 kill -l kill kill -l 15 kill -l term
For 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; 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
Connect via SSH to the ol-node-01 system in the second terminal window.
ssh oracle@<ip_address_of_instance>
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 is gone.ps