Linux usage operations[1]

Copyright Notice

  • The content of this blog is based on my personal study notes from the Dark Horse Programmer course. I hereby declare that all copyrights belong to Dark Horse Programmers or related rights holders. The purpose of this blog is only for personal learning and communication, not commercial use.
  • I try my best to ensure accuracy when organizing my study notes, but I cannot guarantee the completeness and timeliness of the content. The content of this blog may become outdated over time or require updating.
  • If you are a Dark Horse programmer or a related rights holder, if there is any copyright infringement, please contact me in time and I will delete it immediately or make necessary modifications.
  • For other readers, please abide by relevant laws, regulations and ethical principles when reading the content of this blog, refer to it with caution, and bear the resulting risks and responsibilities at your own risk. Some of the views and opinions in this blog are my own and do not represent the position of Dark Horse Programmers.

shortcut key

ctrl + cForced stop

  • To run certain programs on Linux, you can use the shortcut key ctrl + c to force a stop.
  • If the command is entered incorrectly, you can also use the shortcut key ctrl + c to exit the current input and re-enter.

ctrl + d Exit, logout

  • You can use the shortcut key: ctrl + d to log out of the account, or log out of the exclusive pages of some specific programs.
  • Cannot be used to exit vi/vim

historyOrder

  • You can use the history command to view the commands entered in history.
  • You can pass:! command prefix to automatically execute the last command matching the prefix.
  • You can use the shortcut keys: ctrl + r to enter content to match historical commands.
    If the searched content is what you need, then:
  • Enter key can be executed directly
  • You can get this command by pressing the left and right keys on the keyboard (not executed)

Cursor movement shortcut keys

shortcut key effect
ctrl + a Jump to the beginning of the command
ctrl + e Jump to the end of the command
ctrl + left key of keyboard Jump one word to the left
ctrl + keyboard right click Jump one word to the right

Clear screen shortcut key

  • Use the shortcut key ctrl + l to clear the terminal contents, or use the clear command to get the same effect.

Software installation command

  • The Linux system supports these two methods: "App Store" in the Linux command line, and command to install software

Common linux system package managers

Package manager Supported distributions Command example Software repository type Packet format
APT (Debian/Free) Debian, Ubuntu, Linux Mint, etc sudo apt-get install <software> Debian software repository .deb
YUM (RHEL/CentOS) Red Hat Enterprise Linux, CentOS等 sudo yum install <software> RPM software repository .rpm
DNF (Fedora) Fedora sudo dnf install <software> RPM software repository .rpm
Zypper (openSUSE) openSUSE sudo zypper install <software> RPM software repository .rpm
Pacman (Arch Linux) Arch Linux sudo pacman -S <software> Arch software repository .pkg.tar.xz
Portage (Gentoo) Gentoo sudo emerge <software> Portage tree ebuild
Snap Most Linux distributions sudo snap install <software> Snap Store .snap
Flatpak Most Linux distributions flatpak install <software> Flathub and other Flatpak repositories .flatpak
AppImage Most Linux distributions Download and run directly Standalone self-contained application .AppImage

yumOrder

  • yum: RPM package software manager, used to automatically install and configure Linux software, and can automatically resolve dependency issues.

    yum [-y] [install | remove | search] 软件名称
    # -y,自动确认,无需手动确认安装或卸载过程
    
    • install: install
    • remove: uninstall
    • search: search
  • The yum command requires root permissions and an Internet connection. You can switch to root with su, or use sudo to elevate permissions.

aptOrder

  • CentOS systems and Ubuntu use different package managers. CentOS uses yum manager, Ubuntu uses apt manager
  • The usage is the same as yum, and also requires root permissions.
    apt [-y] [install | remove | search] <软件名称>
    
    • apt install wget, install wget
    • apt remove wget, remove wget
    • apt search wget, search wget
  • wget (web get) is a command line tool for downloading files and web pages in Unix-like operating systems. It was originally designed for fetching files from the Internet.
  • wget (web get) is a powerful and flexible tool widely used for automation tasks, system management and network operations. It is commonly preinstalled on Unix and Linux systems and is also available on other operating systems such as Windows (via Cygwin or WSL) and macOS.

systemctl command

  • systemctl is a command-line tool for managing systemd systems and services. systemd is an init system and service manager widely used in modern Linux distributions.
  • Through the systemctl command, you can view, start, stop, restart, disable and enable system services, and manage various run levels (targets) of the system.
  • Software that can be managed by systemctl is also generally called: service
systemctl start |stop | status | enable | disable <服务名>
#start 启动
#stop 关闭
#status 查看状态
#enable 开启开机自启
#disable 关闭开机自启

