Linux Basic Learning

Catalog

Linux Basic Learning

User Login

  • root user
    • Is a special administrative account or can be a super administrator
    • root user has full control of the system
    • Infinite damage to the system
    • In your work, try not to use root if it is not necessary
  • Ordinary user
    • Limited permissions
    • Less damage to the system

terminal

  • classification
    • Device Terminal
    • Physical Terminal
    • Virtual Terminal ctrl+alt+f[1-6]/dev/tty#
    • Graphics Terminal/dev/tty7
    • Serial Terminal
    • /dev/pts/#for pseudo-terminal remote connection via ssh
    • View terminal command tty
    [root@localhost ~]# echo $SHELL
    /bin/bash
    • ip addr (ip a) command to view IP address
    [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

Interactive interface

  • An application attached to the terminal device after the terminal is started
    • GUI (Desktop)
    • CLI command line
      • powershell (win10)
      • sh
      • bash (linux, mac default program)
      • zsh
      • csh
      • tcsh

bash

bash is the user interface of the linux system and provides interaction between the user and the operating system. It receives input from the user and sends it to the operating system for execution (command line operations)

  • Currently the default shell on linux and mac (a command language)
  • centos is used by default
  • Show the shell echo $SHELL currently in use by the system
[root@localhost ~]# echo $SHELL
/bin/bash
  • View shell cat/etc/shells available in the system
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
  • ctrl +d Quickly terminates the current connection
  • Switch shell chsh-s/bin/shell (shell can be modified such as chsh-s/bin/zsh)

Slow steps to modify ssh connections

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

command prompt

[root@localhost ~]#  
Administrator is # (Command Prompt)
Ordinary User Time$

Show prompt format

[root@localhost ~]# echo $PS1
[\u@\h \W]\$
\u Represents the currently logged-in user
\h Host name on behalf of the current host
\W Represents the current directory
0 Represents the default font, 1 is bold, 4 is underlined below the font, 5 is blinking, 7 is highlighted
31-37 Font color
40-47 Represents the background color
echo 'PS1="\[\e[1;35mm\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/ps.sh #Effective Permanently

command

Execute Command: Enter Command Enter

Internal commands: commands that come with the shell

  • help Displays all internal commands

External Commands: Commands provided by third parties

View the type of command:type

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

alias

  • View all current alias es as
[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'
  • Custom alias CD etc='cd/etc'
  • Unalias unalias cdetc
  • Setting an alias is only valid for the current terminal
  • Set all users available/etc/bashrc
  • Valid for current user only ~/.bashrc
  • Execute own command
    • \command
    • "command"
    • 'command'
    • path

Command Format

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

Command command itself

options: start or close some of the functions in the command

  • Long option: --help --color
  • Short option: -i-l

args: The body of a command, typically a directory or file, user name, etc.

Be careful:

  • Short options are mergeable
  • Space Separated
  • ctrl+c to end command execution
  • To execute multiple commands on the same line; separate.
  • A command can be connected on multiple lines with \

Get help for commands

Internal commands:

  • 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.
  • Manbash executes this command and gets a list of all the built-in commands and how to use them
Linux Commands in the system can be divided into internal and external commands.Internal commands, also known as built-in commands ( builtin). How do I distinguish between internal and external commands?input man bash Command, you can view all internal commands
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)

External commands:

  • command -h
  • command --help
  • man coomand
  • Official Documents
[root@localhost ~]#python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
[] Optional
<> Data representing change
... Representation list
a|b|c perhaps
-abc Express-a -b -c
{} Represents grouping

man

 1 Executable programs or shell commands #User commands
 2 System calls (functions provided by the kernel) #System calls
 3 Library calls (functions within program libraries) #Calls to Libraries
 4 Special files (usually found in/dev) #Device files and special files
 5 File formats and conventions eg/etc/passwd #Profile format
 6 Games #Game
 7 Miscellaneous (including macro packages and conventions), e.g. man (7), groff (7)#Miscellaneous
 8 System administration commands (usually only for root) #Commands for management classes
 9 Kernel routines [Non standard] #Kernel API
 Quit q
 Flip Space
 Reverse Enter
 man chapter passwd

bash Shortcut

  • ctrl+l is equivalent to clear
  • ctrl+o executes the current command and displays it
  • ctrl+s lock screen
  • ctrl+q unlock
  • ctrl+c termination command
  • ctrl+z hang command
  • ctrl+a cursor moves to the beginning of the line, equivalent to Home
  • ctrl+e cursor moved to line position, equivalent to End
  • ctrl+xx jumps at the beginning and at the current cursor position
  • ctrl+k Deletes the text after the cursor
  • ctrl+u Delete text before cursor
  • alt+r Delete positive lines

tab key

  • Command Completion
    • Internal Command
    • External command: automatically matches the first found content, based on the path defined by the environment variable, searching one after the other
    • Complete directly if only one user-given command matches
    • If there are multiple matches, you need to press the tab key to show all the matched results
  • Directory Completion
    • Use user-given characters as the beginning of the file and complete them directly if there is only one match
    • If there are multiple matches, you need to press the tab key again to show all the matched results

Quotes

[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

Command History

  • You can use the up and down arrows to find commands previously executed
  • Store file is ~/.bash_history
  • The command executed is history
  • Execute the last command
    • up arrow
    • !!
    • !-1
    • ctrl+p carriage return
  • Call the last value esc of the previous command.
  • !#Specify how many commands
  • !-#Specify the last command
  • !string is used for the last matched command (bottom-up)
  • ctrl+r search command
  • ctrl+g Cancel Search
  • Display Last Command

Command Expansion

touch file{1..20} #Create 20 files
seq 0 2 10
echo file{1..20..2}

Echo echo

echo -e 'dadasda\ndasdasd'
echo -e '\a' #Play sound

View user login information

[root@localhost ~]#whoami # Show current logon user
root
[root@localhost ~]#who am i #Show details of the current logged on user
root     pts/2        2019-08-22 15:54 (192.168.21.1)
[root@localhost ~]#w Displays all users and commands executed
 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 shows the current time
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 #Modification Time
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 #Synchronize network server time
unix Year 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

Show time zone

[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

calendar

  • 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
  • Calendar of cal-y year

[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 #Displays a calendar of a year

[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 Restart

  • Shut down after default 1 minute
    • Shutdown-c cancels shutdown
  • Shutdow-r restart
  • TIME
    • now immediately
    • +n minutes later
    • hh:mm specified time
  • shutdown
    • poweroff
    • halt
    • init 0
  • restart
    • reboot
      • -f Force
      • -p Shutdown
    • init 6

Keywords: Linux shell ssh Mac

Added by silverphpd on Fri, 23 Aug 2019 04:40:25 +0300