Linux 基础学习1

Linux 基础学习

用户登录

  • root用户
    • 是一个特殊的管理账号,也可以成为超级管理员
    • root用户对系统有完全控制的权限
    • 对系统的损害会无限大
    • 在工作中,如果没有特殊的必要,尽量不要使用root
  • 普通用户
    • 权限有限
    • 对系统的损害会小

终端

  • 分类

    • 设备终端
    • 物理终端
    • 虚拟终端 ctrl+alt+f[1-6] /dev/tty#
    • 图形终端 /dev/tty7
    • 串行终端
    • 伪终端 通过ssh远程连接的 /dev/pts/#
    • 查看终端的命令 tty
    [root@localhost ~]# echo $SHELL
    /bin/bash
    
    • 查看ip地址的命令 ip addr (ip a)
    [root@localhost ~]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:ec:6b:40 brd ff:ff:ff:ff:ff:ff
        inet 192.168.234.129/24 brd 192.168.234.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::bce1:f5a5:d3b0:55a8/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    [root@localhost ~]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:ec:6b:40 brd ff:ff:ff:ff:ff:ff
        inet 192.168.234.129/24 brd 192.168.234.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::bce1:f5a5:d3b0:55a8/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    

交互式接口

  • 启动终端以后,在终端设备上附加的一个应用程序
    • GUI(桌面)
    • CLI command line (命令行)
      • powershell (win10)
      • sh
      • bash(linux,mac默认的程序)
      • zsh
      • csh
      • tcsh

bash

bash是linux系统的用户界面,提供了用户和操作系统之间的交互,它接收用户的输入,让它送给操作系统执行(命令行操作)

  • 目前是linux和mac上默认的 shell(一种命令语言)
  • centos默认使用
  • 显示系统当前使用的shell echo $SHELL
[root@localhost ~]# echo $SHELL
/bin/bash
  • 查看系统内可以使用的shell cat /etc/shells
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
  • ctrl +d 快速终止当前的连接
  • 切换shell chsh -s /bin/shell(shell可修改如 :chsh -s /bin/zsh)

修改ssh连接慢的步骤

echo "UseDNS no" >> /etc/ssh/sshd_config
systemctl restart sshd

命令提示符

[root@localhost ~]#  
管理员是#   (命令提示符)
普通用户时$

显示提示符格式

[root@localhost ~]# echo $PS1
[\u@\h \W]\$
\u 代表当前登录的用户
\h 代表当前主机的主机名
\W 代表当前的目录
0表示默认字体,1表示加粗,4在字体下方加下划线 5 闪烁 7 代表突出显示
31-37 字体颜色
40-47 表示背景颜色
echo 'PS1="\[\e[1;35mm\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/ps.sh #永久生效

命令

执行命令: 输入命令回车

内部命令:shell 自带的命令

  • help 显示所有的内部命令

外部命令:第三方提供的命令

查看命令的类型: type

[root@localhost ~]#type echo
echo is a shell builtin
[root@localhost ~]#type top
top is /usr/bin/top

别名

  • 查看当前所有的别名 alias
[root@localhost ~]# alias
alias cp='cp -i'
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 mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  • 自定别名 alias cdetc='cd /etc'
  • 取消别名 unalias cdetc
  • 设置别名只对当前的终端有效
  • 设置所有用户都可以用 /etc/bashrc
  • 只对当前用户有效 ~/.bashrc
  • 执行本身命令
    • \command
    • "command"
    • 'command'
    • path

命令格式

command [options.....] [args...]

command 命令本身

options:启动或者关闭命令里面的某些功能

  • 长选项:--help --color
  • 短选项: -i -l

args:命令的作用体,一般情况下是目录或者文件,用户名等等

注意:

  • 短选项是可以合并
  • 空格隔开
  • ctrl+c 结束命令的执行
  • 在同一行执行多个命令用;隔开
  • 一个命令可以在多行显示用\连接

获取命令的帮助信息

内部命令:

  • help command
