~/src/www.mokhan.ca/xlgmokha [main]
cat unix-linux-systems-guide.md
unix-linux-systems-guide.md 24352 bytes | 2013-08-01 12:00
symlink: /dev/random/unix-linux-systems-guide.md

Unix & Linux Systems Guide

This is a collection of notes covering essential Unix and Linux system administration concepts and tools.

SSH (Secure Shell)

Installation

To install SSH in Ubuntu:

$ sudo apt-get install openssh-client openssh-server

For Cygwin, install the openssh package under “net” and run ssh-host-config after installation.

Basic Usage

Connect to a remote server:

$ ssh mo@<server> -p <port>

The standard port is 22, but you should change this for security. Update /etc/ssh/sshd_config or configure port forwarding on your firewall.

File Transfer with SCP

Copy files between local and remote machines:

# Copy from remote to local
$ scp user@192.168.1.100:/home/remote_user/Desktop/file.txt /home/me/Desktop/file.txt

# Copy from local to remote
$ scp /home/me/Desktop/file.txt user@192.168.1.100:/home/remote_user/Desktop/file.txt

# Copy multiple files with wildcards (note the quotes)
$ scp "user@192.168.1.100:/home/remote_user/Desktop/file*.txt" /home/me/Desktop/

# Copy directory recursively
$ scp -r user@192.168.1.100:/home/remote_user/Desktop/files /home/me/Desktop/

SSH Configuration

Create ~/.ssh/config to manage multiple keys:

Host github.com
  User git
  Hostname github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/github/id_rsa

Host gitlab.com
  User git
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlab/id_rsa

Key Management

Add keys to SSH agent:

$ ssh-add ~/.ssh/github/id_rsa

Set proper permissions:

