Introduction to the Shell and Command Line on Oracle Linux

15
0
Send lab feedback

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.

  1. Open a terminal on the Luna Desktop.

  2. Clone the linux-virt-labs GitHub project.

    git clone https://github.com/oracle-devrel/linux-virt-labs.git
  3. Change into the working directory.

    cd linux-virt-labs/ol
  4. Install the required collections.

    ansible-galaxy collection install -r requirements.yml
  5. 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 sets ansible_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.

  1. Open a terminal and connect via SSH to the ol-node-01 instance.

    ssh oracle@<ip_address_of_instance>
  2. Verify that the default shell, /bin/bash, is running, and display the contents of the SHELL variable.

    echo $SHELL
  3. Ensure you're in the user’s home directory using the tilde (~) metacharacter with the cd command.

    cd ~
  4. Check the current number of command lines maintained by the history command.

    echo $HISTFILESIZE $HISTSIZE
  5. Set HISTSIZE by setting the variable's value to 20.

    HISTSIZE=20
  6. Confirm the updated size of the command-line history for an open terminal window.

    echo $HISTSIZE
  7. View the page-wise output of the history command.

    history | less
  8. Press the q key to quit the previous command.

  9. View the preceding 10 commands from the history database.

    history 10
  10. Use the –c option to clear previous history.

    history -c
  11. View the cleared history.

    history

Work with Files and Directories

  1. Display the present working directory.

    pwd
  2. Display a long list of all the contents of the current working directory.

    ls -la
  3. 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.

  4. Create a directory called temp if one does not already exist.

    mkdir temp
  5. Change to the temp directory.

    cd temp
  6. Display the current working directory.

    pwd
  7. Display the files and directories under the root directory of the Oracle Linux filesystem.

    ls /
  8. Return to your home directory.

    cd ~
  9. Change the directory to the current directory's parent directory.

    cd ..
  10. Display the present working directory.

    pwd
  11. Return to your previous directory.

    cd -
  12. Create a new file called myfile containing the contents hello world.

    echo hello world > myfile
  13. Verify the creation of the new myfile file.

    ls
  14. Display the contents of the myfile file.

    cat myfile
  15. Copy the myfile file to another file name.

    cp myfile myfile2
  16. Display a long list of the contents of the current working directory.

    ls -l
  17. Display the myfile2 file type.

    file myfile2
  18. Display the file contents of myfile2.

    cat myfile2
  19. Copy the myfile2 file to the temp directory.

    cp myfile2 temp/
  20. Display the contents of the temp directory.

    ls -l temp/
  21. Rename the temp directory.

    mv temp temp2
  22. Display the file types in the current working directory.

    ls -F
  23. Recursively copy all files in the temp2 directory to a new directory, temp3.

    cp -R temp2 temp3
  24. Display a list of all files recursively starting from the current working directory.

    ls -R
  25. Create a new temp directory.

    mkdir temp
  26. Create a new empty file called alpha.

    touch alpha
  27. Display the contents of your current directory.

    ls
  28. Delete the alpha file.

    rm alpha
  29. Delete the temp directory.

    rmdir temp
  30. Display the file types in your current directory.

    ls -F
  31. Starting in your home directory, search for all files named myfile.

    find ~ -name myfile
  32. 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.

  1. Type the vim command from your home directory.

    vim
  2. 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?`
  3. 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?.
  4. 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.
  5. Change the word Waht to What.

    • 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. Customize the session by displaying the line numbers.

    • Press Esc to enter command mode.
    • Enter the :set nu command and press Enter. Notice that :set nu appears at the bottom of the terminal window screen.
  11. Remove the line numbers.

    • Press Esc to enter command mode.
    • Type the :set nonu command and press Enter. The line numbers disappear.
  12. Quit and save the file with the changes.

    • Press Esc to enter command mode.
    • Type :w intro.txt and press Enter to save the file.
    • Type :q to quit. The command prompt returns.

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.

  1. Ensure you are in your home directory.

    cd ~
  2. 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.

  3. Identify the owner of the contents in the temp2 directory.

    ls -l temp2
  4. 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
  5. Confirm the ownership change of the contents of the temp2 directory.

    ls -l
  6. Change the user and group ownership of its contents recursively to root and root.

    chown -R root:root temp2  
    ls -lR temp2  
  7. Exit su using the exit command.

    exit
  8. Ensure your system has the umask value set to 0022.

    umask
  9. Set the umask value to 0022 if that is not its current value.

    umask 0022
  10. Create a new directory called perm in the temp3 directory.

    mkdir temp3/perm
  11. Change to the /etc directory and list these four files: group, motd, shadow, and fstab.

    cd /etc  
    ls -l group motd shadow fstab

    Note: In Oracle Linux, there are no permissions on the shadow file.

  12. Copy the four files to your ~/temp3/perm directory. The shadow file will fail to copy.

    cp group motd shadow fstab ~/temp3/perm

    Example Output:

    cp: cannot open ’shadow’ for reading: Permission denied
  13. Go to your temp3 directory and verify the contents of its ~/temp3/perm directory.

    cd ~/temp3
    ls -l perm
  14. Change directories to your home directory.

    cd ~
  15. Create a new directory called test and a new file called test1.

    mkdir test
    touch test/test1
  16. Examine the default permissions of the new file test1.

    ls –l test/test1
  17. Check the default permissions of the new directory test.

    ls –ld test
  18. 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.

  19. 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.

  20. Using octal mode, add write (w) permission for other on the file named group.

    chmod 646 temp3/perm/group
    ls -l temp3/perm
  21. Identify the GID and UID for the motd file.

    ls -n temp3/perm/motd
  22. Create a new directory called notes.

    mkdir notes
  23. Create a new memo file in your notes directory.

    touch notes/memo
    ls -l notes/memo
  24. Remove the read (r) permission for the owner from the memo file in the notes directory. You can use symbolic mode to do this.

    chmod u-r notes/memo
    ls -l notes/memo
  25. 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.

  1. List the processes currently running on your system.

    ps
  2. Print a complete listing of the currently running processes.

    ps -f
  3. Display information about every process running and then show a count of the total number of processes.

    ps -e  
    ps -e | wc -l
  4. Again, run the ps command and observe the TTY column where the controlling terminal is pts/0.

    ps -f
  5. Open a second terminal and connect via SSH to the ol-node-01 instance.

    ssh oracle@<ip_address_of_instance>
  6. 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.

  7. In your first terminal window, enter the sleep 100 command.

    sleep 100
  8. In the second terminal window, use the ps and grep commands to identify the PID of the sleep process.

    ps -ef | grep sleep

    You can find the PID under the second column of the output.

  9. 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.

  10. In the second terminal window, enter the tty command to identify the name of this terminal window.

    The name appears as /dev/pts/<n>, where n is a number (for example, /dev/pts/1).

    tty
  11. Return to your first terminal window.

  12. Find the PID associated with the second terminal window.

    pgrep -t pts/1
  13. 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
  14. 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.

  15. In the terminal window, enter the sleep command and place it in the background.

    sleep 600 &
  16. Use the ps command to identify the bash shell process running in that window.

    ps
  17. Connect via SSH to the ol-node-01 system in the second terminal window.

    ssh oracle@<ip_address_of_instance>
  18. 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 the pstree -p <PID> command.

    • This example uses a PID of 1252.
    • Your PID may differ from the command presented.
    pstree -p 1252
  19. In the second terminal window, terminate the first terminal window using the kill command with the bash shell PID.

    kill -9 1252
  20. In the first terminal window, enter the ps command and notice that the sleep 600 process is gone.

    ps
SSR