Here are some common systemctl commands and their usage:

  1. View all units (Units): List all units currently loaded in the system, including services, sockets, devices, mount points, etc.

       systemctl list-units
    
  2. View service status: Used to view the status of a specific service, including whether it is running, last startup time, active processes, etc.

       systemctl status <服务名>
    
  3. Start service: used to start the specified service

       systemctl start <服务名>
    
  4. Stop service: Used to stop the specified service

       systemctl stop <服务名>
    
  5. Restart service: Used to restart the specified service

       systemctl restart <服务名>
    
  6. Enable service: used to automatically start the specified service when the system starts

       systemctl enable <服务名>
    
  7. Disable service: used to disable specified services when the system starts

       systemctl disable <服务名>
    
  8. View dependencies of a service: used to view the dependencies of a specified service, including other dependent services and units

       systemctl list-dependencies <服务名>
    
  9. Switch run level (target): used to switch to the specified run level (usually called "target")

       systemctl isolate <运行级别>
    
  10. View the system's default run level: used to view the system's current default run level

    systemctl get-default
    
  11. Set the default run level of the system: used to set the default run level of the system

    systemctl set-default <运行级别>
    

Soft connection

  • ln is a commonly used command line tool for creating links, which allows you to create links to files or directories in the file system so that the same files or directories can be referenced without copying the actual data.

  • The ln command has two common link types: hard links and soft links (symbolic links, also called symbolic links) .

  • The ln command is useful for managing links in a file system, saving disk space and making the organization of files and directories more flexible**. Hard links are mostly used for files, while symbolic links are often used for files and directories as they allow references to targets in different file systems. **

  • General syntax of the ln command:

    ln [选项] <源文件/目录> <链接名称>
    

    -s: used to create symbolic links (soft links). If this option is not specified, a hard link is created by default.


  • Common usage and examples:
  1. Create a file hard link:

       ln file1 file2
    
    • Create a hard link named file2 in the current directory, which will have the same contents and inode as file1. Modifying one of the files affects the other because they reference the same data blocks.
  2. Create a hard link to the directory:

       ln /path/to/source/directory /path/to/link/directory
    
    • Create a hard link to a directory so that link/directory and source/directory have the same contents. Note that hard links do not recursively link files within a directory, only the directory itself.
  3. Create a symbolic link:

       ln -s /path/to/source/file /path/to/symlink
    
    • Create a symbolic link named symlink pointing to the source file file.
    • A soft link is a reference to a source file or directory that can span file systems or link to a non-existent target.
  4. View the number of links to a file or directory:

       ls -l <文件/目录>
    
    • The number of hard links is greater than or equal to 2 because at least one hard link points to the actual data block.
  5. Remove link:

       rm <链接文件/目录>
    
    • Using the rm command to remove links does not affect the source files or directories themselves unless the last link is removed.

Date & time zone

  • date is a common command line tool used to display and set the system date and time. It is widely used in Unix-like operating systems, including Linux and macOS.

  • show system time command

    date [-d] [+格式化字符串]
    # 格式化字符串:通过特定的字符串标记,来控制显示的日期格式
    %Y   年
    %y   年份后两位数字 (00..99)
    %m   月份 (01..12)
    %d   日 (01..31)
    %H   小时 (00..23)
    %M   分钟 (00..59)
    %S   秒 (00..60)
    %s   自 1970-01-01 00:00:00 UTC 到现在的秒数
    
  • The date command has various options:

  1. Display current date and time: By default, the date command displays the current date and time, the format may vary depending on the system

       date
    
  2. Custom date and time format: The example will be displayed in the format of "Year-Month-Day Hour:Minute:Second"

       date +"%Y-%m-%d %H:%M:%S"
    
  3. Set the system date and time: This command usually needs to be run with administrator privileges

       sudo date -s "2023-09-26 12:34:56"
    
  4. Calculate dates and times: Use the -d option and a descriptive string (such as "2 days ago") to calculate relative dates and times

       date -d "2 days ago"
    
  5. Display UTC time: Use the -u option to display the date and time in Coordinated Universal Time (UTC).

       date -u
    
  6. Display the time in a certain time zone: By setting the TZ environment variable, you display the date and time in a specific time zone

       TZ="America/New_York" date
    
  7. Display timestamp (Unix time):

       date +%s
    
  8. Change the system hardware clock:

    # hwclock 命令用于读取和更改系统的硬件时钟,--systohc 选项用于将系统时间同步到硬件时钟
    sudo hwclock --systohc
    

Modify linux time zone

  • Use root privileges and execute the following command to change the time zone to the East Eighth District time zone.
    rm -f /etc/localtime
    sudo ln -s /usr/share/zoneinfo/Asis/Shanghai /etc/localtime
    
  • Delete the localtime file that comes with the system and link the /usr/share/zoneinfo/Asia/Shanghai file into a localtime file