$ chmod 600 ~/.ssh/github/*

Debug SSH connections:

$ ssh -v git@github.com

SSHFS - Remote Filesystem Mounting

Mount a remote filesystem:

$ mkdir ~/remote_mount
$ sshfs -C user@server.com: ~/remote_mount
$ ls ~/remote_mount

Unmount:

$ fusermount -u ~/remote_mount

tmux (Terminal Multiplexer)

tmux allows you to create persistent terminal sessions and split windows.

Key Concepts

Basic Commands

Start a new session:

$ tmux new-session -s session_name

Attach to existing session:

$ tmux attach-session -t session_name

List sessions:

$ tmux list-sessions

Key Bindings (Default prefix: Ctrl-b)

  • Ctrl-b d - Detach from session
  • Ctrl-b c - Create new window
  • Ctrl-b n - Next window
  • Ctrl-b p - Previous window
  • Ctrl-b % - Split window vertically
  • Ctrl-b " - Split window horizontally
  • Ctrl-b arrow - Navigate between panes

Configuration

Create ~/.tmux.conf for custom settings:

# Change prefix key
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Enable mouse support
set -g mouse on

# Start windows and panes at 1
set -g base-index 1
setw -g pane-base-index 1

# Status bar configuration
set -g status-right '%Y-%m-%d %H:%M'

grep (Global Regular Expression Print)

grep is essential for searching text patterns in files.

Video Tutorial

Basic Usage

Search for pattern in file:

$ grep "pattern" filename

Search recursively in directories:

$ grep -r "pattern" directory/

Case-insensitive search:

$ grep -i "pattern" filename

Show line numbers:

$ grep -n "pattern" filename

Regular Expressions

Search with regular expressions:

$ grep "^start" filename    # Lines starting with "start"
$ grep "end$" filename      # Lines ending with "end"
$ grep "[0-9]" filename     # Lines containing digits
$ grep ".*error.*" filename # Lines containing "error"

Advanced Options

  • -v - Invert match (show non-matching lines)
  • -c - Count matching lines
  • -l - Show only filenames with matches
  • -A n - Show n lines after match
  • -B n - Show n lines before match
  • -C n - Show n lines before and after match

grep with Pipes

Common pipeline patterns:

$ ps aux | grep nginx
$ cat /var/log/syslog | grep error
$ history | grep ssh

vim (Text Editor)

vim is a powerful modal text editor.

Learning Resources

Essential Commands

Modes

  • i - Insert mode
  • Esc - Normal mode
  • v - Visual mode
  • : - Command mode
  • h j k l - Left, down, up, right
  • w - Next word
  • b - Previous word
  • 0 - Beginning of line
  • $ - End of line
  • gg - Beginning of file
  • G - End of file

Editing

  • x - Delete character
  • dd - Delete line
  • yy - Copy line
  • p - Paste
  • u - Undo
  • Ctrl-r - Redo

Search and Replace

  • /pattern - Search forward
  • ?pattern - Search backward
  • n - Next search result
  • N - Previous search result
  • :%s/old/new/g - Replace all occurrences

Folding

Create and manage code folds:

" Create fold in visual mode
zf

" Open fold
zo

" Close fold
zc

" Set fold method
:set foldmethod=indent
:set foldlevel=0

" Fold more/less
zm
zr

Window Management

Split and resize windows:

" Set window width
Ctrl+w 100 |

" Split windows
:split
:vsplit

" Navigate windows
Ctrl+w h/j/k/l

Case Conversion

  • ~ - Toggle case of character
  • gu - Lowercase selection (visual mode)
  • gU - Uppercase selection (visual mode)

Tags and Navigation

  • Ctrl+] - Jump to tag
  • Ctrl+T - Jump back from tag

Useful Settings

:set spell          " Enable spell checking
:set number         " Show line numbers
:set hlsearch       " Highlight search results
:set incsearch      " Incremental search
:set autoindent     " Auto indentation
:set tabstop=2      " Tab width
:set expandtab      " Convert tabs to spaces

Converting Code to HTML

Use the :TOhtml command to convert your current buffer to HTML with syntax highlighting.

File Permissions and Ownership

Understanding Permissions

File permissions are displayed in the format: rwxrwxrwx

  • First three: Owner permissions
  • Second three: Group permissions
  • Last three: Other permissions

Where:

  • r = read (4)
  • w = write (2)
  • x = execute (1)

chmod Examples

$ chmod 755 file.txt    # rwxr-xr-x
$ chmod 644 file.txt    # rw-r--r--
$ chmod +x script.sh    # Add execute permission
$ chmod -w file.txt     # Remove write permission

chown and chgrp

Change ownership:

$ chown user:group file.txt
$ chown user file.txt
$ chgrp group file.txt

Process Management

Viewing Processes

$ ps aux                # All processes
$ ps aux | grep nginx   # Filter processes
$ top                   # Real-time process view
$ htop                  # Enhanced top (if installed)

Process Control

$ jobs                  # List background jobs
$ fg %1                 # Bring job 1 to foreground
$ bg %1                 # Send job 1 to background
$ nohup command &       # Run command immune to hangups

Signals

$ kill PID              # Terminate process (SIGTERM)
$ kill -9 PID           # Force kill (SIGKILL)
$ kill -HUP PID         # Restart/reload (SIGHUP)
$ killall process_name  # Kill all processes by name

System Information

System Stats

$ uname -a              # System information
$ whoami                # Current user
$ id                    # User and group IDs
$ uptime                # System uptime and load
$ df -h                 # Disk space usage
$ du -sh directory/     # Directory size
$ free -h               # Memory usage

Network Information

$ ifconfig              # Network interfaces
$ ip addr show          # IP addresses (modern)
$ netstat -tuln         # Network connections
$ ss -tuln              # Socket statistics (modern)
$ ping hostname         # Test connectivity

Package Management

Ubuntu/Debian (apt)

$ sudo apt update       # Update package list
$ sudo apt upgrade      # Upgrade packages
$ sudo apt install pkg  # Install package
$ sudo apt remove pkg   # Remove package
$ apt search keyword    # Search packages

CentOS/RHEL (yum/dnf)

$ sudo yum update       # Update packages
$ sudo yum install pkg  # Install package
$ sudo yum remove pkg   # Remove package
$ yum search keyword    # Search packages

Best Practices

Security

  1. Use SSH keys instead of passwords
  2. Change default ports for services
  3. Keep systems updated regularly
  4. Use firewall rules to restrict access
  5. Monitor logs for suspicious activity

Performance

  1. Monitor system resources regularly
  2. Use tmux for persistent sessions
  3. Learn keyboard shortcuts for efficiency
  4. Use aliases for common commands
  5. Script repetitive tasks

Productivity

  1. Master one editor (vim or emacs)
  2. Learn grep and regex patterns
  3. Use shell history effectively
  4. Customize your environment (.bashrc, .vimrc)
  5. Document your configurations