Skip to content

Linux基础和命令入门

Linux目录结构和学习bash命令基础铺垫

1. Linux目录结构

Linux系统中一切皆文件 目录结构图目录结构说明图

2. 使用VIM编辑器

2.1 vim编辑器简介

官网地址:https://www.vim.org/
VI是Unix操作系统和类Unix操作系统中最通用的文本编辑器
vi编辑器的模式:一般模式、编辑模式、命令模式
Alt text

2.2 一般模式

打开文件默认就是一般模式,他的操作快捷键:

语法功能描述
yy复制光标当前一行
y 数字 y复制一段(从第几行到第几行)
p箭头移动到目的行粘贴
u撤销上一步
dd删除光标当前行
d 数字 d删除光标(含)后多少行
x剪切一个字母,相当于 del
X剪切一个字母,相当于 Backspace
yw复制一个词
dw删除一个词
shift+6 (^)移动到行头
shift+4 ($)移动到行尾
1+shift+g移动到页头,数字
shift+g移动到页尾
shift+z+z关闭文件
数字+shift+g移动到目标行

提示

在一般模式中可以进行删除、复制、粘贴等的动作,但是却无法编辑文件内容的!

2.3 编辑模式

进入编辑模式要等到你按下i, I, o, O, a, A等任何一个字母之后才会进入编辑模式。编辑模式可以直接编辑里面内容。

2.4 指令模式

在一般模式当中,输入: / ? 3个中的任何一个按钮,进入指令模式

命令功能
:w保存
:q退出
:!强制执行
/要查找的词
n查找下一个
:n跳转到指定行
N往上查找
:noh取消高亮显示
:set nu显示行号
:set nonu关闭行号
:%s/old/new/g替换内容
/g替换匹配到的所有内容

3. 配置主机名

3.1 查看当前服务器主机名称

sh
[root@hadoop100 ~]# hostname

3.2 通过编辑/etc/hostname 文件

sh
[root@hadoop100 ~]# vi /etc/hostname

3.3 通过命令修改

sh
[root@hadoop100 ~]# hostnamectl set-hostname hadoop100

4. 配置hosts文件

linux主机映射文件也叫hosts文件

sh
vi /etc/hosts
192.168.101.100 hadoop100
192.168.101.101 hadoop101
192.168.101.102 hadoop102
192.168.101.103 hadoop103
192.168.101.104 hadoop104
192.168.101.105 hadoop105

5. 环境变量配置

5.1 修改/etc/profile文件

用来设置系统环境参数,比如$PATH。这里面的环境变量是对系统内所有用户生效。使用bash命令,需要source /etc/profile一下。

5.2 修改~/.bashrc文件

针对某一个特定的用户,环境变量的设置只对该用户自己有效。使用bash命令,只要以该用户身份运行命令行就会读取该文件。
把/etc/profile里面的环境变量追加到~/.bashrc目录:

sh
cat /etc/profile >> ~/.bashrc

说明

登录式Shell,采用用户名比如jack登录,会自动加载/etc/profile。
非登录式Shell,比如在命令行中执行ssh hadoop103 jps,它不会自动加载/etc/profile,会自动加载~/.bashrc

6. service服务管理

service服务就是后台进程, 它也叫守卫进程(daemon)。

6.1 基本用法

service 服务名 start | stop | restart | status
Centos7 使用systemctl代替了service命令:
systemctl start | stop | restart | status 服务名

service命令底层是调用etc/init.d目录下的服务脚本,service服务脚本都在etc/init.d下面可以查看,在Centos7中发现只剩两个服务保留: netconsole, network

sh
[jack@hadoop105 ~]$ ls /etc/init.d/
functions  netconsole  network  README

原因是Centos7的service服务已经移到/usr/lib/systemd/system下, 意味着service命令只能控制这两个服务,除此之外的service服务都由systemctl管理。

