%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
graph TD
A("command<br/><code>ls</code>, <code>cd</code>, <code>echo</code>") --> B("options/flags<br/><code>-l</code>, <code>-a</code>, <code>-r</code>")
B --> C("arguments<br/><code>filenames</code>, <code>paths</code>")
C --> D("redirects<br/><code>></code>, <code><</code>, <code>|</code>")
style A fill:#d2562b,stroke:#fff,stroke-width:2px,color:#fff
style B fill:#2a6f77,stroke:#fff,stroke-width:2px,color:#fff
style C fill:#5b8c5a,stroke:#fff,stroke-width:2px,color:#fff
style D fill:#e8a33d,stroke:#000,stroke-width:2px,color:#000
Lab 9: Linux Admin
Lab 8 set up the terminal and SSH access to our EC2 instance. This lab covers the Linux administration we need once we’re logged in: bash command structure, filesystem paths, moving files to and from a server, and permissions.
Comprehension questions
What are the parts of a bash command?
A bash command has several key parts:
| Part | Description | Example |
| Command | The program to run | ls, cd, echo, grep |
| Options/Flags | Modify behavior, prefixed with (single) or (double) | - or -- |
| Arguments | What the command operates on | files, paths, text |
| Redirects | Send output elsewhere | > file, < input, | pipe |
Example: ls -la /home | grep .txt
Command: ls | Flags: -la | Argument: /home | Pipe: | | Chain: grep .txt
What is the difference between a relative and an absolute path?
An absolute path always starts with / and works from anywhere. A relative path does not start with / and is interpreted from the current working directory. In a relative path, .. goes up one level and . refers to the current directory.
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
graph LR
Path(["Filesystem<br>Paths"])
Absolute("<strong>Absolute Path</strong><br/>Starts from root<br>directory /")
AbEx1("<code>/home/user/documents/file.txt</code>")
AbEx2("<code>/etc/passwd</code>")
AbDescr("Always same location<br>regardless of current<br>directory")
Relative("<strong>Relative Path</strong><br/>Relative to current<br>directory")
RelEx1("<code>documents/file.txt</code>")
RelEx2("<code>../parent-folder/file.txt</code>")
RelDescr("Changes meaning based<br>on where you are<br>in filesystem")
Path -->|"Full from root"| Absolute
Absolute --> AbEx1
Absolute --> AbEx2
Absolute --> AbDescr
Path -->|"From current location"| Relative
Relative --> RelEx1
Relative --> RelEx2
Relative --> RelDescr
style Path fill:#e8a33d,stroke:#000,stroke-width:2px,color:#000
style Absolute fill:#d2562b,stroke:#fff,stroke-width:2px,color:#fff
style Relative fill:#2a6f77,stroke:#fff,stroke-width:2px,color:#fff
What are some ways to direct them to run in a particular place on the filesystem?
Four ways to control where a command runs are listed below.
- Change to a directory, then run the commands:
cd /path/to/directory - Run the script with the absolute path directly:
/absolute/path/to/file.sh - Run the script relative to current location:
relative/path/file.sh - Run the script in the current directory (explicit):
./file.sh
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
graph LR
Control(["Ways to Control<br>Execution Location"])
CD("Use <code>cd</code>to change<br>directory first")
ABS("Use the absolute path<br/>(specify full path)")
REL("Use the relative path<br/>(but specify from<br>current location)")
Dot("Run from current directory with <code>./file.sh</code>")
Control --> CD
Control --> ABS
Control --> REL
Control --> Dot
style Control fill:#d2562b,stroke:#fff,stroke-width:2px,color:#fff
style CD fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
style ABS fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
style REL fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
style Dot fill:#5b8c5a,stroke:#fff,stroke-width:1px,color:#fff
How can you copy, move, or delete a file? What about to or from a server?
Local operations include cp, mv and rm:
| Command | Description |
|---|---|
cp /source/destination |
copy a file |
cp -r source_dir destination_dir |
copy a directory recursively |
mv source destination |
move or rename a file |
rm filename |
delete a file (permanent, no recovery) |
rm -r directory |
delete a directory and it’s contents |
Server operations (via SSH) include scp, rsync, and sftp
| Command | Description |
|---|---|
scp local_file user@server:/remote/path |
copy to server |
scp user@server:/remote/file local_path |
copy from server |
scp -r local_dir user@server:/remote/path |
copy directory to server |
rsync -av local_dir user@server:/remote/path |
sync files (better for large operations) |
sftp user@server |
interactive file transfer session |
Key note: rm is permanent! There is no trash can in Linux. Use the -i flag to prompt before deletion: rm -i filename
Mind map of: operating system, Windows, MacOS, Unix, Linux, Distro, Ubuntu
%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%
mindmap
root((Operating System))
Commercial
Windows
GUI-focused
Proprietary<br>kernel
MacOS
Unix-based
Proprietary<br>kernel
Desktop &<br>laptop
Unix family
Unix
Original<br>design
POSIX<br>standard
BSD, Solaris
Linux
Free & open<br>source
POSIX<br>compatible
Kernel +<br>utilities
Distros
Ubuntu
Debian-based
User-friendly
Server &<br>desktop
CentOS
RedHat-based
Enterprise
Fedora
Cutting<br>edge
RedHat-based
Distro
Linux kernel +<br>tools
Different package<br>managers
Different<br>defaults
Community<br>focused
Key relationships between operating systems:
The Operating System is the software that manages hardware and runs programs. Windows and MacOS are commercial proprietary operating systems. Unix is the original multiuser system, developed at AT&T Bell Labs in the 1970s; it began as proprietary software, and its design was later standardized as POSIX.
Linux is a free, open-source Unix-like kernel created by Linus Torvalds, and the distro (Distribution) is the Linux kernel packaged with utilities, package manager, defaults. Ubuntu is a popular Linux distro based on Debian.
What are the 3x3 options for Linux file permissions? How are they indicated in ls -l?
The 3×3 permission matrix:
| Owner (User) | Group | Other |
|---|---|---|
| Read (r) = 4 | Read (r) = 4 | Read (r) = 4 |
| Write (w) = 2 | Write (w) = 2 | Write (w) = 2 |
| Execute (x) = 1 | Execute (x) = 1 | Execute (x) = 1 |
How they appear in ls -l:
-rw-r--r-- 1 user group 1234 Jan 15 10:30 file.txt
^ ^^^^^^ ^^^^^^ ^^^^^^
| | | |
| | | +-- Other: read only
| | +--------- Group: read only
| +-------------- User: read & write
+----------------------- File type (- = regular file, d = directory)
Breaking down permissions:
- Position 1: File type (
-file,ddirectory,llink) - Positions 2-4: Owner permissions (rwx)
- Positions 5-7: Group permissions (rwx)
- Positions 8-10: Other permissions (rwx)
Octal notation (chmod):
chmod 755 file: Owner: rwx (7), Group: rx (5), Other: rx (5)chmod 644 file: Owner: rw (6), Group: r (4), Other: r (4)chmod 700 file: Owner: rwx (7), Group: (0), Other: (0)chmod 600 file: Owner: rw (6), Group: (0), Other: (0): typical for SSH keys
Recap
A bash command is a command, its options or flags, its arguments, and any redirects. Absolute paths start with
/and work from anywhere, while relative paths are interpreted from the current working directory.cp,mv, andrmhandle files locally, andscp,rsync, andsftpmove files to and from a server over SSH.rmis permanent, sorm -iis worth the extra prompt.Linux permissions are read, write, and execute for the owner, the group, and everyone else, shown in positions 2 through 10 of
ls -l.chmodsets them in octal notation, and600(read and write for the owner only) is the typical setting for SSH keys.
Linux Commands puts these commands to work on the EC2 instance, and Linux Users follows with a non-root user and a dedicated service account.