This note is based on the book - How Linux Works 3rd edition - by Brain Ward. I find it one of the best book for anyone trying to learn linux. With that said, I wish sometime there was a little bit more explanation of the some of the topics in depth. Thats when Unix and Linux System Administration Handbook comes in handy. It is the holy bible of linux. With 1180 pages, I only occasionally visit some topics but most of the time it is a shelf decorator.
The content below is the notes/summary from Chapter 1 and 2 of the book and mostly about basic commands.
The Bourne Shell : /bin/sh
Linux uses an enhanced version of the Bourne shell called bash or the “Bourne-again” shell. /bin/shell is a link to bash in Linux.
1
2
| # To change Shell
chsh
|
1
2
3
4
5
6
7
8
9
10
| $ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/usr/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/dash
/usr/bin/tmux
|
1
2
3
4
5
6
7
8
| # MAN page for chsl
$ chsh -h
Usage: chsh [options] [LOGIN]
Options:
-h, --help display this help message and exit
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL new login shell for the user account
|
1
2
| # Example
$ chsh -s /usr/bin/tmux
|
Basic Commands
1
2
| cat # Output the contents of one or more file
$ cat /etc/passwd
|
1
2
3
4
5
| $ ls # List the content of a directory
$ ls --help
$ ls -l #detailed long list
$ ls -R # list subdirectories recursively
$ ls -a # Don't ignore entries starting with .
|
1
2
3
| $ cp file1 file2
$ cp file dir
$ cp file1 file2 file3 dir
|
1
2
| $echo Hello Again.
Hello Again.
|
Navigating Directories
1
2
3
| / -> root
../ -> parent directory
./ -> Current directory
|
1
2
3
| $ cd dir # Change Directory
$ mkdir dir # Make Directory
$ rmdir dir # Remove Directory
|
Shell Globbing
1
2
3
4
| > at* expands to all files that start with at
> *at expands to all the files that end with at
> *at* expands to all files that contains at
> b?at expands to match exactly one arbitrary character # b?at matches boat and brat
|
grep
1
2
3
4
| $ grep root /etc/passwd
# Output
root:x:0:0:root:/root:/bin/bash
nm-openvpn:x:121:122:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin
|
It lets display the content of a very large file one screenful at a time. To go to next page press space, to go back, press b, to quit q
1
| $ grep ie /usr/share/dict/words | less
|
1
2
| # print working directory
$ pwd
|
1
2
| # difference between two files
$ diff file1 file2
|
1
2
3
4
5
6
| # Check file or directory
$ file CCNA
CCNA: directory
$ file ./CCNA/CISCO\ commands
./CCNA/CISCO commands: very short file (no magic)
|
Find
1
2
3
4
5
6
7
8
| # Find and Locate
$ find dir -name file -print
# Example
find /var/www -type f -name "*.php" -path "*/wp-content/*"
│ │ │ │
│ │ │ │
Where to start Only regular Filename must Full path must
the search files end with .php contain /wp-content/
|
Scenario
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| /var/www
├── site1
│ ├── wp-content ← matches
│ │ └── themes
│ │ └── style.php
│ └── index.php ← does NOT match (no wp-content in path)
├── site2
│ ├── cache ← does NOT match
│ └── somefile.php
└── old-projects
└── wordpress-2023
└── wp-content ← matches
└── plugins
└── plugin.php
|
1
2
3
4
5
6
| # Find
$ find /var/www -type f -name "*.php" -path "*/wp-content/*"
# Output
/var/www/site1/wp-content/themes/style.php
/var/www/old-projects/wordpress-2023/wp-content/plugins/plugin.php
|
1
2
3
4
5
| # Head and Tail
$ head /etc/passwd # First 10 lines
$ tail /etc/passwd # Last 10 lines
$ head -5 /etc/passwd # First 5 lines only
|
Shell Variable and Environment Variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # 1. Normal shell variable (local only)
count=42
echo $count # → 42
bash # open new shell
echo $count # → (empty)
exit # back to original shell
echo $count # → 42 (still exists here)
# 2. Environment variable (inherited)
export NAME="John"
echo $NAME # → John
bash # new shell
echo $NAME # → John (inherited!)
exit
# 3. Most common real-life pattern
export PATH="$HOME/bin:$PATH" # add your own bin folder permanently
export EDITOR=nano # set default editor
export LANG=en_US.UTF-8 # set language
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # Command Path
$ echo $PATH # list of folders your shell searches
/usr/local/sbin:/usr/local/bin:...
# type: tells you what the shell will actually run when you type a command
$ type ls
ls is aliased to `ls --color=auto`
$ which ls
/usr/bin/ls
$ which -a ls
/usr/bin/ls
/bin/ls
|
1
2
3
4
5
6
7
8
9
10
11
| # Modifying Path -Temporary
# Disappears after session is closed
# Add a directory at the BEGINNING (highest priority)
export PATH="/new/path:$PATH"
# Add at the END (lowest priority)
export PATH="$PATH:/new/path"
# Example - add your personal bin folder
export PATH="$HOME/.local/bin:$PATH"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # Permanent Modification
# 1. First check which shell you are using
$ echo $SHELL
/bin/bash
# 2. Edit Shell's start up file
# For bash users (most common on Ubuntu, Debian, Fedora, etc.)
$ nano ~/.bashrc
# or
$ vim ~/.
# 3. Add path
# Option A - Add at the beginning (recommended - your programs first)
$ export PATH="$HOME/.local/bin:$PATH"
# Option B - Add at the end (system programs first)
$ export PATH="$PATH:$HOME/my-custom-scripts"
# Option C - Multiple additions
$ export PATH="$HOME/bin:$HOME/.local/bin:/opt/mytool/bin:$PATH"
# 4. Apply the changes
$ source ~/.bashrc
# 5. Verify
$ echo $PATH
|
Command Line Editing
1
2
3
4
5
6
7
8
9
10
11
| # Holy trinity — 80% of your editing power
Ctrl + A → beginning
Ctrl + E → end
Ctrl + W → delete last word
Ctrl + U → delete everything before cursor
Ctrl + K → delete everything after cursor
Ctrl + R → search history (type and press repeatedly)
Ctrl + L → clear screen (keep command)
# Bonus power combo
Ctrl + W Ctrl + Y → cut last word → paste it somewhere else
|
man page
Online Manual Section
| Section | What it contains | Typical examples |
|---|
| 1 | Executable programs / commands | ls, grep, cat, passwd (the command) |
| 2 | System calls (kernel functions) | fork, open, read, write |
| 3 | Library functions | printf, strlen, malloc |
| 4 | Special files (devices) | /dev/sda, /dev/null |
| 5 | File formats and conventions | passwd, shadow, hosts, crontab |
| 6 | Games | tetris, nethack |
| 7 | Miscellaneous (protocols, standards, etc.) | regex, utf-8, systemd.unit |
| 8 | System administration commands | useradd, systemctl, fdisk |
1
2
| man passwd # shows the command passwd (how to change passwords) → section 1
man 5 passwd # shows the file format of /etc/passwd → section 5
|
1
2
3
4
5
6
| $ command > file # erase content of file and add the command
$ command >> file # append
$ head < /proc/cpuinfo
$ head /proc/cpuinfo | tr a-z A-Z # tr command translate all cpu info from lowercase to upper case
|
Standard Error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # First command
ls /ffff > f 2>e
# After running:
ls -l
-rw-r--r-- 1 user user 0 ... f ← empty file
-rw-r--r-- 1 user user 39 ... e ← contains the error
cat e
ls: cannot access '/ffff': No such file or directory
# Second command (the classic one)
ls /ffff > f 2>&1
# After running:
ls -l
-rw-r--r-- 1 user user 39 ... f ← only one file, contains the error
cat f
ls: cannot access '/ffff': No such file or directory
|
/etc/sudoers
Instead of using nano, vim or any other file editor , always use visudo command to edit the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| $ sudo visudo
# Output
...
...
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "@include" directives:
@includedir /etc/sudoers.d
|
1
2
3
4
| # to add user
sudo usermod -aG sudo username
# or
sudo adduser username sudo
|
Modern sudo includes all files from /etc/sudoers.d/. This is cleaner than editing the main /etc/sudoers file, especially on managed systems.
Example
Name the file after the user or purpose /etc/sudoers.d/karking
1
2
| $ sudo visudo -f /etc/sudoers.d/username
# -f tells visudo to edit this specific file instead of the default /etc/sudoers
|
Modify the file
1
2
3
4
5
6
7
| karking ALL=(ALL:ALL) ALL # Full sudo (like members of the sudo group)
karking ALL=(ALL) NOPASSWD: ALL # Full sudo, no password prompt (use sparingly!)
karking ALL=(ALL) NOPASSWD: /usr/bin/apt update, /usr/bin/apt upgrade # Only specific commands, no password
karking ALL=(postgres) NOPASSWD: /usr/bin/psql # Run as another user (e.g., as postgres)
|
Quick verification command
1
2
3
4
| sudo visudo -c -f /etc/sudoers.d/username # Just syntax-check this file
sudo visudo -c # Check entire config
ls -l /etc/sudoers.d/ # Should show -r--r----- root root
sudo whoami
|