Skip to content
Search

Shell:Basic

lastmod 2024-06-20created 2022-09-2342 edits

Shell basics.

shell

A shell is a program that provides an interface for implementing various operating system functions and services on an operating system. It is called a shell (the English word for an outer casing) because it is the layer that wraps the interface between the user and the internals of the operating system (the kernel).

Shells are generally classified into two types: command-line and graphical.

  • A command-line shell provides a command-line interface (CLI) on the operating system, whereas
  • a graphical shell provides a graphical user interface (GUI).

Shell programs

Terminal emulators

Basic shell commands

  • pwd command - print name of current/working directory
  • cd command - Change the shell working directory
  • echo command - display a line of text
  • ls command - list directory contents
  • mkdir command - make directories
  • rm command - remove files or directories
  • cp command - copy files and directories
  • mv command - move (rename) files
  • cat command - concatenate files and print on the standard output
  • touch command - change file timestamps
  • man pages
  • env command

Special commands

  • Shortcuts
    • ~: Home Directory
    • .: Current Directory
    • ..: Parent Directory
  • Redirecting the command output: >, >>
  • Pipe |
  • history command
    • !n - Runs the history entry at line number n. Negative numbers are also allowed; !-1 is the same as !!.
    • !! - The history entry with the highest number. That is, the command you just ran
  • Shebang: #! on the first line
  • Here document - Passes a string to stdin in a form like << EOF.
  • export environment variable
  • source command

Developer essentials

Advanced

ETC

  • cal command
  • whoami command
  • id command
  • tree command

Filesystem

Before learning anything else, we should look at how files and directories are organized within a Linux system:

$ ls /
bin boot dev etc home lib lib64 lost+found mc media mnt opt output proc root run sbin srv sys tmp usr var

Useful commands

  • ln
    • Creating soft link to a file - ln -s [소스] [링크위치]
    • Creating hard links
  • tar
    • Extracting a tar file
    • Creating a tar file
  • vim editor
    • :q to exit vim
    • Open a new file or edit an existing file
    • Different modes of vim - vimdiff
    • :w to save a file
    • :q! to quit without saving
  • Becoming root user
    • Using sudo command
  • !! trick
  • Environment variables
    • Setting up environment variable values
  • locate command
  • Finding date/time in different timezones
  • Bash history
  • Sort files by size
  • uniq/sort command

Using Curl

  • Viewing a file
  • Downloading the file
  • Download with the same name
  • Doing POST request using curl
  • Following redirection
  • Example: to view github’s pull request patch
  • Viewing more details about the transfer
  • Doing multiple requests at once
  • Inspecting HTTP headers
  • Adding new HTTP headers
  • Curl book

Users and groups

  • Finding the owner of file
  • /etc/passwd file
  • Details about groups
  • wheel group
  • Becoming superuser
  • Adding a new user
  • Changing user passwords
  • Modifying existing user details
  • Deleting a user
  • Adding a new group
  • Adding new group to an user

File permissions

  • chmod, chown command
  • PATH variable
  • ~/.bash_profile file
  • .bashrc file
  • /etc/profile file
  • which command
  • Use which command to see how $PATH variable works
  • she-bang or sha-bang in executable files

Processes in Linux

  • How to view all running processes?
  • How to find a particular process?
  • How to kill/stop a particular process?
  • Finding out list of open files
  • Signals
  • top command
  • Load average
  • htop tool
  • More about Linux processes
  • /proc directory
  • /proc/cpuinfo
  • /proc/cmdline
  • /proc/meminfo
  • /proc/uptime
  • /proc/sys/ & sysctl command
  • Enabling IP forward with sysctl

Linux services

  • What is a service?
  • What is a daemon?
  • What is the init system?
  • Units in systemd
  • .service units in systemd
  • How to find all the systemd units in the system?
  • Working with a particular service
  • Enabling or disabling a service
  • Shutdown or reboot the system using systemctl
  • journalctl
  • Finding the logs of a service
  • To view only the last N entries
  • Continuous stream of logs
  • Listing of previous boots
  • Time-based log viewing
  • Total size of the journal logs
  • Writing your own service file

Securing services with systemd

  • Installing verybad service
  • Vulnerabilities in the application
  • Directory traversal vulnerability/ LFI
  • Arbitary file write vulnerability
  • Remote code execution (RCE) vulnerability
  • Remove access to system’s tmp directory
  • Protecting home dirctories
  • Fixing directory paths
  • DynamicUser
  • Allowed Executables
  • What is next?

Package management

  • dnf command
  • Searching for a package
  • Finding more information about a package
  • Installing a package
  • To list the available updates
  • To list all security updates
  • Update the packages via dnf
  • Find out the services & applications need restart after update in Fedora/CentOS/RHEL
  • Automatic updates in dnf systems
  • apt command
  • apt update
  • Installing a package via apt
  • apt-cache search
  • Listing upgrades
  • Upgrading packages
  • Figuring out which services/processes need restart after package upgrade in Debian
  • Listing available security updates in Debian systems
  • Unattended upgrades in Debian systems

SELinux

  • SELinux Modes
  • getenforce
  • setenforce
  • Labels/Contexts
  • Checking contexts of files/directories or processes
  • SELinux booleans

Mounting file systems

  • Connecting USB drives to your system
  • Mounting a device
  • Unmounting
  • Encrypting drives with LUKS (for only Linux)
  • Encrypting drives for any OS using Veracrypt

Networking commands

  • Finding the IP address
  • Finding ARP table
  • ping command
  • Short note about DNS
  • /etc/hosts
  • /etc/resolv.conf
  • systemd-resolved controlled name resolution
  • resolvectl command
  • host command
  • dig command
  • ss command
  • traceroute command
  • tracepath command
  • Remote login to a computer using ssh tool
  • ssh key generation
  • ssh-copy-id
  • Stop and disable the sshd service
  • Disable password based login for ssh
  • How to find active (open) network connections from your computer?
  • To know about ports

Linux firewall

  • Installation
  • Tables, chains and rules
  • filter table
  • nat table
  • iptables command
  • View the existing rules
  • Appending rules to INPUT chain
  • Flushing all rules
  • Example of a series of rules
  • Delete a rule based on rule number
  • Delete a rule directly
  • Saving the rules
  • A blog post from Major Hayden
  • Debugging firewall rules

Miscellaneous

  • w command
  • How long is the system running?
  • Finding CPU time of a command
  • dmesg command
  • Setting up cron jobs
  • Finding out details about previous logins or system reboots

Practice

It is a good idea to create an isolated space with vbox or vagrant, etc.

  • copy paste
  • Find your user id
  • Creating softlinks
  • Basic vim usage
  • Adding a new user
  • Deleting an existing user
  • Finding the IP address of dgplug.org
  • Change the local timezone of the system
  • Add sudo access to an user

See also

Favorite site

References


  1. 44bits_-_linux-and-mac-command-line-survival-guide-for-beginner.pdf ↩

author Zer0 sitemap