BCS1110 1.0-RELEASE (GENERIC) bcs1110.ashish.nl/labs/lab-02/ [ Light ]
guest@bcs1110:~$ cat lab-02.md
# Lab 02 — Hi Tux

Objective

Learn basic Linux commands and file operations on the Raspberry Pi.

Introduction

In this lab you will learn how to work with the Linux shell on your Raspberry Pi.

Your main goal is to find a file that contains login information about your SSH logins, copy it, adjust its permissions, and transfer it to your own laptop.

By the end of this lab, you will know how to:

  • Navigate the directory structure
  • Create directories
  • Copy files
  • Change file ownership and permissions
  • Run commands with elevated privileges (sudo)
  • Securely copy files from the Raspberry Pi to your laptop

Tips

  • Raspberry Pi OS is based on Debian 12 (bookworm).
  • Your user (e.g. pi0010) is a regular user without admin rights.
  • The Linux file system is tree-structured, starting at /.
  • Review the Filesystem Hierarchy Standard (FHS) here: FHS 3.0.
  • If unsure about paths, use absolute paths instead of relative ones: absolute vs. relative paths.
  • Some commands require superuser privileges. Use sudo instead of su.
  • For help with commands, use the built-in manual:
  man command

Quit the manual by pressing q.

Preparation

Windows users
You can use ssh and scp directly from Command Prompt or PowerShell/Windows Terminal.

How to open a terminal on Windows:

  • Command Prompt: press Win, type “cmd”, press Enter.
  • PowerShell or Windows Terminal: press Win, type “powershell” or “windows terminal”, press Enter.

Verify OpenSSH is available:

  • Command Prompt: run where ssh and where scp.
  • PowerShell: run Get-Command ssh and Get-Command scp.

If ssh/scp are not found, install “OpenSSH Client”:

  • Settings → Apps → Optional Features → Add a feature → OpenSSH Client → Install, then reopen your terminal

macOS / Linux
Use the built-in ssh and scp from Terminal.
Verify: run which ssh and which scp

Linux Commands Explained

Before we begin the tasks, let's get familiar with some essential Linux commands you'll be using. If you want to learn more, you can see the Linux Command Cheat Sheet here.

1. Paths and Navigations

  • Who am I?
    The whoami command displays the username of the current user. This is useful when you need to know which user account you are currently using, especially in a multi-user environment or when working with shell scripts.

    Example:

    user@debian:~$ whoami
    user
    
  • Where am I?
    The pwd command prints current working directory.

    Example:

    user@debian:~$ pwd
    /home/user
    
    Linux directory structure

    Linux keeps everything in a single directory tree that starts at the root directory /. Think of / as the trunk, folders as branches, and files as leaves. A path like /home/user means: from root (/) go into home, then user. If a path begins with / it's absolute; otherwise it's relative to where you are now. The shorthand ~ expands to your home directory (e.g. /home/user). pwd shows your current absolute location in this tree.

    Mini example tree (simplified):

    /
    ├── bin
    ├── etc
    ├── home
    │   └── user
    │       └── lab
    └── var
        └── log
    
  • List files
    The ls command is used to display the contents of a directory.

    Example:

    user@debian:~$ ls
    file1.txt  file2.txt  folder1  folder2
    

    You can also use the ls command with various options to get more detailed information. For example, ls -l will display the long-format listing, which includes information about file size, owner, name, type and permissions, and more:

    user@debian:~$ ls -l
    total 8
    -rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
    -rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
    drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
    drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2
    

    There are more oprions you can use, for additional information use man ls

  • Move Around
    Use cd to change directories.

    • Absolute paths start at /: cd /var/log

    • Relative paths start from where you are: cd ../folder1

    • Shortcuts:

      • cd (no args) → your home (/home/user)
      • cd .. → jump back to the previous directory
      • .. → parent directory, . → current directory

      Example:

      user@debian:~$ cd /var/log
      user@debian:/var/log$ cd ..
      user@debian:/var$
      

2. Create a subdirectory and Copy Files

  • Make directory
    mkdir /path/newdir

  • Copy a file
    cp /path/src /path/destination/
    Use absolute path until you are comfortable.

3. Permissions and Ownership

  • See details
    Again, if you want to see longer file description, use ls -l

  • Ownership
    When we create a new file and examine its owenership and permissions, we see something similar:

    -rw-r--r-- 1 user user 0 Sep 12 12:35 /home/user/project/file.txt
    

    Visual map of each column in the long listing:

    -rw-r--r--  1  user  user   0  Sep 12 12:35  /home/user/project/file.txt
    ──────────  │   │     │     │      └───┬───┘             └──── full path (name)
    permissions │   │     │     │          │
                │   │     │     │          modified date & time
                |   |     |     |
                │   │     │     size (bytes)
                |   |     |
                │   │     group (primary group of owner)
                |   |
                │   owner (user who owns the file)
                |
                hard link count
    

    Notes:

    • The first block (-rw-r--r--) encodes file type + permission bits (expanded further below in the Permissions section).
    • Columns are separated by whitespace; some timestamps may include a year instead of time (older files).

    In Linux, every file and directory has an owner and a group associated with it. The owner is the user who created the file or directory, and the group is the primary group of the user who created it.

    To change the ownership of the file, we can use the chown command. The basic syntax is:

    chown [owner]:[group] [file/directory]
    

    Note: If group is omitted, only the owner is changed.

    Example:

    user@debin:~$ sudo chown user1 ~/project/file.txt
    user@debian:~$ ls -l ~/project/file.txt
    -rw-r--r-- 1 user1 user 0 Apr 24 12:34 /home/labex/project/dir1/file.txt
    

    Remember, you need to have the appropriate permissions to change the ownership of files and directories. If you're not the owner or don't have the necessary privileges, you'll need to use the sudo command to execute the chown operation.

  • Admin privileges
    sudo <command> runs a command as root. Some commands will not run without it, and you should expect a Permission denied error.

