Bo2SS

Bo2SS

3 Basic Knowledge of Linux

Course Content#

Transition from Graphical [Windows] to Character Interface [Linux]#

Errors and Prompts#

Graphical → Text Information

  • Never ignore screen output; pay close attention to every response from the system!
  • Program return values are reflected on the right side of the next line
    • Image
    • return 0 indicates success

Command Format#

Command [options] [option parameters]... [parameters]...

  • Options: --full option name -short option name
  • Spaces: Any number of spaces counts as one space, serving as a separator

Linux Considers Security More#

Users, User Groups, Permissions

  • Change Password: passwd
  • Exit User: exit, logout, ctrl + d
  • Create New User: useradd
  • Home Directory: /home, other users may not have modification permissions in other directories
  • Groups
  • [PS] Everything is a file; all devices, abstract processes, running data, CPU...
    • User-related files /etc/passwd, /etc/group, related commands usermod, userdel, id
    • id: can view your own or a specified user's uid, gid (primary group), groups (belonging groups)

Users and Groups#

  • Image
  • Permissions: Character representation (r readable, w writable, x executable) → 8/10 decimal description
  • Each file has an owner, a owning group, and other users
  • Enter ls -l to view
    • img
    • See the red box, file types l, d, etc., see additional knowledge point - 7 types of files

Browsing Files#

Linux has only one tree, starting from the root directory /

Who Am I, Where Am I, Where Am I Going#

  • Who am I whoami, who am i (seeing the essence)
    • Image
    • whoami: the system feels who you are, who am i: who you actually are
  • Where am I pwd: print working directory
  • Where am I going cd: change directory
  • [PS] When writing commands or paths, make good use of the Tab key

Software Installation (Ubuntu)#

  • .deb: use dpkg -i xxx.deb
  • .tar: use tar to decompress
  • apt installation: can automatically resolve dependencies, can choose suitable software source addresses
    • Where did it install? Dispersed: bin, lib; /usr/→bin, include, lib, local, share (manual), src
    • Related commands
      • Image
      • search can find a software, check if it exists, may have forgotten the name
      • apt remove xxx --purge (do not keep configuration files)
      • apt autoremove should be used with caution, as it is managed by apt, it may mistakenly delete something that is needed but not running, or software downloaded in other ways

Common Linux Commands#

File and Directory Operations#

  • Image
  • ln: soft link, hard link

Modifying and Viewing File Contents#

  • Image
  • ⭐ The Three Musketeers of Linux: grep [Global Regular Expression Print], awk [Data Processing], sed [Batch Operations]
  • ❗ Pipe: | passes the output of the previous command to the next command
  • less is friendlier than more, with more features
  • wc: word count, can accept parameters -l [line count] etc.

Finding and Locating Files#

  • Image
  • which: find the specific location of executable files, which one is being used
  • locate
    • Based on indexing, searching is very fast
    • 【Not updated in real-time】, because updating requires traversing all files, which is slow
    • Will be updated regularly, or use sudo updatedb to immediately update the database
  • Image
  • sudo -i: log in with superuser privileges to the default shell
  • chmod: change mode
  • chown: change owner
  • Image
  • Commonly used ps -ef, outputs detailed information of all processes
  • kill specify PID [process ID] to kill the process
  • pkill can batch kill processes matching characters, only killing those the user has permission to kill, use sudo pkill with caution
  • crontab -e: edit [scheduled tasks], refer to the format inside
  • ctrl + z, fg, bg, jobs
    • jobs view process numbers
    • ctrl + z will pause the process
    • bg can run in the background, output will be displayed in the terminal
    • %2 (or fg %2) [under zsh], fg 2 [under bash] brings process 2 to the foreground
    • [PS] fg can be used when modifying source files, gcc, to improve efficiency

Obtaining System Information#

  • Image
  • Can be used for tuning
  • Check if resources are occupied by others, check if the system has been compromised: top, htop, nmon
  • 【How to reduce the chances of cloud host being compromised】
    • Cancel password, configure sshd, log in via public and private key
    • Change to a complex password
    • Change the default username or create a new user
    • Change the ssh connection port
  • -h speaks human language, friendly display
    • du -h, friendly display of directory file sizes, K, M
    • free -h, friendly display of memory data
  • nmon: suitable for checking when the system has issues
  • ifconfig [interfaces config] displays network device information meaning-blog
    • Can see private IP, cannot see public IP
  • uname -a: prints all available system information, can also use cat /etc/os-release

Other Commands#

  • Image
  • Used to commonly use telnet before ssh [open source]
  • 【scp uses ssh to copy】
    • Remote 👉 Local: scp username@ip_address: remote file local path
    • Local 👉 Remote: scp local file username@ip_address: remote path
  • After using poweroff on the cloud host, you need to connect to the console to power it on

