CentOS7安装与Linux基础

1 Linux安装与基础

1.1 Linux哲学思想

  • 一切皆文件

  • 单一用途的程序,小型

  • 链接程序,共同完成复杂任务

  • 配置数据存储在文本中

  • 避免令人困惑的用户界面

    1.1.1 Linux操作系统

​ 完整的类Unix操作系统

​ linux内核+GNU工具=Linux/GNU

​ 如:CentOS,Ubuntu,Android

1.1.2 Linux内核版本组成

  • 主版本号

  • 次版本号

  • 末版本号

  • 打包版本号

  • 厂商版本号

CentOS7安装与Linux基础

1.2 Linux发行版

  • slackware: SUSE Linux Enterprise Server(SLES),OpenSure桌面

  • Debian:Ubuntu,deepin,mint

  • redhat: RHEL:ReadHat Enterprise linux,每18个月发行一个新版本

    ​ CentOS:Community Enterprise Operating System 兼容RHEL的格式

    ​ 中标麒麟: 中标软件

    ​ Fedora:每6个月发行新版本

  • Alpine: 一个面向安全的轻型Linux发行版。不同于通常Linux发行版,Alpine采用了musllibc和busybox以减少系统的体积和运行时资源消耗,但功能上比busybox又完善的多,只有5M 左右大小

  • ArchLinux: 轻量简洁,遵循K.I.S.S原则(keep it simple and stupid),Manjaro

  • Gentoo:极致性能,不提供传统意义的安装程序,下载源代码在本机上编译安装软件

  • LFS: Linux From scratch 自制Linux,只是一个说明书

  • Android: kerner+busybox(工具集)+ java虚拟机

Linux发行版排名:https://distrowactch.com/dwres.php?resoure=popularity.

Linux分支参考网站: http://futurist.se/gldt/

2 Linux系统安装

2.1 安装CentOS

开始安装

CentOS7安装与Linux基础

选择语言

CentOS7安装与Linux基础

选择时区

CentOS7安装与Linux基础

选择手动分区

CentOS7安装与Linux基础

添加挂载点

CentOS7安装与Linux基础

接收自定义分区

CentOS7安装与Linux基础

配置网络

CentOS7安装与Linux基础

关闭KDUMP

CentOS7安装与Linux基础

配置好以后开始安装过程

CentOS7安装与Linux基础

配置root密码

CentOS7安装与Linux基础

创建一个可登陆用户并设置密码

CentOS7安装与Linux基础

安装完成重启

CentOS7安装与Linux基础

测试lee新用户可以正常登陆

CentOS7安装与Linux基础

3 Linux常用命令

3.1 设置主机名

hostname NAME

范例

[root@centos7 ~]#hostname nc-dx-k8s-node1-10-10.teach.org

注意:主机名不要使用下划线

3.2 区别指定的命令是内部命令还是外部命令

type COMMAND

范例:查看是否存在对应内部和外部命令

[root@centos7 ~]#type -a echo
echo is a shell builtin
echo is /usr/bin/echo

3.2.1 shell中可执行的两类命令

  • 内部命令:由shell自带的,而且通过某命令形式提供
  • 外部命令:在文件系统路径下有对应的可执行程序文件

3.3 命令别名

常用的较长命令,可以将其定义为较短的别名,以方便执行。

显示当前shell进程所有可用的命令别名

alias

定义别名NAME,其相当于执行命令VALUE

范例:

[root@centos7 ~]#alias ipnet='/etc/sysconfig/network-scripts/'
[root@centos7 ~]#alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ipnet='/etc/sysconfig/network-scripts/'
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'

撤销别名:unalias

3.4 查看系统版本

3.4.1 查看内核版本

[root@centos7 ~]#uname -r
3.10.0-1160.el7.x86_64

3.4.2 查看操作系统发行版本

[root@centos7 ~]#cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@centos7 ~]#cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

3.5 查看硬件信息

3.5.1 查看cpu

[root@centos7 ~]#lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1 #每个core有几个线程
Core(s) per socket:    1 #每个槽位有1个core
Socket(s):             1 #服务器面板上有1个cpu槽位
NUMA node(s):          1 #nodes的数量
Vendor ID:             AuthenticAMD
CPU family:            23
Model:                 24
Model name:            AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
Stepping:              1
CPU MHz:               2096.064
BogoMIPS:              4192.12
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             64K
L2 cache:              512K
L3 cache:              4096K
NUMA node0 CPU(s):     0 #对应的core

3.5.2 查看内存大小

