Introduction to Oracle Linux: Shell and Command Line

14
0
Send lab feedback

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.

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

  1. Verify that the default shell, /bin/bash, is running, and use the echo command on Oracle Linux to display the contents of the SHELL variable.

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

    cd ~
  3. Ensure the current number of command lines maintained by the history command.

    echo $HISTFILESIZE $HISTSIZE
  4. Set HISTSIZE to 20 using the following command.

    HISTSIZE=20
  5. Confirm that the command-line history for an open terminal window size is set to 20.

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

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

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

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

    history -c
  10. 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.

  1. Display the present working directory using the pwd command.

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

    ls -la
  3. Display the file types in your current directory using the ls command.

    ls -Fa
  4. If a temp directory doesn't already exist, create a directory called temp using the mkdir command.

    mkdir temp
  5. Change to the temp directory using the cd command.

    cd temp
  6. Display the present working directory using the pwd command.

    pwd
  7. Display the files and directories under the root directory using the ls command.

    ls /
  8. Return to your home directory using the cd command.

    cd ~
  9. Change the directory to the parent directory using the cd command.

    cd ..
  10. Display the present working directory using the pwd command.

    pwd
  11. Quickly return to your previous directory using the cd command.

    cd -
  12. Create a new file called myfile using the echo and redirect commands.

    echo hello world > myfile
  13. Verify the new myfile file was created using the ls command.

    ls
  14. Display the contents of the myfile file using the cat command.

    cat myfile
  15. Copy the myfile file to another file name using the cp command.

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

    ls -l
  17. Display the myfile2 file type using the file command.

    file myfile2
  18. Display the file contents of myfile2 using the cat command.

    cat myfile2
  19. Copy the myfile2 file to the temp directory using the cp command.

    cp myfile2 temp/
  20. Display the contents of the temp directory using the ls command.

    ls -l temp/
  21. Rename the temp directory using the mv command.

    mv temp temp2
  22. Display the file types in your current directory using the ls command.

    ls -F
  23. Copy all files in the temp2 directory to a new directory, temp3, using cp command.

    cp -R temp2 temp3
  24. Display all files recursively using the ls command.

    ls -R
  25. Create a new temp directory using the mkdir command.

    mkdir temp
  26. Create a new file called alpha using the touch command.

    touch alpha
  27. Display the contents of your current directory using the ls command.

    ls
  28. Delete the alpha file using the rm command.

    rm alpha
  29. Delete the temp directory using the rmdir command.

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

    ls -F
  31. Starting in your home directory, find all files named myfile using the find command.

    find ~ -name myfile
  32. Starting in your home directory, find all files named temp2 using the find 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.

  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 next 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 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.
  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. This will change 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 execute the dd command. The entire line is deleted.
  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 in the file that it encounters. 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

Overview

In this section, you will view and change file ownership. You will also view and change permissions on files.

  1. Ensure you are in your home directory using the pwd command.

    pwd
  2. 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.

  3. Identify the owner of the contents in the temp2 directory using the ls command.

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

    ls -l
  6. 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  
  7. Exit su using the exit command.

    exit
  8. Ensure that the umask value is set to 0022 on your system. To verify, run the umask command.

    umask
  9. If the umask is not set to 0022, then set the umask value to 0022 running the umask command.

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

    mkdir temp3/perm
  11. Change to the etc directory and list these four files – group, motd, shadow, fstab for Oracle Linux using the ls command.

    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 using the cd command.

    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 the chmod command and 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

    Note: 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 is the use of 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 using the ls command.

    ls -n temp3/perm/motd
  22. Create a new directory called notes using the mkdir command.

    mkdir notes
  23. Create a new file called memo in your dir4 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. Use the cat command to view the memo 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.

  1. Use the following ps command to list the processes currently running on your system.

    ps
  2. Use the –f option to print a full listing for the ps command.

    ps -f
  3. Use the –e option to print information about every process running. Then use the ps and wc commands to show the total number of processes.

    ps -e  
    ps -e | wc -l
  4. Once again, use the ps command.

    ps -f

    Note: Observe the TTY column where the controlling terminal is pts/0.

  5. Open a second terminal window and connect via ssh to the ol-server system.

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

  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

    The PID can be found under the second column of the output.

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

  10. 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
  11. 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
  12. 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
  13. 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.

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

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

    ps
  16. In the second terminal connect via ssh to the ol-server system.

    ssh oracle@<ip_address_of_ol-server>
  17. 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
  18. In the second terminal window, terminate the first terminal window using the kill command with the bash shell PID.

    kill -9 1252
  19. In the first terminal window, enter the ps command and notice that the sleep 600 process was killed.

    ps

For More Information

Oracle Linux 8 Documentation Oracle Linux 9 Documentation Oracle Linux Training Oracle Linux Training Station

SSR