Lab 3.1 - Working With Text Files in The CLI
Lab 3.1 - Working With Text Files in The CLI
Objectives
In this lab, you will become familiar with Linux command line text editors and configuration files.
Part 1: Graphical Text Editors
Part 2: Command Line Text Editors
Part 3: Working with Configuration Files
Required Resources
CyberOps Workstation virtual machine
Instructions
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
d. Click File > Save to save the file. Notice that SciTE attempts to save the file to the current user’s home
directory, which is analyst, by default. Name the file space.txt and click Save.
e. Close SciTE by clicking the X icon on the upper right side of the window and then reopen SciTE.
f. Click File > Open… and search for the newly saved file, space.txt.
Question:
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
d. Notice that while SciTE is open on the foreground, the terminal window used to launch it is still open in
the background. In addition, notice that the terminal window used to launch SciTE no longer displays the
prompt.
Question:
e. Close this instance of SciTE by either clicking the X icon as before, or by switching the focus back to the
terminal window that launched SciTE and stopping the process. You can stop the process by pressing
CTRL+C.
Note: Starting SciTE from the command line is helpful when you want to run SciTE as root. Simply
precede scite with the sudo command, sudo scite.
f. Close SciTE and move on to the next section.
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
its original code was first created in 1976. An updated version of vi is named vim which stands for vi-
improved. Today most vi users are actually using the updated version, vim.
Due to the lack of graphical support, nano (or GNU nano) can be controlled solely through the keyboard.
CTRL+O saves the current file; CTRL+W opens the search menu. GNU nano uses a two-line shortcut bar at
the bottom of the screen, where a number of commands for the current context are listed. After nano is open,
press CTRL+G for the help screen and a complete list.
a. In the terminal window, type nano space.txt to open the text file created in Part 1.
[analyst@secOps ~]$ nano space.txt
b. nano will launch and automatically load the space.txt text file. While the text may seem to be truncated
or incomplete, it is not. Because the text was created with no return characters and line wrapping is not
enabled, by default, nano is displaying one long line of text.
Use the Home and End keyboard keys to quickly navigate to the beginning and to the end of a line,
respectively.
What character does nano use to represent that a line continues beyond the boundaries of the screen?
The greater than sign (>) or the dollar sign ($), depending on the nano version.
c. As shown on the bottom shortcut lines, CTRL+X can be used to exit nano. nano will ask if you want to
save the file before exiting (‘Y’ for Yes, or N for ‘No’). If ‘Y’ is chosen, you will be prompted to press enter
to accept the given file name, or change the file name, or provide a file name if it is a new unnamed
document.
d. To control nano, you can use CTRL, ALT, ESCAPE or the META keys. The META key is the key on the
keyboard with a Windows or Mac logo, depending on your keyboard configuration.
Navigation in nano is very user friendly. Use the arrows to move around the files. Page Up and Page
Down can also be used to skip forward or backwards entire pages. Spend some time with nano and its
help screen. To enter the help screen, press CTRL+G. Press q to quit the help screen and return to
document editing in nano.
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
While a few files are displayed, none of them seem to be configuration files. This is because it is
convention to hide home-directory-hosted configuration files by preceding their names with a “.” (dot)
character.
b. Use the ls command again but this time add the –a option to also include hidden files in the output:
[analyst@secOps ~]$ ls –la
total 144
drwx------ 14 analyst analyst 4096 Apr 17 16:34 .
drwxr-xr-x 3 root root 4096 Mar 20 2018 ..
-rw------- 1 analyst analyst 424 Apr 17 12:52 .bash_history
-rw-r--r-- 1 analyst analyst 21 Feb 7 2018 .bash_logout
-rw-r--r-- 1 analyst analyst 57 Feb 7 2018 .bash_profile
-rw-r--r-- 1 analyst analyst 97 Mar 20 2018 .bashrc
-rw-r--r-- 1 analyst analyst 141 Feb 7 2018 .bashrc_stock
drwxr-xr-x 8 analyst analyst 4096 Mar 25 12:18 .cache
drwxr-xr-x 10 analyst analyst 4096 Jul 19 2018 .config
drwxr-xr-x 2 analyst analyst 4096 Mar 22 2018 Desktop
-rw-r--r-- 1 analyst analyst 23 Mar 23 2018 .dmrc
drwxr-xr-x 3 analyst analyst 4096 Apr 2 14:44 Downloads
drwx------ 3 analyst analyst 4096 Mar 22 2018 .gnupg
-rw------- 1 analyst analyst 2520 Mar 24 12:32 .ICEauthority
drwxr-xr-x 2 analyst analyst 4096 Mar 24 2018 .idlerc
drwxr-xr-x 9 analyst analyst 4096 Jul 19 2018 lab.support.files
-rw------- 1 analyst analyst 61 Mar 24 12:36 .lesshst
drwxr-xr-x 3 analyst analyst 4096 Mar 22 2018 .local
drwx------ 5 analyst analyst 4096 Mar 24 2018 .mozilla
drwxr-xr-x 2 analyst analyst 4096 Mar 21 2018 second_drive
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
c. Use cat command to display the contents of the .bashrc file. This file is used to configure user-specific
terminal behavior and customization.
[analyst@secOps ~]$ cat .bashrc
export EDITOR=vim
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
Do not worry too much about the syntax of .bashrc at this point. The important thing to notice is
that .bashrc contains configuration for the terminal. For example, the line PS1='\[\e[1;32m\][\u@\h \W]\$\[\
e[0m\] ' defines the prompt structure of the prompt displayed by the terminal: [username@hostname
current_dir] followed by a dollar sign, all in green. A few other configurations include shortcuts to
commands such as ls and vi. In this case, every time the user types ls, the shell automatically converts
that to ls –color to display a color-coded output for ls (directories in blue, regular files in grey, executable
files in green, etc.)
The specific syntax is out of the scope of this course. What is important is understanding that user
configurations are conventionally stored as hidden files in the user’s home directory.
d. While configuration files related to user applications are conventionally placed under the user’s home
directory, configuration files relating to system-wide services are place in the /etc directory, by
convention. Web services, print services, ftp services, and email services are examples of services that
affect the entire system and of which configuration files are stored under /etc. Notice that regular users do
not have writing access to /etc. This is important as it restricts the ability to change the system-wide
service configuration to the root user only.
Use the ls command to list the contents of the /etc directory:
[analyst@secOps ~]$ ls /etc
adjtime host.conf mke2fs.conf rc_maps.cfg
apache-ant hostname mkinitcpio.conf request-key.conf
apparmor.d hosts mkinitcpio.d request-key.d
arch-release ifplugd modprobe.d resolv.conf
avahi initcpio modules-load.d resolvconf.conf
bash.bash_logout inputrc motd rpc
bash.bashrc iproute2 mtab rsyslog.conf
binfmt.d iptables nanorc securetty
ca-certificates issue netconfig security
crypttab java-7-openjdk netctl services
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
e. Use the cat command to display the contents of the bash.bashrc file:
[analyst@secOps ~]$ cat /etc/bash.bashrc
#
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
# /etc/bash.bashrc
#
case ${TERM} in
xterm*|rxvt*|Eterm|aterm|kterm|gnome*)
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'printf "\033]0;%s@%s:%s\007"
"${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'
;;
screen)
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'printf "\033_%s@%s:%s\033\\"
"${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'
;;
esac
[ -r /usr/share/bash-completion/bash_completion ] && .
/usr/share/bash-completion/bash_completion
[analyst@secOps ~]$
The syntax of bash.bashrc is out of scope of this course. This file defines the default behavior of the shell
for all users. If a user wants to customize his/her own shell behavior, the default behavior can be
overridden by editing the .bashrc file located in the user’s home directory. Because this is a system-wide
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
configuration, the configuration file is placed under /etc, making it editable only by the root user.
Therefore, the user will have to log in as root to modify bash.bashrc.
Question:
Why are user application configuration files saved in the user’s home directory and not under /etc with all
the other system-wide configuration files?
Regular users do not have permission to write to /etc. Because Linux is a multi-user operating system,
placing user-application configuration files under /etc would keep users from being able to customize their
applications.
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
f. Save the file by selecting File > Save and close SciTE by clicking the X icon.
g. Click the Terminal application icon located on the Dock, at the bottom center of the Cisco CyberOPS VM
screen. The prompt should appear in red instead of green.
Question:
Did the terminal window which was already open also change color from green to red? Explain.
No. The .bashrc file is executed and applied when a terminal is first opened, so any previously opened
terminals will be unaffected by the changes to the .bashrc file.
h. The same change could have been made from the command line with a text editor such as nano. From a
new terminal window, type nano .bashrc to launch nano and automatically load the .bashrc file in it:
[analyst@secOps ~]$ nano .bashrc
export EDITOR=vim
[ Read 5 lines ]
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
j. Press CTRL+X to save and then press Y to confirm. The text editor nano will also offer you the chance to
change the filename. Simply press ENTER to use the same name, .bashrc.
k. The text editor nano will end, and you will be back on the shell prompt. This time reload the bash terminal
by entering the command bash in the terminal. The prompt should now appear in yellow instead of red.
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
1
2 #user html;
3 worker_processes 1;
4
5 #error_log logs/error.log;
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid;
10
11
12 events {
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
13 worker_connections 1024;
14 }
15
16
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 #tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65;
32
33 #gzip on;
34
35 types_hash_max_size 4096;
36 server_names_hash_bucket_size 128;
37
38 server {
39 listen 81;
40 server_name localhost;
41
42 #charset koi8-r;
43
44 #access_log logs/host.access.log main;
45
46 location / {
47 root /usr/share/nginx/html;
48 index index.html index.htm;
49 }
<Some output omitted>
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
b. While the configuration file has many parameters, we will configure only two: the port nginx listens on for
incoming connections, and the directory it will serve web pages from, including the index HTML
homepage file.
c. Notice that at the bottom of the window, above the nano commands, the line number is highlighted and
listed. On line 39, change the port number from 81 to 8080. This will tell nginx to listen to HTTP requests
on port TCP 8080.
d. Next, move to line 47 and change the path from /usr/share/nginx/html/ to
/usr/share/nginx/html/text_ed_lab/
Note: Be careful not to remove the semi-colon at the end of the line or nginx will throw an error on
startup.
e. Press CTRL+X to save the file. Press Y and then ENTER to confirm and use the custom_server.conf as
the filename.
f. Type the command below to execute nginx using the modified configuration file:
[analyst@secOps ~]$ sudo nginx -c custom_server.conf
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
i. After successfully opening the nginx homepage, look at the connection message in the terminal window.
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 20 www.netacad.com
Lab - Working with Text Files in the CLI
Question:
j. To shut down the nginx webserver, press ENTER to get a command prompt and type the following
command in the terminal window:
[analyst@secOps ~]$ sudo pkill nginx
k. You can test whether the nginx server is indeed shut down by first clearing the recent history in the web
browser, then close and re-open the web browser, then go to the nginx homepage at 127.0.0.1:8080.
Question:
Challenge Question: Can you edit the /etc/nginx/custom_configuration.conf file with SciTE? Describe the
process below.
Remember, because the file is stored under /etc, you will need root permissions to edit it.
From a terminal window, issue sudo scite /etc/nginx/custom_configuration.conf to launch scite as root.
Reflection
Depending on the service, more options may be available for configuration.
Configuration file location, syntax, and available parameters will vary from service to service. Always consult
the documentation for information.
Permissions are a very common cause of problems. Make sure you have the correct permissions before
trying to edit configuration files.
More often than not, services must be restarted before the changes take effect.
End of document
2018 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 20 www.netacad.com