More about permissions

You have already seen this notation several times: -rw-r--r--, but what is it exactly?

Shortly, these are called permissions. Each file and directory has 3 permission categories:

  1. Owner: the creator of the file/directory
  2. Group: the group that the owner belongs to
  3. Others: everyone else who does not belong in the first two categories

Each of the categories has 3 permission types:

  1. Read (r): user can view the content of the file or list files in the directory
  2. Write (w): user can modify file's contents or delete/create files in the directory
  3. Execute (x): user can run files as a program or access contents of a directory

The first 10 characters represent the file permissions:

  1. The first character indicates the file type (- for regular file, d for directory).
  2. The next 3 characters represent the owner's permissions.
  3. The next 3 characters represent the group's permissions.
  4. The final 3 characters represent the permissions for others.

This is a broad topic, and you can access more materials about it here.

Visual examples

  1. Directory with mode drwxr-xr-x (a common default):
d  rwx  r-x  r-x
┬  ─┬─  ─┬─  ─┬─
│   │    │    │
│   │    │    └─ 4. Others  = read + execute
│   │    └────── 3. Group   = read + execute
│   └─────────── 2. User    = read + write + execute
└─────────────── 1. File type: d = directory ( - would mean regular file )
  1. Regular file with mode -rw-r--r-- (from the listing above):
-  rw-  r--  r--
┬  ─┬─  ─┬─  ─┬─
│   │    │    │
│   │    │    └─ Others = read
│   │    └────── Group  = read
│   └─────────── User   = read + write
└─────────────── File type: - = regular file

4. Viewing files

  • Quick print
    The cat command is a versatile tool that allows you to concatenate and display the contents of text files. The basic syntax of the cat command is as follows:

    cat [options] [file(s)]
    

    Example:

    user@debian:~$ cat sample.txt
    This is the first line.
    

5. Renaming and Moving files

  • Rename/move
    The Linux mv command is used not only to move files, but also to rename them. The syntax is: mv /path/oldname /path/newname

    Example:

    mv auth.log lab.txt
    

Use those commands to complete the tasks given to you.

Tasks

Using information given to you above or online sources, complete the list of following tasks. Your primary goal is to copy files from Raspberry Pi to your laptop.

1. Print the Current Directory

Show the full path of your current working directory.

Expected output:

/home/piet

2. Create a Subdirectory

Create a lab subdirectory inside your home directory.

3. Copy the SSH Log File

Attempt to copy the system authentication log (/var/log/auth.log) into your lab directory (/home/piet/lab).

Expect: a failure indicating you do not have permission to copy the file. A regular user can't copy a file which is owned by the superuser root.

Try the command with elevated permissions.

4. Check File Size

List the detailed information of the copied file in your lab directory and confirm its size and owner/group.

Example output:

-rw-r----- 1 root root 10380 Jul 18 10:09 /home/piet/lab/auth.log

5. Read the File

Try to read the copied file.

Expect: a Permission denied error.

Explain the error.

6. Change File Ownership

Change the ownership of the copied file so your user owns it.

7. Verify Ownership

List the file again and confirm the owner has changed to your user. Expected output:

-rw-r----- 1 piet root 10380 Jul 18 10:09 /home/piet/lab/auth.log

Can you see what have changed?

8. Read the File Again

Read the file now that you own it.

Now you should see the content, e.g.:

2025-07-18T09:28:37.362910+02:00 pi01 sshd[51661]: pam_unix(sshd:session): session opened for user piet(uid=1012) by (uid=0)
2025-07-18T09:28:41.173032+02:00 pi01 sudo:     piet : TTY=pts/0 ; PWD=/home/piet ; USER=root ; COMMAND=/usr/bin/cp /var/log/auth.log .

9. Explain Why You Can Read It

❓ Why can you now read the file? In one or two sentences, explain why reading now succeeds.

For more detail, review Linux file permissions: Linux Handbook: File Permissions.

10. Explore Other Information in the File

What other information or entries do you see in this log file?

11. Copy the File to Your Laptop

  1. Rename auth.log to lab.txt.

  2. Finally, copy lab.txt to your laptop. Note: run the command from your own terminal (not from the raspberry pi).

Windows (Command Prompt):

scp piNNNN@piNNNN.kale:/home/piet/lab/lab.txt %USERPROFILE%\Desktop\
  • %USERPROFILE% is (Windows CMD): Windows Command Prompt (cmd.exe) variable that expands to the current user’s profile directory, e.g. C:\Users\<YourName>. Works only in Command Prompt!

Windows (PowerShell):

scp piNNNN@piNNNN.kale:/home/piet/lab/lab.txt $HOME\Desktop\
  • $HOME% is (Windows PowerShell): automatic variable pointing to the current user’s home/profile directory (usually C:\Users\YourName). Equivalent to $env:USERPROFILE.

macOS / Linux

scp piNNNN@piNNNN.kale:/home/piet/lab/lab.txt ~/Desktop/
  • ~ is (macOS/Linux shells): tilde expansion for the current user’s home directory. ~/Desktop/ is the same as $HOME/Desktop/.

Replace:

  • NNNN with your Pi number
  • On first connection, accept the host key and enter your password.

You will need this file for your final submission.

Summary

In this lab you practiced:

  • Navigating directories and creating folders
  • Copying files with and without sudo
  • Checking and changing file ownership
  • Reading system logs
  • Renaming files
  • Transferring files from the Raspberry Pi to your computer

These are essential Linux skills you will use in all future labs.