[root@localhost ~]# help command
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.
    
    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.
    
    Options:
      -p        use a default value for PATH that is guaranteed to find all of
        the standard utilities
      -v        print a description of COMMAND similar to the `type' builtin
      -V        print a more verbose description of each COMMAND
    
    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.
  • man bash 执行这条命令,会得到所有内建命令列表及使用方法
Linux系统中的命令可分为内部命令和外部命令。内部命令,又称为内建命令(builtin)。怎么区分内部命令和外部命令了? 输入man bash命令,就可查看所有的内部命令
BASH(1)                         General Commands Manual                        BASH(1)

NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [file]

COPYRIGHT
       Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.

DESCRIPTION
       Bash  is  an  sh-compatible command language interpreter that executes commands
       read from the standard input or from a file.   Bash  also  incorporates  useful
       features from the Korn and C shells (ksh and csh).

       Bash  is  intended to be a conformant implementation of the Shell and Utilities
       portion of the IEEE POSIX specification (IEEE Standard 1003.1).   Bash  can  be
       configured to be POSIX-conformant by default.

OPTIONS
       All of the  single-character shell options documented in the description of the
       set builtin command can be used as options when the shell is invoked.  In addi‐
       tion, bash interprets the following options when it is invoked:

       -c string If  the -c option is present, then commands are read from string.  If
                 there are arguments after the string, they are assigned to the  posi‐
                 tional parameters, starting with $0.
       -i        If the -i option is present, the shell is interactive.
       -l        Make bash act as if it had been invoked as a login shell (see INVOCA‐
                 TION below).
       -r        If the -r option  is  present,  the  shell  becomes  restricted  (see Manual pape bash(1) line 1 (press h for help or q to quiit)

外部命令:

  • command -h
  • command --help
  • man coomand
  • 官方文档
[root@localhost ~]#python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
[] 可选项
<> 表示变化的数据
... 表示列表
a|b|c 或者
-abc 表示-a -b -c
{} 表示分组

man

 1   Executable programs or shell commands  #用户命令
 2   System calls (functions provided by the kernel) # 系统调用
 3   Library calls (functions within program libraries) # 库的调用
 4   Special files (usually found in /dev) #设备文件与特殊文件
 5   File formats and conventions eg /etc/passwd # 配置文件格式
 6   Games #游戏
 7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) # 杂项
 8   System administration commands (usually only for root) # 管理类的命令
 9   Kernel routines [Non standard] # 内核的API
 退出q
 翻屏 空格
 翻行 回车
 man 章节 passwd

bash 快捷键

  • ctrl+l 清屏 相当于clear
  • ctrl+o 执行当前的命令,并显示当前的命令
  • ctrl+s 锁屏
  • ctrl+q 解锁
  • ctrl+c 终止命令
  • ctrl+z 挂起命令
  • ctrl+a 光标移动到行首,相当于Home
  • ctrl+e 光标移动到行位,相当于End
  • ctrl+xx 在开头和当前光标所在位置跳转
  • ctrl+k 删除光标后的文字
  • ctrl+u 删除光标前的文字
  • alt+r 删除正行

tab 键

  • 命令补全
    • 内部命令
    • 外部命令:根据环境变量定义的路径,从前往后依次查找,自动匹配第一个查找到的内容
    • 如果用户给的命令只有唯一一个匹配,则直接补全
    • 如果有多个匹配,则需要在按tab键将所有匹配到的结果展示出来
  • 目录补全
    • 把用户给定的字符作为文件的开头,如果有唯一一个匹配则直接补全
    • 如果有多个匹配,则需要再次按tab键把所有的匹配到的结果展示出来

引号

[root@localhost ~]#name=alexdsb
[root@localhost ~]#echo "$name"
alexdsb
[root@localhost ~]#echo '$name'
$name
[root@localhost ~]#echo "wo shi `tty`"
wo shi /dev/pts/2
[root@localhost ~]#tty
/dev/pts/2
[root@localhost ~]#echo "wo shi $(tty)"
wo shi /dev/pts/2

命令历史

  • 可以使用上下箭头来查找之前执行过的命令
  • 存放文件是~/.bash_history
  • 执行的命令是history
  • 执行上一条命令
    • 上箭头
    • !!
    • !-1
    • ctrl+p 回车
  • 调用上一条命令的最后一个值 esc .
  • !# 指定第多少条命令
  • !-# 指定倒数第#条命令
  • !string 用来最近一次匹配到的命令(从下往上)
  • ctrl+r 搜索命令
  • ctrl+g 取消搜索
  • # 显示最后#条命令

命令展开

touch file{1..20} #创建20个文件
seq 0 2 10
echo file{1..20..2}

echo 回显

echo -e 'dadasda\ndasdasd'
echo -e '\a' #播放声音

查看用户登录信息

[root@localhost ~]#whoami # 显示当前的登录用户
root
[root@localhost ~]#who am i #显示当前登录用户的详细信息
root     pts/2        2019-08-22 15:54 (192.168.21.1)
[root@localhost ~]#w 显示所有的用户并显示执行的命令
 16:27:54 up  5:19,  9 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty2                      11:37    4:48m  0.02s  0.02s -bash
wu       tty3                      11:39    4:48m  0.02s  0.02s -bash
root     :0       :0               11:30   ?xdm?   1:28   0.36s /usr/libexec/gnome-session-binary --session gnome-classi
root     pts/0    :0               11:35    4:43m  0.03s  0.03s bash
root     pts/1    192.168.21.1     12:11    4:03m  0.02s  0.02s -bash
root     pts/2    192.168.21.1     15:54    2.00s  0.18s  0.03s w
wu       pts/3    192.168.21.1     12:27    2:26   0.05s  0.05s -bash

date

[root@localhost ~]#date  显示当前的时间
Thu Aug 22 16:30:06 CST 2019 
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
[root@localhost ~]#date 010923102018 #修改时间
Tue Jan  9 23:10:00 CST 2018
[root@localhost ~]#date
Tue Jan  9 23:10:18 CST 2018
[root@localhost ~]#ntpdate time.windows.com #同步网络服务器时间
unix元年 1970-01-01
[root@localhost ~]#date
Thu Aug 22 16:35:44 CST 2019
[root@localhost ~]#date
Thu Aug 22 16:35:47 CST 2019
[root@localhost ~]#date
Thu Aug 22 16:35:48 CST 2019
[root@localhost ~]#date +%a
Thu
[root@localhost ~]#date +%A
Thursday
[root@localhost ~]#date +%F
2019-08-22
[root@localhost ~]#date +%H
16
[root@localhost ~]#date +%I
04
[root@localhost ~]#date +%m
08
[root@localhost ~]#date +%d
22
[root@localhost ~]#date +%M
38
[root@localhost ~]#date +%h
Aug
[root@localhost ~]#date +%c
Thu 22 Aug 2019 04:38:42 PM CST
[root@localhost ~]#date +%T
16:39:01
[root@localhost ~]#date +%y
19
[root@localhost ~]#date +%Y
2019
[root@localhost ~]#date +%Y/m/%d
2019/m/22
[root@localhost ~]#date +%Y/%m/%d
2019/08/22
[root@localhost ~]#date +%s
1566463197
[root@localhost ~]#date +%W
33

显示时区

[root@localhost ~]#timedatectl 
      Local time: Thu 2019-08-22 16:42:43 CST
  Universal time: Thu 2019-08-22 08:42:43 UTC
        RTC time: Thu 2019-08-22 08:42:43
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@localhost ~]#timedatectl set-timezone Asia/Tokyo

日历

  • cal

[root@localhost ~]# cal
     August 2019    
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
  • cal -y 一年的日历

[root@localhost ~]# cal -y
                               2019                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2                   1  2
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    3  4  5  6  7  8  9
13 14 15 16 17 18 19   10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 18 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31         24 25 26 27 28         24 25 26 27 28 29 30
                                              31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6             1  2  3  4                      1
 7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31            25 26 27 28 29 30 31   29 30

       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
27 28 29 30 31         24 25 26 27 28 29 30   29 30 31
  • cal # 显示某一年的日历

[root@localhost ~]# cal 2008
                               2008                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2                      1
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
                                              30 31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
27 28 29 30            25 26 27 28 29 30 31   29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5                   1  2       1  2  3  4  5  6
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    7  8  9 10 11 12 13
13 14 15 16 17 18 19   10 11 12 13 14 15 16   14 15 16 17 18 19 20
20 21 22 23 24 25 26   17 18 19 20 21 22 23   21 22 23 24 25 26 27
27 28 29 30 31         24 25 26 27 28 29 30   28 29 30
                       31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          1  2  3  4                      1       1  2  3  4  5  6
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    7  8  9 10 11 12 13
12 13 14 15 16 17 18    9 10 11 12 13 14 15   14 15 16 17 18 19 20
19 20 21 22 23 24 25   16 17 18 19 20 21 22   21 22 23 24 25 26 27
26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31
                       30

关机重启

  • shutdown 默认1分钟之后关机
    • shutdown -c 取消关机
  • shutdow -r 重启
  • TIME
    • now 立即
    • +n n分钟之后
    • hh:mm 指定时间
  • 关机命令
    • poweroff
    • halt
    • init 0
  • 重启
    • reboot
      • -f 强制
      • -p 关机
    • init 6
posted @ 2019-08-23 08:42  郭楷丰  阅读(424)  评论(0编辑  收藏  举报
Live2D