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.gitChange into the working directory.
cd linux-virt-labs/olInstall the required collections.
ansible-galaxy collection install -r requirements.ymlDeploy 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 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 $SHELLEnsure 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 $HISTSIZESet
HISTSIZEby setting the variable's value to 20.HISTSIZE=20Confirm the updated size of the command-line history for an open terminal window.
echo $HISTSIZEView the page-wise output of the
historycommand.history | lessPress the
qkey to quit the previous command.View the preceding 10 commands from the history database.
history 10Use the
–coption to clear previous history.history -cView the cleared history.
history
Work with Files and Directories
Display the present working directory.
pwdDisplay a long list of all the contents of the current working directory.
ls -laDisplay 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 tempChange to the
tempdirectory.cd tempDisplay the current working directory.
pwdDisplay 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.
pwdReturn to your previous directory.
cd -Create a new file called
myfilecontaining the contentshello world.echo hello world > myfileVerify the creation of the new
myfilefile.lsDisplay the contents of the
myfilefile.cat myfileCopy the
myfilefile to another file name.cp myfile myfile2Display a long list of the contents of the current working directory.
ls -lDisplay the
myfile2file type.file myfile2Display the file contents of
myfile2.cat myfile2Copy the
myfile2file to thetempdirectory.cp myfile2 temp/Display the contents of the
tempdirectory.ls -l temp/Rename the
tempdirectory.mv temp temp2Display the file types in the current working directory.
ls -FRecursively copy all files in the
temp2directory to a new directory,temp3.cp -R temp2 temp3Display a list of all files recursively starting from the current working directory.
ls -RCreate a new
tempdirectory.mkdir tempCreate a new empty file called
alpha.touch alphaDisplay the contents of your current directory.
lsDelete the
alphafile.rm alphaDelete the
tempdirectory.rmdir tempDisplay the file types in your current directory.
ls -FStarting in your home directory, search for all files named
myfile.find ~ -name myfileStart 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.vimPress 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 temp2Change 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 -lChange the user and group ownership of its contents recursively to root and root.
chown -R root:root temp2 ls -lR temp2Exit
suusing theexitcommand.exitEnsure your system has the umask value set to 0022.
umaskSet the umask value to 0022 if that is not its current value.
umask 0022Create a new directory called
permin thetemp3directory.mkdir temp3/permChange 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 deniedGo to your
temp3directory and verify the contents of its~/temp3/permdirectory.cd ~/temp3 ls -l permChange directories to your home directory.
cd ~Create a new directory called
testand a new file calledtest1.mkdir test touch test/test1Examine the default permissions of the new file
test1.ls –l test/test1Check the default permissions of the new directory
test.ls –ld testUsing 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/permIdentify the GID and UID for the
motdfile.ls -n temp3/perm/motdCreate a new directory called
notes.mkdir notesCreate a new
memofile in yournotesdirectory.touch notes/memo ls -l notes/memoRemove 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/memoView 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.
psPrint a complete listing of the currently running processes.
ps -fDisplay information about every process running and then show a count of the total number of processes.
ps -e ps -e | wc -lAgain, run the
pscommand and observe the TTY column where the controlling terminal ispts/0.ps -fOpen 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 100In 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).ttyReturn to your first terminal window.
Find the PID associated with the second terminal window.
pgrep -t pts/1In 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 29957Run 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.psConnect 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 1252In the second terminal window, terminate the first terminal window using the
killcommand with the bash shell PID.kill -9 1252In the first terminal window, enter the
pscommand and notice that thesleep 600process is gone.ps