ntp program

  • Automatically calibrate system time through ntp program
    # 安装ntp:
    yum -y install ntp
    # 启动并设置开机自启:
    systemctl start ntpd
    systemctl enable ntpd
    # 也可以手动校准(需root权限):ntpdate -u ntp.aliyun.com
    ntpdate -u ntp.aliyun.com
    

IP address & host name

ip&hostname

  • Every computer connected to the Internet will have an address that is used to communicate with other computers.
  • There are two main versions of IP addresses, V4 version and V6 version.
  • The address format of the IPv4 version is: abcd, where abcd represents a number from 0 to 255. For example, 192.168.88.101 is a standard IP address.
  • You can use the command: ifconfigto check the IP address of the machine. If you cannot use the ifconfig command, you can install:yum -y install net-tools

  • 127.0.0.1: This IP address is used to refer to this machine
  • 0.0.0.0 special IP address
    • Can be used to refer to the local machine
    • Can be used in port binding to determine binding relationships
    • In some IP address restrictions, it means all IPs. For example, if the release rule is set to 0.0.0.0, it means that any IP is allowed to access.

  • In addition to the external contact address (IP address), each computer can also have a name, which is called a host name. For Windows or Linux systems, you can set the host name for the system.
  • View hostnamehostname
  • Set hostnamehostnamectl set-hostname <主机名>

DNS

  • A domain name is a human-readable name used to identify and locate specific resources on the Internet. Domain names are often used in place of IP addresses.

  • Domain name resolution is the process of converting domain names into corresponding IP addresses or other network resources.

  • A domain name consists of a series of tags separated by dots. For example, baidu.com, where .comis the top-level domain name (Top-Level Domain, TLD) and baiduis the second-level domain name (Second-Level Domain, SLD).

  • The main components of domain name resolution include:

    • Domain Name Server (DNS Server): These are special servers that store the mapping between domain names and IP addresses. Each domain name is associated with one or more name servers.
    • Domain Name Resolver (DNS Resolver): This is software on the user's computer or network device that converts domain names provided by the user into IP addresses. Most operating systems and network devices include built-in DNS resolvers.

Domain name resolution process analysis:

  • Chart elements:
    1. User device: Sends a query to the local DNS resolver.
    2. Local DNS resolver: resolves domain names by querying root domain name servers, TLD DNS servers, and authoritative DNS servers.
    3. DNS server hierarchy: Represents the interaction between root domain name servers, TLD DNS servers, and authoritative DNS servers.
    4. Target server: Represents the web server hosting the website, which ultimately serves the web page to the user.
目标服务器
DNS服务器层级
用户设备
查询
查询
返回根域名服务器IP
查询
返回TLD DNS服务器IP
查询
返回目标域名的IP
提供网页
Web服务器
根域名服务器
TLD DNS服务器
权威DNS服务器
本地DNS解析器
用户输入域名

  • Specific process text analysis:
  1. The user enters a domain name into a web browser or other application, for example www.baidu.com.
  2. The application sends the domain name to the local DNS resolver.
  3. The local DNS resolver first checks its local cache to see if the domain name has already been resolved. If cached, it returns the corresponding IP address.
  4. If there is no relevant information in the local cache, the resolver will send a query request to the root domain name servers (Root DNS Servers) to ask for the address of the top-level domain name server (TLD DNS Servers), such as the .comDNS server of .
  5. The TLD DNS server will return the address of the authoritative DNS server for the .com domain.
  6. The local resolver continues to send query requests to the authoritative DNS server to obtain baidu.comthe IP address.
  7. Once the local resolver obtains the IP address, it stores the address in the local cache and returns it to the application.
  8. Applications can use the IP address to establish www.baidu.coma connection to and request the corresponding web page or resource.

win configure hostname mapping

  • C:\Windows\System32\drivers\etc\hostsAfter configuring records in the Windows system: file, you can use terminal tools (such as FinalShell) to connect to the Linux server through the domain name (host name).
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

Virtual machine configuration fixed IP

  • 虚拟机的Linux操作系统,其IP地址是通过DHCP服务获取的。
  • DHCP:动态获取IP地址,即每次重启设备后都会获取一次,可能导致IP地址频繁变更
  • 原因:我们要远程连接到Linux系统,如果IP地址经常变化,就要频繁修改适配很麻烦

第一步:在VM中配置IP地址网关和网段

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第二步:在Linux中固定IP

  • 使用vim编辑/etc/sysconfig/network-scripts/ifcfg-ens33文件,填入如下内容
    在这里插入图片描述
  • 执行:systemctl restart network 重启网卡
  • 执行ifconfig查看ip地址

Guess you like

Origin blog.csdn.net/yang2330648064/article/details/133294991