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

  • 目前是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

內部命令:shell 自帶的命令windows

  • help 顯示全部的內部命令

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

查看命令的類型: typebash

[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:啓動或者關閉命令裏面的某些功能session

  • 長選項:--help --color
  • 短選項: -i -l

args:命令的做用體,通常狀況下是目錄或者文件,用戶名等等ssh

注意:

  • 短選項是能夠合併
  • 空格隔開
  • 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
相關文章
相關標籤/搜索