Summary of Basic Knowledge#

Terminal & Shell#

  • Terminal is like a device
  • Shell software runs inside the Terminal, such as bash, zsh, used to interpret commands entered in the Terminal
  • When Shell receives commands: whether built-in → look for paths in the system environment variable PATH, call

Delimiters#

  • Some special symbols also belong to delimiters: pipe |, redirection >, >>, <, <<, background running &, sequential execution &&
    • Redirecting to /dev/zero, /dev/null: input to a data black hole, anything input here is directly ignored, see man zero for details 【/dev: devices】

Programs and Processes#

  • A program is an executable binary file; a process is the program's image in memory, instantiated

Paths#

  • Absolute Path: starting point is the root directory /
  • Relative Path: make good use of current path "." and parent path ".."
  • Remote Path: protocol://username@location/path
  • Special Path: ~username = home directory of the username, - = last working directory

Software#

  • There is no concept of a registry in Linux
  • Image

Hidden Files#

  • Names starting with . are hidden
  • Special directories: current directory "." and parent directory ".."

File Types#

【7 types of file types】

  • Image
  • link: can be temporarily understood as a shortcut in Windows
  • block: most blocks in the system are 4096Byte = 4KB, serving as a buffer, analogous to express delivery
  • character: devices are also files, such as virtual terminal /dev/pts/0
  • socket: based on the network, it is indispensable
  • pipe: does not occupy memory, only responsible for transmission, for example, one-time transmission: echo > pipe file, cat pipe file
  • 3 types of ordinary files
  • Image

Modifying File Permissions#

  • chmod
    • Make good use of +, -, =
      • Image
      • a = all, + adds permissions, - removes permissions, = directly overwrites permissions
    • Try to minimize the use of chmod 777, as it opens too many permissions
  • chown
    • Can modify the user and group to which the file belongs simultaneously
      • chown owner file
    • Can modify the user to which the directory and all files in the directory belong
      • chown -R owner directory
  • chgrp: modify the group to which the file belongs, generally can be replaced by chown
  • View file permissions
    • ll = ls -lh
      • Can view the specific command used by ll through which ll or alias ll
    • l = ls -lah
    • Three times in the system: modification time mtime, read time atime, permission modification time ctime

Users#

  • Use root with caution
  • su username: switch to another user, need to enter their password
  • su - username
    • Using "-", will completely update environment variables, suitable for complex operations
    • Not adding "-", will update environment variables appropriately, suitable for temporary user switching
  • Not adding a username defaults to switching to root

Additional Knowledge Points#

  • . Current directory .. Parent directory / Root directory
  • Zombie process [harmful]: the child process dies, the parent process does not manage it, the child process still occupies resources; orphan process [harmless]: will be adopted by process 1
  • Chatting with other terminals
    • Enter w to view online terminals
      • Image
      • TTY [Teletype]: terminal; pts [pseudo terminal slave]: virtual terminal
    • Enter echo "hello" >> /dev/pts/1 to send "hello" to terminal pts/1
      • Image
      • pts/1 can receive the message and echo back in the same way
    • You can also use wall "System is rebooting in 15s" to inform all online users that you are about to reboot
  • sudo !! can prepare to execute the last command as a superuser
    • Image
    • Can prepare to execute the last command as a superuser without needing to enter the command again
  • whereis
    • Image
    • View the installation directory of the software
  • file file
    • Image
    • View specific information about the file
  • Usually, using [man command] or [command -h] can view command help
    • Format rules for usage instructions: [] optional options, | parameters that cannot be used simultaneously
  • Directories are also files
  • alias alias=command, can set an alias corresponding to the command

Tips#

  • User Related

    • When creating a user without specifying a special group, a group with the same name as the username will be created
    • A user can belong to multiple different groups
  • Excellent resource monitoring software

    • Htop: a monitoring and process management software running on Linux, friendlier than top
    • nmon: a computer performance system monitoring tool for Linux [developed by IBM]
    • dstat: a flexible resource statistics tool
  • Command Related

    • There is no difference between apt and apt-get
    • history: prints the command history output
    • tldr + command: view command format
    • echo $PATH: prints the PATH environment variable, which stores the paths of the system's default executable files
  • CPU status information us, sy, ni, id, wa, hi, si, st meanings-CSDN

  • How to change hostname on Ubuntu-blog, about hostnamectl

  • Recommended for learning computer networks: “Computer Networking: A Top-Down Approach”, focus on the first 5 chapters


Course Notes#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.