Linux


1.Linux简介及安装

  1.1Linux简介

        Linux 内核最初只是由芬兰人林纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的,

      是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 UNIX 的多用户、多任务、支持多线程和

      多 CPU 的操作系统,Linux 能运行主要的 UNIX 工具软件、应用程序和网络协议。它支持 32 位和 64 位硬件。

      Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

        Linux 系统是?款操作系统,不同于?众消费领域的 Windows 系统和苹果系统,Linux 系统主要是应?于服务器

      和?型计算机领域。我们?活中的?部分的??和 APP,其实都是基于 Linux 系统运?的,这其中就包括我们经常会

      使?的淘宝和微信。Linux 系统的开发模式是开源,?这也正是它的核?特?。开源代表该系统的源代码能被?由获取

      、使?和修改。所以 Linux 系统和其他商业软件最?的区别就是它拥有更多的?由性。

  1.2Linux安装

        Linux官网镜像下载地址:https://www.centos.org/download/

      Linux安装步骤比较繁琐,现在的云服务器挺普遍的,价格也便宜,如果自己不想搭建,也可以直接买一个服务器使用,

      如果需要自己在本地搭建Linux环境,请自行在百度搜索方法,这里演示的是在服务器上运行Linux。

  1.3Linux云服务登录

      • 检查本地ssh

        C:\>ssh
        usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
                   [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
                   [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
                   [-i identity_file] [-J [user@]host[:port]] [-L address]
                   [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
                   [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
                   [-w local_tun[:remote_tun]] destination [command]
      • 在服务器创建账户密码,然后客户端通过ssh的服务连接到阿?云的,ssh服务的端?是22

      • 登录云服务器启动Linux

        ssh 用户名@服务器地址
        C:\>ssh wq@1*1.**.***.**
        wq@1*1.**.***.**'s password:********
        Activate the web console with: systemctl enable --now cockpit.socket
        
        Last login: Mon Feb 21 16:09:09 2022
        [wq@centos-master ~]$

2.Linux常见问题

  • Linux长时间无操作强制退出  

      我们使用Linux系统的时候,会发现我们如果长时间不操作系统,再去操作的时候会被强制退出,对于这个问题,我们在不操作系统的时候输入“top”按下回车即可解决,下次我们重新操作系统的时候直接按Q退即可
    [wq@centos-master ~]$ top
    top - 17:15:58 up 36 days, 21:30, 31 users,  load average: 0.03, 0.04, 0.03
    Tasks: 266 total,   1 running, 256 sleeping,   9 stopped,   0 zombie
    %Cpu(s):  0.8 us,  1.0 sy,  0.0 ni, 97.8 id,  0.2 wa,  0.2 hi,  0.0 si,  0.0 st
    MiB Mem :   3736.5 total,    179.6 free,    920.2 used,   2636.7 buff/cache
    MiB Swap:      0.0 total,      0.0 free,      0.0 used.   2536.7 avail Mem
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       1248 root      20   0 1875736  78376  20500 S   0.7   2.0 505:44.24 kubelet
    3849011 root      20   0 2195772 101076   7732 S   0.7   2.6  89:12.22 beam.smp
         11 root      20   0       0      0      0 I   0.3   0.0  17:26.67 rcu_sched
       1057 root      20   0 1500208  34364   4952 S   0.3   0.9  45:04.27 containerd
       1249 root      20   0  736064  39068  37208 S   0.3   1.0  84:34.92 rsyslogd
       1253 root      20   0   24616   6516   3724 S   0.3   0.2  54:49.12 tat_agent
       4394 root      20   0  755060  26660   5616 S   0.3   0.7 108:56.27 kube-scheduler
    1287215 wq        20   0   69540   5704   4704 S   0.3   0.1   0:02.13 top
  • Linux的超级管理员是root

3.Linux目录结构

  • /bin:

    bin 是 Binaries (二进制文件) 的缩写, 这个目录存放着最经常使用的命令。
  • /boot:

    这里存放的是启动 Linux 时使用的一些核心文件,包括一些连接文件以及镜像文件。
  • /dev :

    dev 是 Device(设备) 的缩写, 该目录下存放的是 Linux 的外部设备,在 Linux 中访问设备的方式和访问文件的方式是相同的。
  • /etc:

    etc 是 Etcetera(等等) 的缩写,这个目录用来存放所有的系统管理所需要的配置文件和子目录。
  • /home:

    用户的主目录,在 Linux 中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的,如上图中的 alice、bob 和 eve。
  • /lib:

    lib 是 Library(库) 的缩写这个目录里存放着系统最基本的动态连接共享库,其作用类似于 Windows 里的 DLL 文件。几乎所有的应用程序都需要用到这些共享库。
  • /lost+found

    这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件。
  • /media

    linux 系统会自动识别一些设备,例如U盘、光驱等等,当识别后,Linux 会把识别的设备挂载到这个目录下。
  • /mnt

    系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在 /mnt/ 上,然后进入该目录就可以查看光驱里的内容了。
  • /opt

    opt 是 optional(可选) 的缩写,这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。
  • /proc

    proc 是 Processes(进程) 的缩写,/proc 是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,这个目录是一
    个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。 这个目录的内容不在硬盘上而是在内存里,我们也可以直接修改里面的某些文件
  • /root

    该目录为系统管理员,也称作超级权限者的用户主目录。
  • /sbin

    s 就是 Super User 的意思,是 Superuser Binaries (超级用户的二进制文件) 的缩写,这里存放的是系统管理员使用的系统管理程序。
  • /selinux

    这个目录是 Redhat/CentOS 所特有的目录,Selinux 是一个安全机制,类似于 windows 的防火墙,但是这套机制比较复杂,这个目录就是存放selinux相关的文件的。
  • /srv

    该目录存放一些服务启动之后需要提取的数据。
  • /sys

    这是 Linux2.6 内核的一个很大的变化。该目录下安装了 2.6 内核中新出现的一个文件系统 sysfs 。
    sysfs 文件系统集成了下面3种文件系统的信息:针对进程信息的 proc 文件系统、针对设备的 devfs 文件系统以及针对伪终端的 devpts 文件系统。
    该文件系统是内核设备树的一个直观反映。
    当一个内核对象被创建的时候,对应的文件和目录也在内核对象子系统中被创建。
  • /tmp

    tmp 是 temporary(临时) 的缩写这个目录是用来存放一些临时文件的。
  • /usr

     usr 是 unix shared resources(共享资源) 的缩写,这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下
    类似于 windows 下的 program files 目录。
  • /usr/bin:

    系统用户使用的应用程序。
  • /usr/sbin:

    超级用户使用的比较高级的管理程序和系统守护程序。
  • /usr/src:

    内核源代码默认的放置目录。
  • /var

    var 是 variable(变量) 的缩写,这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。
  • /run

    是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。如果你的系统上有 /var/run 目录,应该让它指向 run。

4.Linux常见命令

  • ls 查看

    • ls -a 列出目录下的所有?件,包含.开头的隐藏?件

      [wq@centos-master ~]$ ls -a
      .   asd.py         .bash_logout   .bashrc  index.txt  .python_history
      ..  .bash_history  .bash_profile  .config  python     .viminfo
    • ls -l 列出文件的详细信息

      [wq@centos-master ~]$ ls -l
      total 12
      -rw-rw-r-- 1 wq wq   22 Feb 21 16:12 asd.py
      -rw-rw-r-- 1 wq wq   12 Feb 21 16:06 index.txt
      drwxrwxr-x 3 wq wq 4096 Feb 21 16:18 python
    • ls -i 查看?件的iNode号(iNode会指向?件块所在的位置)

      [wq@centos-master ~]$ ls -i
      2359404 asd.py  2359481 index.txt  2359391 python
    • ls -h 以容易读取的方式显示文件大小

    • ls -d 仅列出目录本身

      [wq@centos-master ~]$ ls -d
      .
    • ls / 查看Linux根目录

      [wq@centos-master ~]$ ls /
      bin   data  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
      boot  dev   home  lib64  media       opt  root  sbin  sys  usr
    • ls常用命令

      • ls -lh:查看?件的详细信息(包含了?件??)

        [wq@centos-master ~]$ ls -lh
        total 12K
        -rw-rw-r-- 1 wq wq   22 Feb 21 16:12 asd.py
        -rw-rw-r-- 1 wq wq   12 Feb 21 16:06 index.txt
        drwxrwxr-x 3 wq wq 4.0K Feb 21 16:18 python
      • ls -la:查看所有?件(包含了隐藏?件)

        [wq@centos-master ~]$ ls -la
        total 56
        drwx------   4 wq   wq   4096 Feb 21 16:26 .
        drwxr-xr-x. 14 root root 4096 Feb 21 11:08 ..
        -rw-rw-r--   1 wq   wq     22 Feb 21 16:12 asd.py
        -rw-------   1 wq   wq   7666 Feb 21 18:34 .bash_history
        -rw-r--r--   1 wq   wq     18 Jul 27  2021 .bash_logout
        -rw-r--r--   1 wq   wq    141 Jul 27  2021 .bash_profile
        -rw-r--r--   1 wq   wq    376 Jul 27  2021 .bashrc
        drwx------   3 wq   wq   4096 Feb 21 11:22 .config
        -rw-rw-r--   1 wq   wq     12 Feb 21 16:06 index.txt
        drwxrwxr-x   3 wq   wq   4096 Feb 21 16:18 python
        -rw-------   1 wq   wq     20 Feb 21 16:11 .python_history
        -rw-------   1 wq   wq   7392 Feb 21 16:25 .viminfo
      • ls -lah:查看所有?件以及?件大小

        [wq@centos-master ~]$ ls -lah
        total 56K
        drwx------   4 wq   wq   4.0K Feb 21 16:26 .
        drwxr-xr-x. 14 root root 4.0K Feb 21 11:08 ..
        -rw-rw-r--   1 wq   wq     22 Feb 21 16:12 asd.py
        -rw-------   1 wq   wq   7.6K Feb 21 18:35 .bash_history
        -rw-r--r--   1 wq   wq     18 Jul 27  2021 .bash_logout
        -rw-r--r--   1 wq   wq    141 Jul 27  2021 .bash_profile
        -rw-r--r--   1 wq   wq    376 Jul 27  2021 .bashrc
        drwx------   3 wq   wq   4.0K Feb 21 11:22 .config
        -rw-rw-r--   1 wq   wq     12 Feb 21 16:06 index.txt
        drwxrwxr-x   3 wq   wq   4.0K Feb 21 16:18 python
        -rw-------   1 wq   wq     20 Feb 21 16:11 .python_history
        -rw-------   1 wq   wq   7.3K Feb 21 16:25 .viminfo
  • alias 别名

    • alias:查看系统存在的别名

      [wq@centos-master ~]$ alias
      alias egrep='egrep --color=auto'
      alias fgrep='fgrep --color=auto'
      alias grep='grep --color=auto'
      alias l.='ls -d .* --color=auto'
      alias ll='ls -l --color=auto'
      alias ls='ls --color=auto'
      alias vi='vim'
      alias xzegrep='xzegrep --color=auto'
      alias xzfgrep='xzfgrep --color=auto'
      alias xzgrep='xzgrep --color=auto'
      alias zegrep='zegrep --color=auto'
      alias zfgrep='zfgrep --color=auto'
      alias zgrep='zgrep --color=auto'
    • 定义别名:alisa 别名="系统已存在的变量"

      [wq@centos-master ~]$ alias a="ls -lh"
      [wq@centos-master ~]$ alias
      alias a='ls -lh'
      alias egrep='egrep --color=auto'
      alias fgrep='fgrep --color=auto'
      alias grep='grep --color=auto'
      alias l.='ls -d .* --color=auto'
      alias ll='ls -l --color=auto'
      alias ls='ls --color=auto'
      alias vi='vim'
      alias xzegrep='xzegrep --color=auto'
      alias xzfgrep='xzfgrep --color=auto'
      alias xzgrep='xzgrep --color=auto'
      alias zegrep='zegrep --color=auto'
      alias zfgrep='zfgrep --color=auto'
      alias zgrep='zgrep --color=auto'
    •  取消别名:unalias 自定义的别名

      [wq@centos-master ~]$ unalias a
      [wq@centos-master ~]$ alias
      alias egrep='egrep --color=auto'
      alias fgrep='fgrep --color=auto'
      alias grep='grep --color=auto'
      alias l.='ls -d .* --color=auto'
      alias ll='ls -l --color=auto'
      alias ls='ls --color=auto'
      alias vi='vim'
      alias xzegrep='xzegrep --color=auto'
      alias xzfgrep='xzfgrep --color=auto'
      alias xzgrep='xzgrep --color=auto'
      alias zegrep='zegrep --color=auto'
      alias zfgrep='zfgrep --color=auto'
  • tree 树形目录结构

    通常情况下系统未安装该命令,需要管理员运行命令yum install -y tree安装 直接使?tree显示深度太多
    • tree:显示当前路径下的所有文件目录结构

      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── b
      │       └── c
      │           └── d
      │               └── e
      │                   └── f
      │                       └── g
      ├── asd.py
      ├── index.txt
      └── python
          └── code
              └── index.py
      
      9 directories, 3 files
    • tree -L 指定层次  目录名称

      [wq@centos-master ~]$ tree -L 4 /
      .
      ├── a
      │   └── b
      │       └── c
      │           └── d
      ├── asd.py
      ├── index.txt
      └── python
          └── code
              └── index.py
      
      6 directories, 3 files
  • mkdir:创建文件夹(目录)

    -p 创建多级?录(跨级创建) -v 查看?录创建的过程(创建?录可视化)
    [wq@centos-master ~]$ mkdir -vp a/b/c/d/e/f/g
    mkdir: created directory 'a'
    mkdir: created directory 'a/b'
    mkdir: created directory 'a/b/c'
    mkdir: created directory 'a/b/c/d'
    mkdir: created directory 'a/b/c/d/e'
    mkdir: created directory 'a/b/c/d/e/f'
    mkdir: created directory 'a/b/c/d/e/f/g'
    [wq@centos-master ~]$ tree -a
    .
    ├── a
    │   └── b
    │       └── c
    │           └── d
    │               └── e
    │                   └── f
    │                       └── g
    ├── asd.py
    ├── .bash_history
    ├── .bash_logout
    ├── .bash_profile
    ├── .bashrc
    ├── .config
    │   └── procps
    ├── index.txt
    ├── python
    │   └── code
    │       └── index.py
    ├── .python_history
    └── .viminfo
  • rmdir删除文件夹(目录)

    仅可以删除空??录(不可以删除包含内容的?录)如果删除的?录不是空的,就会出现问题,具体如下:
    [wq@centos-master ~]$ rmdir a
    rmdir: failed to remove 'a': Directory not empty
    [wq@centos-master ~]$ rmdir a/b/c/d/e/f/g
    [wq@centos-master ~]$ tree -a
    .
    ├── a
    │   └── b
    │       └── c
    │           └── d
    │               └── e
    │                   └── f
    ├── asd.py
    ├── .bash_history
    ├── .bash_logout
    ├── .bash_profile
    ├── .bashrc
    ├── .config
    │   └── procps
    ├── index.txt
    ├── python
    │   └── code
    │       └── index.py
    ├── .python_history
    └── .viminfo
  • pwd:查看当前目录

    [wq@centos-master ~]$ pwd
    /home/wq
  • cd:切换目录

    • cd 目录名称:切换到指定目录
      [wq@centos-master ~]$ pwd
      /home/wq
      [wq@centos-master ~]$ cd python
      [wq@centos-master python]$ pwd
      /home/wq/python
    • cd ..返回当前目录的上一级目录
      [wq@centos-master python]$ pwd
      /home/wq/python
      [wq@centos-master python]$ cd ..
      [wq@centos-master ~]$ pwd
      /home/wq
    • cd:返回到当前用户的根目录
      [wq@centos-master code]$ pwd
      /home/wq/python/code
      [wq@centos-master code]$ cd
      [wq@centos-master ~]$ pwd
      /home/wq
  • touch:创建文件

    [wq@centos-master ~]$ dir
    a  asd.py  index.txt  python
    [wq@centos-master ~]$ touch hello.txt
    [wq@centos-master ~]$ dir
    a  asd.py  hello.txt  index.txt  python
  • rm:删除文件或目录

    • 删除文件

      • rm -f:强制删除文件

        [wq@centos-master ~]$ rm -f hello.txt
      • rm -i:询问确认后删除文件

        [wq@centos-master ~]$ rm -i index.txt
        rm: remove regular file 'index.txt'? 
      • rm -v:删除文件(可视化的只展示)

        [wq@centos-master ~]$ rm -v index.txt
        removed 'index.txt'
      • rm -iv:询问确认后删除文件+可视化展示

        [wq@centos-master ~]$ rm -iv index.txt
        rm: remove regular empty file 'index.txt'? y
        removed 'index.txt'
    • 删除文件夹(目录)

      • rm -r ?录名(删除时会询问是否删除)

        [wq@centos-master e]$ rm -r f
      • rm -rf ?录名(强制删除,若目录不存在,此命令依旧可以执?,不报错)

        [wq@centos-master ~]$ rm -rf a
      • rm -rv ?录名(可视化强制) 删除目录和?件时,先删除文件在删除目录

        mkdir: created directory 'a/b/c/d'
        [wq@centos-master ~]$ rm -rv a
        removed directory 'a/b/c/d'
        removed directory 'a/b/c'
        removed directory 'a/b'
        removed directory 'a'
  • cp:复制+修改名称

    • 复制文件:原文件不变,只对目标文件进行更改

      [wq@centos-master ~]$ tree
      .
      ├── a
      ├── b
      └── index.txt
      
      2 directories, 1 file
      [wq@centos-master ~]$ cp index.txt a/index.py
      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── index.py
      ├── b
      └── index.txt
      
      2 directories, 2 files
    • 复制目录(文件夹):把一个文件从一个目录复制到另一个目录下

      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── index.py
      ├── b
      └── index.txt
      
      2 directories, 2 files
      [wq@centos-master ~]$ cp a/index.py b/index.sh
      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── index.py
      ├── b
      │   └── index.sh
      └── index.txt
      
      2 directories, 3 files
  •  mv:剪贴/移动文件

    • 文件移动到文件夹下

      [wq@centos-master ~]$ tree
      .
      ├── a
      ├── b
      └── index.txt
      
      2 directories, 1 file
      [wq@centos-master ~]$ mv index.txt a/
      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── index.txt
      └── b
      
      2 directories, 1 file
    •  目录下的文件移到到另一个目录下

      [wq@centos-master ~]$ tree
      .
      ├── a
      │   └── index.txt
      └── b
      
      2 directories, 1 file
      [wq@centos-master ~]$ mv a/index.txt b/
      [wq@centos-master ~]$ tree
      .
      ├── a
      └── b
          └── index.txt
      
      2 directories, 1 file
    •  重命名

      [wq@centos-master b]$ tree
      .
      └── index.txt
      
      0 directories, 1 file
      [wq@centos-master b]$ mv index.txt index.sh
      [wq@centos-master b]$ tree
      .
      └── index.sh
      
      0 directories, 1 file
  •  cat:查看文件内容

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author:特昂糖
    
    age=21
    name="特昂糖"
    salary=9899.99
    isboy=True
    birthday="2000-02-03"
    myName="我的名字"
    
    #prints输出变量值
    print(myName,age,salary,isboy,birthday,myName)
    
    #type()查看变量age的类型
    print(type(age))
    #查看变量name的类型
    print(type(name))
    #查看变量salary的类型
    print(type(salary))
    #查看变量isboy的数据类型
    print(type(isboy))
    #查看变量birthday的数据类型
    print(type(birthday))
    •  cat -n  文件名称 :在终端输出文件内容并显示行数

      [wq@centos-master a]$ cat -n type.py
           1  #! /usr/bin/env python
           2  # -*- coding:utf-8 -*-
           3  # author:特昂糖
           4
           5  age=21
           6  name="特昂糖"
           7  salary=9899.99
           8  isboy=True
           9  birthday="2000-02-03"
          10  myName="我的名字"
          11
          12  #prints输出变量值
          13  print(myName,age,salary,isboy,birthday,myName)
          14
          15  #type()查看变量age的类型
          16  print(type(age))
          17  #查看变量name的类型
          18  print(type(name))
          19  #查看变量salary的类型
          20  print(type(salary))
          21  #查看变量isboy的数据类型
          22  print(type(isboy))
          23  #查看变量birthday的数据类型
    •  cat 文件名:输出文件所有内容

      [wq@centos-master a]$ cat type.py
      #! /usr/bin/env python
      # -*- coding:utf-8 -*-
      # author:特昂糖
      
      age=21
      name="特昂糖"
      salary=9899.99
      isboy=True
      birthday="2000-02-03"
      myName="我的名字"
      
      #prints输出变量值
      print(myName,age,salary,isboy,birthday,myName)
      
      #type()查看变量age的类型
      print(type(age))
      #查看变量name的类型
      print(type(name))
      #查看变量salary的类型
      print(type(salary))
      #查看变量isboy的数据类型
      print(type(isboy))
      #查看变量birthday的数据类型
  •  tac:倒叙显示文件内容

    [wq@centos-master a]$ tac type.py
    print(type(birthday))#查看变量birthday的数据类型
    print(type(isboy))
    #查看变量isboy的数据类型
    print(type(salary))
    #查看变量salary的类型
    print(type(name))
    #查看变量name的类型
    print(type(age))
    #type()查看变量age的类型
    
    print(myName,age,salary,isboy,birthday,myName)
    #prints输出变量值
    
    myName="我的名字"
    birthday="2000-02-03"
    isboy=True
    salary=9899.99
    name="特昂糖"
    age=21
    
    # author:特昂糖
    # -*- coding:utf-8 -*-
    #! /usr/bin/env python
  •  more 文件名:一段一段显示文件内容

    [wq@centos-master a]$ more type.py
    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author:特昂糖
    
    age=21
    name="特昂糖"
    salary=9899.99
    isboy=True
    birthday="2000-02-03"
    myName="我的名字"
    
    #prints输出变量值
    print(myName,age,salary,isboy,birthday,myName)
    
    #type()查看变量age的类型
    print(type(age))
    #查看变量name的类型
    --More--(63%)
  • less:上下左右查看文件内容

  • head -n N(N为数字,查看几行输几):查看文件开头几行

    [wq@centos-master a]$ head -n 5 type.py
    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author:特昂糖
    
    age=21
  •  tail -n N(N为数字,查看几行输几):查看文件结尾几行

    [wq@centos-master a]$ tail -n 3 type.py
    print(type(isboy))
    #查看变量birthday的数据类型
  • tail -f 文件名:查看实时文件