[root@centos7 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        162M        375M        7.6M        434M        652M
Swap:          1.9G        264K        1.9G
[root@centos7 ~]#cat /proc/meminfo 
MemTotal:         995676 kB
MemFree:          384444 kB
MemAvailable:     668456 kB
Buffers:            4176 kB
Cached:           389996 kB
SwapCached:           40 kB
Active:           231732 kB
Inactive:         203896 kB
Active(anon):      26888 kB
Inactive(anon):    22308 kB
Active(file):     204844 kB
Inactive(file):   181588 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       1952764 kB
SwapFree:        1952500 kB
Dirty:                 4 kB
Writeback:             0 kB
AnonPages:         41448 kB
Mapped:            29228 kB
Shmem:              7740 kB
Slab:              91984 kB
SReclaimable:      50580 kB
SUnreclaim:        41404 kB
KernelStack:        3920 kB
PageTables:         3640 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2450600 kB
Committed_AS:     234380 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      178416 kB
VmallocChunk:   34359310332 kB
Percpu:            22528 kB
HardwareCorrupted:     0 kB
AnonHugePages:      8192 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       63360 kB
DirectMap2M:      985088 kB
DirectMap1G:           0 kB
[root@centos7 ~]#

3.5.3 查看硬盘和分区情况

[root@centos7 ~]#blkid
/dev/sda1: UUID="30988762-b535-4145-8e94-4f17e1c7c617" TYPE="xfs" 
/dev/sda2: UUID="f720d1f7-ad53-443f-8a7f-b7838dc1f580" TYPE="xfs" 
/dev/sda3: UUID="97d22c9f-9550-46fb-9fed-0bb874de83f9" TYPE="swap" 
/dev/sda5: UUID="18731b4a-c59e-40f3-bf05-10d656e54e26" TYPE="xfs" 
/dev/sr0: UUID="2020-11-02-15-15-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
[root@centos7 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  953M  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0  1.9G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0 46.6G  0 part /data
sr0     11:0    1  9.5G  0 rom  
[root@centos7 ~]#cat /proc/partitions
major minor  #blocks  name

   8        0  209715200 sda
   8        1     975872 sda1
   8        2  104857600 sda2
   8        3    1952768 sda3
   8        4          1 sda4
   8        5   48827392 sda5
  11        0    9961472 sr0

3.6 日期和时间

Linux的两种时间

​ 系统时间:有Linux内核通过CPU的工作频率进行的

​ 硬件时钟:主板

​ 命令: date 显示和设置系统时间

选项:

  • date 显示时间
[root@centos7 ~]#date
Tue Jan 26 23:47:57 CST 2021
  • %Y 显示年 2021
[root@centos7 ~]#date +%Y
2021
  • %m 显示月 01
[root@centos7 ~]#date +%m
01
  • %d 显示日 26
[root@centos7 ~]#date +%d
26
  • %F 显示年月日
[root@centos7 ~]#date +%F
2021-01-26
  • %H 显示小时 24小时制 (00~23)
[root@centos7 ~]#date +%H
23
  • %M 显示分钟 (00~59)
[root@centos7 ~]#date
Fri Jan 29 19:57:17 CST 2021
[root@centos7 ~]#date +%M
57
  • %S 显示分秒(00~59)
  • %w 显示周 (0~6)
[root@centos7 ~]#date +%w
5
[root@centos7 ~]#date
Fri Jan 29 20:03:54 CST 2021
  • %s 显示1月1号 至今 多少秒
[root@centos7 ~]#date +%s
1611921876
  • %T 显示时间 时:分:秒
[root@centos7 ~]#date +%T
20:05:28

3.7 信息输出 echo

echo命令看将后面跟的字符进行输出

语法:

echo [-neE] 字符串

选项:

  • -E (默认)不支持\解释转义功能
  • -n 不换行
  • -e 启用\字符的转义解释功能

显示变量

echo "$VAR_NAME"    #用变量值替换,弱引用
[root@centos7 ~]#echo "$HOSTNAME"
centos7.9-server.rog
echo '$VAR_NAME'    #变量不会替换,强引用
[root@centos7 ~]#echo '$HOSTNAME'
$HOSTNAME

启用-e命令选项,出现以下字符,则特别加以处理,不会将它当成一般字符输出

  • \a 发出警告声
  • \b 退格键
  • \c 最后不加上换行符
  • \e escape,相当于\033
  • \n 换行且光标移至行首
  • \r 回车,即光标移至行首,但不换行
  • \t 插入tab
  • \\ 插入\字符
  • \0nnn 插入nnn(八进制) 所代表的ASCII字符
  • \xHH 插入HH(十六进制) 所代表的ASCII字符 (man 7 ascii)
[root@centos7 ~]#echo -e '\033[43;33;1;5mCentos\e[0m'
Centos
[root@centos7 ~]#echo \$PATH
$PATH
[root@centos7 ~]#echo \
> 

[root@centos7 ~]#echo \\
\
[root@centos7 ~]#ehco \\\
> 

3.8 screen

利用screen可以实现会话管理,如:新建会话,共享会话等,Centos7来自于base源,Centos8来自于epel源。

安装screen

[root@centos7 ~]#yum -y install screen

前提:连接的是同一网络中的同一台机器

screen命令常见用法:

  • 创建新screen会话

    screen -S [SESSION]

  • 加入screen会话

    screen -x [SESSION]

  • 退出并关闭screen会话

    exit

  • 剥离当前screen会话

    Ctrl+a,b

  • 显示所有已经打开的screen会话

    screen -ls

  • 恢复某screen会话

    screen -r [SESSION]

[root@centos7 ~]#screen -S 
[screen is terminating]
[root@centos7 ~]#screen -ls
There is a screen on:
    46322.top   (Attached)
1 Socket in /var/run/screen/S-root.

[root@centos7 ~]#screen -x top
[screen is terminating]

CentOS7安装与Linux基础

3.9 关机和重启

关机:

  • halt
  • poweroff
  • init 0

重启:

  • reboot

    -f:强制,不调用shutdown

    -p:切断电源

  • init 6

关机或重启:shutdown

[root@centos7 ~]#shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

     --help      Show this help     #显示帮助
  -H --halt      Halt the machine   #关机
  -P --poweroff  Power-off the machine  #关机
  -r --reboot    Reboot the machine     #重启
  -h             Equivalent to --poweroff, overridden by --halt     #关机
  -k             Don't halt/power-off/reboot, just send warnings    #发出警告
     --no-wall   Don't send wall message before halt/power-off/reboot   #关机或重启不发送wall消息
  -c             Cancel a pending shutdown      #取消挂起的关机

TIME:无指定,默认相当于+1(Centos7)

​ now: 立刻,相当于+0

​ +#:相对时间法,几分钟之后;例 +3

​ hh:mm: 绝对时间法,指明具体时间

[root@centos7 ~]#shutdown +10
Shutdown scheduled for Fri 2021-01-29 21:31:54 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from [email protected] (Fri 2021-01-29 21:21:54 CST):

The system is going down for power-off at Fri 2021-01-29 21:31:54 CST!

^C
[root@centos7 ~]#shutdown -c

Broadcast message from [email protected] (Fri 2021-01-29 21:22:00 CST):

The system shutdown has been cancelled at Fri 2021-01-29 21:23:00 CST!

[root@centos7 ~]#

4 history 命令相关shell变量

  • HISTSIZE #命令历史记录的条数

  • HISTFILE #指定命令历史文件,默认为~/.bash_history

  • HISTFILESIZE #命令历史文件记录历史的条数

  • HISTTMIEFORMAT #显示时间,例: HISTTIMEFORMAT=" %F %T"

  • HISTIGNORE #指定在历史中不记录的命令,例如: HISTIGNORE="str1:str2*...",忽略strl命令,str2开头的历史

  • HISTCONTROL #控制命令历史记录方式,可设置值:

    ​ ignoredups: 默认,忽略重复的命令,连续且相同认为重复

    ​ ignorespace:忽略空白开头的命令

    ​ ignoreboth:相当于ignoredups和ignorespace组合

    ​ erasedups:删除重复命令

    范例:

    修改~/.bash_profile,在最后一行增加HISTTIMEFORMAT设置,看到执行命令的时间

    [root@centos7 ~]#echo 'export HISTTIMEFORMAT="%F %T"' >> ~/.bash_profile
    [root@centos7 ~]#cat ~/.bash_profile 
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export PATH
    export HISTTIMEFORMAT="%F %T"
    
    [root@centos7 ~]#history 
      1  2021-01-29
      22:12:04history 

4.1 history

选项:

  • -c 清空历史命令
  • -d offset 删除历史中第offset个命令
  • n 显示最近的n条命令
  • -a 追加本次会话新执行的历史命令列表到历史文件
  • -r 读历史文件附加到历史列表

  • -w 保存历史列表到指定的历史文件
  • -n 读取历史文件中未读过的行到历史列表
  • -p 展开历史参数成多行,但不存在历史列表中
  • -s 展开历史参数成一行,附件在历史列表后
[root@centos7 ~]#history -c
[root@centos7 ~]#history 
    1  2021-01-29 22:18:47history 
[root@centos7 ~]#history -r
[root@centos7 ~]#history 1
  511  2021-01-29 22:19:03history 1

猜你喜欢

转载自blog.51cto.com/13618052/2611464