sh
[jack@hadoop102 ~]$ ls /usr/lib/systemd/system
arp-ethers.service                      machines.target                                swap.target
auditd.service                          messagebus.service                             sys-fs-fuse-connections.mount
autovt@.service                         microcode.service                              sysinit.target
basic.target                            multi-user.target                              sysinit.target.wants
basic.target.wants                      multi-user.target.wants                        sys-kernel-config.mount
blk-availability.service                NetworkManager-dispatcher.service              sys-kernel-debug.mount
bluetooth.target                        NetworkManager.service                         syslog.socket
brandbot.path                           NetworkManager-wait-online.service             syslog.target.wants
brandbot.service                        network-online.target                          systemd-ask-password-console.path
chrony-dnssrv@.service                  network-pre.target                             systemd-ask-password-console.service
chrony-dnssrv@.timer                    network.target                                 systemd-ask-password-plymouth.path
.....

6.2 管理开机自启的服务

sh
[root@hadoop100 ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'

netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:开    3:开    4:开    5:开    6:关

由于Centos7已经不使用init服务开启启动服务了(单线程),Centos7使用systemd服务来并发启动服务,chkconfig命令仍然读取/etc/init.d/,因此只能列出老的服务两个netconsole,network, 需要读取centos7的所有服务使用systemctl list-unit-files命令:

sh
[root@hadoop100 ~]# systemctl list-unit-files
UNIT FILE                                     STATE   
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static  
dev-mqueue.mount                              static  
tmp.mount                                     disabled
brandbot.path                                 disabled
auditd.service                                enabled 
autovt@.service                               enabled 
.......

其中static指的是依赖其他服务的是否自启

7. 系统的运行级别

7.1 Linux进程运行级别

Alt text

提示

运行级别1可以理解为windows中的安全模式

7.2 CentOS7运行级别变化的地方

multi-user.target 等价于原运行级别 3(多用户,无图形界面)
graphical.target 等价于原运行级别 5(多用户,有图形界面)

  1. 查看当前运行级别

    shell
    [root@hadoop100 ~]# systemctl get-default
    multi-user.target
  2. 修改当前运行级别

    sh
    [root@hadoop100 ~]# systemctl set-default TARGET.target (这里 TARGET multi-user 或者 graphical)
  3. 切换图形和命令行模式(3和5)

    • 使用快捷键: Ctrl+Alt+F1 图形化 Ctrl+Alt+F2 命令行
    • 使用命令切换: init 3/5

8. 帮助命令

8.1 man获取帮助信息(manual的简写)

基本语法: man [命令或配置文件]

信息功能
NAME命令的名称和单行描述
SYNOPSIS怎样使用命令
DESCRIPTION命令功能的深入讨论
EXAMPLES怎样使用命令的例子
SEE ALSO相关主题(通常是手册页)

8.2 help命令获取内置命令帮助信息

shell
[root@hadoop100 ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
。。。。。。

8.3 --help参数获取外部命令帮助信息

sh
root@hadoop100 ~]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all            不隐藏任何以. 开始的项目
  -A, --almost-all        列出除. 及.. 以外的任何项目
      --author            与-l 同时使用时列出每个文件的作者

8.4 bash命令类别区分

bash命令类别有两种:内置命令和外部命令

类型说明
内置命令一些基础功能的系统命令是直接内嵌在 shell 中的,系统加载启动之后会随着 shell 一起加载,常驻系统内存中
外部命令用户自行安装的程序或者说除了内置命令的其他命令

查看命令类别

sh
[root@hadoop100 bin]# type cd
cd shell 内嵌

8.5 使用快捷键起到帮助作用

常用快捷键功能
Ctrl + C停止进程
Ctrl+L清屏,等同于 clear;彻底清屏是:reset
Tab键提示(更重要的是可以防止敲错)
上下键查找执行过的命令
Ctrl + U删除光标前的所有内容
Ctrl + W删除光标前的一个单词
Backspace逐个字符删除光标前的内容
Ctrl + K删除光标后的所有内容

斜体 快捷键表示不同的终端和操作系统可能会支持不同的快捷键,具体情况可能有所不同。