Note:
- This tutorial is available in an Oracle-provided free lab environment.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
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-labsGitHub 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_interpreterfor 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 Oracle Linux 8. To use Oracle Linux 9, add
-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 Cloud Native Environment 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 thecdcommand.cd ~ -
Check the current number of command lines maintained by the
historycommand.echo $HISTFILESIZE $HISTSIZE -
Set
HISTSIZEby 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
historycommand.history | less -
Press the
qkey to quit the previous command. -
View the preceding 10 commands from the history database.
history 10 -
Use the
–coption 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 -FaThe
-ashows all entries, including those starting with.. The-Fappends an indicator such as\for a directory or*for an executable to each entry. -
Create a directory called
tempif one does not already exist.mkdir temp -
Change to the
tempdirectory.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
myfilecontaining the contentshello world.echo hello world > myfile -
Verify the creation of the new
myfilefile.ls -
Display the contents of the
myfilefile.cat myfile -
Copy the
myfilefile to another file name.cp myfile myfile2 -
Display a long list of the contents of the current working directory.
ls -l -
Display the
myfile2file type.file myfile2 -
Display the file contents of
myfile2.cat myfile2 -
Copy the
myfile2file to thetempdirectory.cp myfile2 temp/ -
Display the contents of the
tempdirectory.ls -l temp/ -
Rename the
tempdirectory.mv temp temp2 -
Display the file types in the current working directory.
ls -F -
Recursively copy all files in the
temp2directory 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
tempdirectory.mkdir temp -
Create a new empty file called
alpha.touch alpha -
Display the contents of your current directory.
ls -
Delete the
alphafile.rm alpha -
Delete the
tempdirectory.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
vimcommand from your home directory.vim -
Press the
ikey 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
Escto enter normal mode. - Use the
h,j,k,l, or arrow keys to navigate to the last character of the line. - Press the
akey 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
Escto return to normal mode. - Move the cursor to the third line by pressing the
jor down arrow key. This action will move the cursor down. - Press
hor the left arrow key to move the cursor to the left. - Bring the cursor to the d character in the string id.
- Press the
rkey and insert character s. That replaces the character d with the character s.
- Press
-
Change the word
WahttoWhat.- Press
Escand move the cursor to the third line. - Place your cursor on the character a of the word Waht and execute the
cwcommand. - Enter the text hat, which changes the whole word Waht to what.
- Press
ESCwhen finished modifying the word.
- Press
-
Copy and paste the line
Hello World.- Press
ESCto return to command mode. - Move the cursor to the beginning of the Hello World line.
- Execute the
yycommand to copy the string. - Execute the
pcommand to paste the string. The whole line is copied and pasted.
- Press
-
Delete the additional
Hello Worldline.- Press
Escto enter command mode. - Move the cursor to the beginning of the second line Hello World and type the
ddcommand, which deletes the entire line.
- Press
-
To search for the string
What.- Press
Escto 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
nkey.- The cursor will move to the second string in the file.
-
Customize the session by displaying the line numbers.
- Press
Escto enter command mode. - Enter the
:set nucommand and pressEnter. Notice that:set nuappears at the bottom of the terminal window screen.
- Press
-
Remove the line numbers.
- Press
Escto enter command mode. - Type the
:set nonucommand and pressEnter. The line numbers disappear.
- Press
-
Quit and save the file with the changes.
- Press
Escto enter command mode. - Type
:w intro.txtand pressEnterto save the file. - Type
:qto 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 -ldThe owner of the existing directory is displayed in the third column of the output.
-
Identify the owner of the contents in the
temp2directory.ls -l temp2 -
Change the ownership of the
temp2directory to the root user.- Use
sudo sucommand 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
temp2directory.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
suusing theexitcommand.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
permin thetemp3directory.mkdir temp3/perm -
Change to the
/etcdirectory and list these four files:group,motd,shadow, andfstab.cd /etc ls -l group motd shadow fstabNote: In Oracle Linux, there are no permissions on the shadow file.
-
Copy the four files to your
~/temp3/permdirectory. Theshadowfile will fail to copy.cp group motd shadow fstab ~/temp3/permExample Output:
cp: cannot open ’shadow’ for reading: Permission denied -
Go to your
temp3directory and verify the contents of its~/temp3/permdirectory.cd ~/temp3 ls -l perm -
Change directories to your home directory.
cd ~ -
Create a new directory called
testand 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
motdfile.chmod g+w temp3/perm/motd ls -l temp3/permSymbolic 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
motdfile to-rwxrw----.chmod 760 temp3/perm/motd ls -l temp3/permOctal mode uses values with a base 8, in this case 0-7.
-
Using octal mode, add write (w) permission for
otheron the file namedgroup.chmod 646 temp3/perm/group ls -l temp3/perm -
Identify the GID and UID for the
motdfile.ls -n temp3/perm/motd -
Create a new directory called
notes.mkdir notes -
Create a new
memofile in yournotesdirectory.touch notes/memo ls -l notes/memo -
Remove the read (r) permission for the owner from the
memofile in thenotesdirectory. You can use symbolic mode to do this.chmod u-r notes/memo ls -l notes/memo -
View the contents of the
memofile.cat notes/memoThis 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
pscommand 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
pscommand in the new terminal window.ps -fObserve 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 100command.sleep 100 -
In the second terminal window, use the
psandgrepcommands to identify the PID of thesleepprocess.ps -ef | grep sleepYou can find the PID under the second column of the output.
-
To terminate the sleep process, use the
killcommand 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 29987Notice the
sleep 100is terminated in the first terminal window. -
In the second terminal window, enter the
ttycommand to identify the name of this terminal window.The name appears as
/dev/pts/<n>, wherenis 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
killcommand 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 termFor 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
sleepcommand and place it in the background.sleep 600 & -
Use the
pscommand 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
sleepprocess 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
killcommand with the bash shell PID.kill -9 1252 -
In the first terminal window, enter the
pscommand and notice that thesleep 600process is gone.ps
For More Information
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Introduction to the Shell and Command Line on Oracle Linux
F50766-05
July 2024