Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

First Login and Shell Basics for Administrators

Log in to a Linux server over SSH, learn what the prompt tells you, use history, tab completion, pipes and man pages, and adopt shell habits that prevent costly mistakes.

Most servers have no screen. You reach them over SSH and work in a shell, a program that reads commands and runs them. This lesson covers the first login, how to read the prompt, and the habits that separate careful administrators from the ones with horror stories.

Connecting with SSH

Terminal
ssh [email protected]          # user and host
ssh -p 2222 dana@203.0.113.10       # non-default port
ssh -i ~/.ssh/id_ed25519 dana@web01 # specific key

The first time you connect, SSH shows the host key fingerprint and asks you to confirm it. Compare it with a trusted source if you can; the fingerprint is then stored in ~/.ssh/known_hosts and checked on later visits. A warning that it changed means the server was rebuilt or, rarely, that someone is intercepting traffic. Do not ignore it.

To log in with a key instead of a password, create a key pair on your computer and copy the public half to the server:

Terminal
ssh-keygen -t ed25519 -C "dana laptop"
ssh-copy-id dana@web01

The private key never leaves your machine. Hardening SSH is covered later in the security chapter.

Reading the prompt

A typical prompt looks like dana@web01:~$. It tells you the user, the host, the current directory (~ means your home) and the privilege level: $ for a normal user, # for root. If you manage several servers, put the hostname in the prompt and glance at it before every destructive command.

Commands worth knowing from day one

Terminal
whoami           # current user
id               # user id, group ids
pwd              # where am I
uptime           # load averages and time up
w                # who is logged in
last -n 5        # recent logins
date; timedatectl

History, completion and shortcuts

  • Tab completes commands, paths and options. Press it twice to list candidates.
  • Up arrow and Ctrl+R recall and search earlier commands.
  • !! repeats the previous command; sudo !! repeats it with sudo.
  • Ctrl+A and Ctrl+E jump to the start or end of the line, Ctrl+C interrupts, Ctrl+D ends input or logs out.
  • history | grep nginx finds an old command.

Getting help

Terminal
man ssh              # manual page; q quits, / searches
man 5 passwd         # section 5 = file formats
ssh --help
apropos partition    # search manual summaries

Manual sections you will meet: 1 user commands, 5 file formats, 8 administration commands. Reading the man page of an unfamiliar command before running it as root is an excellent habit.

Pipes and redirection

A pipe sends one command's output into the next. Redirection sends it to a file.

Terminal
ps aux | grep sshd
ls /etc | wc -l
dmesg | tail -n 20
sudo journalctl -u ssh > ssh.log      # overwrite
echo "note" >> notes.txt              # append
some-command 2> errors.txt            # standard error only
some-command > all.txt 2>&1           # both streams

Safe habits

Before you press Enter on a command that deletes, formats or changes ownership, read it once more. Check the path, check the host in your prompt, and be suspicious of a stray / or *.

  1. List before you delete. Run ls with the same glob first, then rm.
  2. Quote variables in scripts: rm -- "$file", not rm $file.
  3. Use -i for interactive confirmation when deleting a few files.
  4. Keep a second session when changing SSH, firewall or network settings.
  5. Use sudo per command, not a root shell, so actions are logged.
  6. Use tmux or screen for long jobs, so a dropped connection does not kill them.
flowchart LR
    A["Type command"] --> B{"Destructive?"}
    B -->|Yes| C["Preview with ls or echo"]
    C --> D["Check host and path"]
    D --> E["Run"]
    B -->|No| E

Preview before you destroy.

A stand-in: parsing the prompt

This model splits a prompt string into user, host and privilege level. It is plain Python; the prompts are examples.

Python Stand-in: read the prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: how pipes compose

A pipeline is a chain of small functions. This model shows the same idea with Python, filtering made-up process lines the way grep and wc -l would.

Python Stand-in: a pipeline in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Pasting multi-line commands from a web page without reading them.
  • Running chmod -R or chown -R on / or on a path with a typo.
  • Forgetting that > overwrites a file.
  • Logging in as root over SSH instead of using a named account.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a # at the end of the prompt usually mean?

    $ is a normal user, # is root.
  2. 2. Which keys search your command history interactively?

    Ctrl+R opens a reverse search through history.
  3. 3. What does >> do compared with >?

    truncates the file, >> appends to it.

  4. 4. Why use tmux for a long job over SSH?

    tmux keeps the session on the server so you can reattach.

Summary

  • SSH is the main door; confirm the host key and prefer key login.
  • The prompt shows user, host, directory and privilege.
  • History, tab completion, man pages, pipes and redirection speed you up.
  • Preview destructive commands, quote variables, keep a second session.

Next: The Filesystem Hierarchy for Administrators