尚硅谷Linux学习(2022)
Linux是什么?
- Linux是一个操作系统(os)
- Linux之父——李纳斯 · 托瓦兹(Linus Torvalds)
一、Linux文件
Linux系统中一切皆文件。
二、Linux 目录结构
具体介绍
- /bin: 是Binary的缩写,这个目录存放着最经常使用的文件
- /sbin:s就是super user的意思,这里存放着系统管理员使用的系统管理程序
- /home:存放着普通用户的主目录,在linux中的每一个用户都有一个自己的目录,一般该目录是以用户的账号命名的
- /root:该目录为系统管理员,也称作超级权限者的用户主目录
- /boot:存放的是启动linux的一些核心文件,包括一些链接文件以及镜像文件
- /proc:虚拟目录,是系统内存的映射,可以访问该目录获得系统信息
- /srv:server缩写,存放服务启动后需要提取的数据
- /sys:安装了2.6内核中新出现的一个文件系统
- /tmp:存放临时文件
- /dev:类似于windows的设备管理器,把硬件用文件存储
- /media:linux系统会自动识别一些设备,例如U盘、光驱等等,识别后,linux会把识别的设备挂载在这个目录下
- /mnt:系统提供该目录是为了让用户临时挂载别的文件系统,我们可以将外部的存储挂载在/mnt/上,然后进入该目录就可以查看内容了。d:/myshare
- /opt:这是给主机额外安装软件的目录
- /usr/local: 这是另一个给主机额外安装软件的目录,一般通过编译源码方式安装的程序
- /var:该目录存放着不断扩充的东西,习惯将经常被修改的目录放在这个目录下,包括日志文件。
- /selinux[security-enhanced linux]360:sElinux是一种安全子系统,他能控制程序只访问特定文件
三、linux目录总结
- linux有且只有一个根目录/
- linux的各个目录存放的内容是规划好的,不能乱放文件
- linux是以文件的形式管理设备,因此linux系统,一切皆为文件
vi与vim编辑器
linux系统会内建vi文本编辑器,vim具有程序编辑能力,是vi增强版
三种模式:正常模式、插入/编辑模式、命令模式
普通模式命令
vim initial-setup-ks.cfg:打开当前文件
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd 桌面/
[root@hadoop100 桌面]# vim initial-setup-ks.cfg
i——进入编辑模式——esc退出
u——撤销
yy——复制行
dd——删除行
p——粘贴(5p则复制五行)
3yy——复制三行
3dd——删除三行
y$——复制从当前位置到行尾
y^——复制从当前位置到行始
d$——删除从当前位置到行尾
d^——删除从当前位置到行始
yw——复制当前单词
dw——删除当前单词(从光标开始直到空格)
x——剪切
r——替换当前字母
R——替换后面所有的字母
$——光标移动到行尾(home也可以)
^——光标移动到行始(end也可以)
b——跳到单词头
e——跳到单词末尾
gg——H——跳到文章开始位置
行数+gg——行数+G——行数+H——跳到指定行数
G——跳到文章结尾
L——跳到当前视图的结尾
插入模式命令
i——当前光标前
a——当前光标后
o——当前光标行的下一行
I——光标所在行最前
A——光标所在行最后
O——当前光标行的上一行
命令模式
注意:输入指令的时候不能处于编辑模式
:w——保存(什么文件已写入)
:q——退出当前文件
:wq——保存并退出
:q!——强制退出且不保存
:set nu——显示行号
:set nonu——不显示行号
/boot——查找boot(所有boot会进入高亮)
- n——跳转到下一个boot
- N——跳转到上一个boot
:noh——退出高亮
稍微了解
: s/old/new——替换当前行匹配到的第一个old为new
: s/old/new/g——替换当前行匹配呆的所有old为new
:%s/old/new——替换文档中每一行匹配呆的第一个old为new
:%/old/new/g——替换文档中每一行匹配到的所有old为new
网路连接测试
本地
(c) 2018 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>ipconfig
Windows IP 配置
以太网适配器 以太网:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
无线局域网适配器 本地连接* 1:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
无线局域网适配器 本地连接* 2:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
以太网适配器 VMware Network Adapter VMnet1:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::a455:f662:4ec8:4c7%15
IPv4 地址 . . . . . . . . . . . . : 192.168.121.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
以太网适配器 VMware Network Adapter VMnet8:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::b96d:addd:ba10:229d%4
IPv4 地址 . . . . . . . . . . . . : 192.168.74.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
无线局域网适配器 WLAN:
连接特定的 DNS 后缀 . . . . . . . :
IPv6 地址 . . . . . . . . . . . . : 240e:468:5a1:f01f:a8d4:7379:6fd8:3c3f
临时 IPv6 地址. . . . . . . . . . : 240e:468:5a1:f01f:a1ed:e8a1:3979:2c38
本地链接 IPv6 地址. . . . . . . . : fe80::a8d4:7379:6fd8:3c3f%7
IPv4 地址 . . . . . . . . . . . . : 192.168.43.191
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : fe80::6b1:67ff:feb1:8634%7
192.168.43.1
以太网适配器 蓝牙网络连接:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
C:\Users\Administrator>ping 192.168.74.129
正在 Ping 192.168.74.129 具有 32 字节的数据:
来自 192.168.74.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.74.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.74.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.74.129 的回复: 字节=32 时间<1ms TTL=64
192.168.74.129 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
虚拟机
[root@hadoop100 ~]# ping 192.168.101.25
PING 192.168.101.25 (192.168.101.25) 56(84) bytes of data.
64 bytes from 192.168.101.25: icmp_seq=1 ttl=128 time=0.606 ms
64 bytes from 192.168.101.25: icmp_seq=2 ttl=128 time=0.526 ms
^C
--- 192.168.101.25 ping statistics ---
17 packets transmitted, 17 received, 0% packet loss, time 16019ms
rtt min/avg/max/mdev = 0.526/0.834/1.638/0.339 ms
[root@hadoop100 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.130 netmask 255.255.255.0 broadcast 192.168.74.255
inet6 fe80::6971:3df5:224c:5455 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:3d:f1:39 txqueuelen 1000 (Ethernet)
RX packets 443820 bytes 621322968 (592.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84274 bytes 5177997 (4.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:74:64:14 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
VMware 提供了三种网路连接模式
-
桥接模式
虚拟机直接连接外部屋里网络的模式,主机气到了网桥的作用。这种模式下,虚拟机可以直接访问外部网络,并且对外部网络是可见的。
-
NAT模式
虚拟机和主机构建一个专用网络,并通过虚拟机网络地址转换(NAT)设备对IP进行转换。虚拟机通过共享主机IP可以访问外部网络,但外部网络无法访问虚拟机。
-
仅主机模式
虚拟机只与主机共享一个专用网络,与外部网络无法通信。
修改静态IP
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=237bfe5b-f058-4e3c-b930-94d5d1ee312e
DEVICE=ens33
ONBOOT=yes
#IP地址
IPADDR=192.168.202.100
#网关
GATEWAY=192.168.202.2
#域名解析器
DNS1=192.168.202.2
[root@hadoop100 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@hadoop100 ~]# service network restart # 重启网络
Restarting network (via systemctl):
[ 确定 ]
[root@hadoop100 ~]# ifconfig
......
测试连接
C:\Users\Administrator>ping 192.168.111.100
正在 Ping 192.168.111.100 具有 32 字节的数据:
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
192.168.111.100 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
修改主机名
[root@hadoop100 ~]# hostname
hadoop100
[root@hadoop100 ~]# hostnamectl set-hostname spark100
hadoop100
[root@hadoop100 ~]# hostname
spark100
[root@hadoop100 ~]# vim /etc/hosts
C:\Users\Administrator>ping hadoop100
正在 Ping hadoop100 [192.168.111.100] 具有 32 字节的数据:
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.111.100 的回复: 字节=32 时间<1ms TTL=64
192.168.111.100 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms
远程登录
C:\Users\Administrator>ssh root@hadoop100
The authenticity of host 'hadoop100 (192.168.111.100)' can't be established.
ECDSA key fingerprint is SHA256:EfysTwTI9bX0bS5XvdDmDKA4PTA2hZdAmVLlX6XQJNc.
Are you sure you want to continue connecting (yes/no)?yes
Warning: Permanently added 'hadoop100,192.168.111.100' (ECDSA) to the list of known hosts.
root@hadoop100's password:
Last login: Wed Jun 1 08:38:52 2022
[root@hadoop100 ~]#
下载xshell和xftp工具便捷操作。
Linux管理服务
[root@hadoop100 ~]# ls /usr/sbin
......
[root@hadoop100 ~]# ls /usr/sbin | grep service
service
[root@hadoop100 ~]# ls /etc/init.d/
functions netconsole network README
[root@hadoop100 ~]# service network restart # cenos6重启网络
Restarting network (via systemctl): [ 确定 ]
[root@hadoop100 ~]# systemctl restart restart # cenos7重启网络
Failed to restart restart.service: Unit not found.
[root@hadoop100 ~]# systemctl status network
。。。。。。
[root@hadoop100 ~]# systemctl status NetworkManager
。。。。。。
[root@hadoop100 ~]#
系统运行级别
运行级别(runleve)
查看默认级别:vim /etc/inittab
Linux系统级别有7种运行级别(runleve):常用的是级别3和5
- 运行级别0: 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
- 运行级别1: 单用户工作状态,root权限,用于系统维护,净值远程登录
- 运行级别2: 多用户状态(有NFC),不支持网络
- 运行级别3: 完全的多用户状态(有NFC),登陆后进入控制台命令行模式
- 运行级别4: 系统未使用,保留
- 运行级别5: X11控制台,登陆后进入图形GUI模式
- 运行级别6: 系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
[root@hadoop100 ~]# setup
[root@hadoop100 ~]# systemctl get-default
graphical.target
[root@hadoop100 ~]# vim /etc/inittab
配置服务开机启动和关闭防火墙
[root@hadoop100 ~]# chkconfig
注:该输出结果只显示 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:关
[root@hadoop100 ~]# setup # 图形化界面
[root@hadoop100 ~]# chkconfig network off # 关闭nework
[root@hadoop100 ~]# chkconfig
注:该输出结果只显示 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:关
[root@hadoop100 ~]# chkconfig network no # 开启nework --7版本没反应的
[root@hadoop100 ~]# systemctl status NetworkManager # 查看开机自启动的配置,默认开机自启动级别enabled 自启动
[root@hadoop100 ~]# systemctl disable NetworkManager # 更换级别为不自启
[root@hadoop100 ~]# systemctl enable NetworkManager # 换回来
[root@hadoop100 ~]# systemctl list-unit-files # 查看所有服务开机是否自启动信息
UNIT FILE STATE
var-lib-nfs-rpc_pipefs.mount static # 静态的
brandbot.path disabled # 不自启
cups.path enabled # 自启
-------------------------------------------------------------------------------------
防火墙
-------------------------------------------------------------------------------------
[root@hadoop100 ~]# systemctl status firewalld # 查看当前防火墙状态
[root@hadoop100 ~]# systemctl stop firewalld.service # 关闭
[root@hadoop100 ~]# systemctl disable firewalld.service # 更改开机自启动配置
[root@hadoop100 ~]# systemctl ensable firewalld.service # 开启
[root@hadoop100 ~]# systemctl start firewalld.service # 开启
[root@hadoop100 ~]# systemctl status firewalld # 然后查看
关机重启命令
[root@hadoop100 ~]# shutdown # 一分钟之后关机
Shutdown scheduled for 六 2022-06-04 16:51:59 CST, use 'shutdown -c' to cancel.
[root@hadoop100 ~]#
Broadcast message from root@hadoop100 (Sat 2022-06-04 16:50:59 CST):
The system is going down for power-off at Sat 2022-06-04 16:51:59 CST!
^C
[root@hadoop100 ~]# shutdown -c # 取消
Broadcast message from root@hadoop100 (Sat 2022-06-04 16:51:06 CST):
The system shutdown has been cancelled at Sat 2022-06-04 16:52:06 CST!
[root@hadoop100 ~]# shutdown now # 立刻关机
[root@hadoop100 ~]# shutdown 3 # 三分钟之后关机
Shutdown scheduled for 六 2022-06-04 16:57:34 CST, use 'shutdown -c' to cancel.
1、基本语法
(1)sync (功能描述:将数据有内存同步到硬盘中)
(2)halt (功能描述:停机,关闭系统,但不断电)
(3)poweroff (功能描述:关机,断电)
(4)reboot (功能描述:就是重启,等同于 shutduwn-r now)
(5)shutdown【选项】 时间
常用基本命令(重点)
一、帮助指令
1、帮助命令
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls -l /bin/ | grep sh
......
[root@hadoop100 ~]# man ls
[root@hadoop100 ~]# man cd
[root@hadoop100 ~]# type cd
cd 是 shell 内嵌
2、help 获得 sehll内置命令的帮助信息
[root@hadoop100 ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@hadoop100 ~]# type useradd
useradd 是 /usr/sbin/useradd
[root@hadoop100 ~]# type history
history 是 shell 内嵌
[root@hadoop100 ~]# history # 展示之前敲过的命令
[root@hadoop100 ~]# man -f cd
cd (1) - GNU Bourne-Again SHell (GNU 命令解释程序 “Bourne二世”)
cd (3tcl) - 改变工作目录
cd (1p) - change the working directory
[root@hadoop100 ~]# man 1p cd
[root@hadoop100 ~]# man 3 cd
[root@hadoop100 ~]# man man
[root@hadoop100 ~]# help cd
[root@hadoop100 ~]# help ls
-bash: help: 没有与 `ls' 匹配的帮助主题。尝试 `help help' 或者 `man -k ls' 或者 `info ls'。
[root@hadoop100 ~]# ls --help
3、常用快捷键
Ctrl + c——停止进程
Ctrl + l——清屏;等同于clear;彻底清除是:reset;
善于用 tab 键——提示(更重要的是可以防止敲错)
上下键——查找执行过的命令
二、文件目录类
1、pwd 显示当前工作目录的绝对路径
[root@langchao ~]# pwd
/root
[root@langchao ~]# cd /etc/sysconfig/
[root@langchao sysconfig]# pwd
/etc/sysconfig
[root@hadoop100 桌面]# type pwd
pwd 是 shell 内嵌
[root@hadoop100 ~]# cd /root/桌面
[root@hadoop100 桌面]# pwd
/root/桌面
[root@hadoop100 桌面]# cd ../视频/ # ../返回上一级
[root@hadoop100 视频]# cd /root
[root@hadoop100 ~]# cd 桌面/ # 跳转到子文件夹
[root@hadoop100 ~]# cd /etc/sysconfig/
[root@hadoop100 sysconfig]# cd -
/root
[root@hadoop100 ~]# cd - # 跳到上次文件位置
/etc/sysconfig
[root@hadoop100 sysconfig]# cd
[root@hadoop100 ~]# cd /home/langchao
[root@hadoop100 langchao]# su langchao
[langchao@hadoop100 ~]$ pwd
/home/langchao
[langchao@hadoop100 ~]$ exit
exit
[root@hadoop100 langchao]#
2、列出目录内容
选项
-a ——全部的文件,联通隐藏挡(开头为 . 的文件)一起列出来(常用)
-l ——长数据列出,包含文件的属性与权限等等数据;(常用)等价于 “ ll ”
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .config .dbus .ICEauthority .local .tcshrc .Xauthority 模板 图片 下载 桌面
.. .bash_history .bash_profile .cache .cshrc .esd_auth initial-setup-ks.cfg .mozilla .viminfo 公共 视频 文档 音乐
[root@hadoop100 ~]# cd ..
[root@hadoop100 /]# cd -
/root
[root@hadoop100 ~]# cd .
[root@hadoop100 ~]# cd ./桌面/
[root@hadoop100 ~]# ls -l
总用量 8
-rw-------. 1 root root 1867 5月 31 20:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 1895 5月 31 20:28 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 5月 31 12:29 公共
drwxr-xr-x. 2 root root 6 5月 31 12:29 模板
drwxr-xr-x. 2 root root 6 5月 31 12:29 视频
drwxr-xr-x. 2 root root 6 5月 31 12:29 图片
drwxr-xr-x. 2 root root 6 5月 31 12:29 文档
drwxr-xr-x. 2 root root 6 5月 31 12:29 下载
drwxr-xr-x. 2 root root 6 5月 31 12:29 音乐
drwxr-xr-x. 2 root root 100 6月 1 09:47 桌面
[root@hadoop100 ~]# ll
...
[root@hadoop100 ~]# type ll
ll 是 `ls -l --color=auto' 的别名
3、mkdir 创建和删除一个空的目录
[root@hadoop100 ~]# mkdir a
[root@hadoop100 ~]# ls
a anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mkdir /a
[root@hadoop100 ~]# ls
a anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mkdir /b
[root@hadoop100 ~]# ls
a anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls /
a b bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@hadoop100 ~]# ls
a anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mkdir b c
[root@hadoop100 ~]# ls
a anaconda-ks.cfg b c initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mkdir d/e/f
mkdir: 无法创建目录"d/e/f": 没有那个文件或目录
[root@hadoop100 ~]# mkdir d d/e d/e/f
[root@hadoop100 ~]# ls d/
e
[root@hadoop100 ~]# ls d/e
f
[root@hadoop100 ~]# mkdir -p g/h/i
[root@hadoop100 ~]# ls g/h
i
[root@hadoop100 ~]# rmdir a
[root@hadoop100 ~]# ls
anaconda-ks.cfg b c d g initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# rmdir b c
[root@hadoop100 ~]# rmdir d g
rmdir: 删除 "d" 失败: 目录非空
rmdir: 删除 "g" 失败: 目录非空
[root@hadoop100 ~]# rmdir d/e/f d/e d/
[root@hadoop100 ~]# ls
anaconda-ks.cfg g initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# rmdir -p g/h/i/
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
4、touch 创建空文件
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# touch hello
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# touch hello2
[root@hadoop100 langchao]# ls
hello2
[root@hadoop100 langchao]# cd
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# touch /home/langchao/hello3
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls /home/langchao
hello2 hello3
[root@hadoop100 ~]# vim hello4 # 直接 :q 退出就没有文件
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# vim hello4 # :wq 保存了就有
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello hello4 initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
5、复制文件或者文件夹
cp ——(功能描述:复制source文件到dest)
选项
-r —— 递归复制整个文件夹
参数
source——源文件
dest——目标文件
[root@hadoop100 ~]# ls
anaconda-ks.cfg hello hello4 initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls /home/langchao/
hello2 hello3 initial-setup-ks.cfg
[root@hadoop100 ~]# cp initial-setup-ks.cfg ../home/langchao/hello2
cp:是否覆盖"../home/langchao/hello2"? y
[root@hadoop100 ~]# ls /home/langchao/
hello2 hello3 initial-setup-ks.cfg
[root@hadoop100 ~]# vim /home/langchao/hello2
[root@hadoop100 ~]# cp initial-setup-ks.cfg /home/langchao/
cp:是否覆盖"/home/langchao/initial-setup-ks.cfg"? y
[root@hadoop100 ~]# \cp initial-setup-ks.cfg /home/langchao/
[root@hadoop100 ~]# \cp initial-setup-ks.cfg /home/langchao/hello3 #原生命令
[root@hadoop100 ~]# cp --help
...
[root@hadoop100 ~]# 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'
[root@hadoop100 ~]# mkdir a
[root@hadoop100 ~]# cp initial-setup-ks.cfg a/
[root@hadoop100 ~]# ls
a anaconda-ks.cfg hello hello4 initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cp -r a/ /home/langchao/
[root@hadoop100 ~]# ls /home/langchao/
a hello2 hello3 initial-setup-ks.cfg
[root@hadoop100 ~]# cd /home/langchao/a
[root@hadoop100 a]# ls
initial-setup-ks.cfg
6、删除文件和文件目录
选项
-r ——递归删除目录中所有内容
-f ——强制执行删除操作,而不是提示用于进行确认。
-v ——显示指令的详细执行过程
[root@hadoop100 ~]# ls
a anaconda-ks.cfg hello hello4 initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# rm hello
rm:是否删除普通空文件 "hello"?y
[root@hadoop100 ~]# rm -f hello4
[root@hadoop100 ~]# ls
a anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# rm a
rm: 无法删除"a": 是一个目录
[root@hadoop100 ~]# rm -r a
rm:是否进入目录"a"? y
rm:是否删除普通文件 "a/initial-setup-ks.cfg"?y
rm:是否删除目录 "a"?y
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ls
a hello2 hello3 initial-setup-ks.cfg
[root@hadoop100 langchao]# rm -rf a/
[root@hadoop100 langchao]# ls
hello2 hello3 initial-setup-ks.cfg
[root@hadoop100 langchao]# rm -f ./* # 清空文件夹
[root@hadoop100 langchao]# ls
[root@hadoop100 langchao]#
7、移动和重命名文件
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mv initial-setup-ks.cfg /home/langchao/
[root@hadoop100 ~]# ls
anaconda-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ls
initial-setup-ks.cfg
[root@hadoop100 langchao]# mv initial-setup-ks.cfg /root/
[root@hadoop100 langchao]# cd -
/root
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mv initial-setup-ks.cfg /home/langchao/1.cfg
[root@hadoop100 ~]# ls
anaconda-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls /home/langchao/
1.cfg
[root@hadoop100 ~]# mv /home/langchao/1.cfg initial-setup-ks.cfg
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mv anaconda-ks.cfg 2.cfg
[root@hadoop100 ~]# ls
2.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mv 2.cfg anaconda-ks.cfg
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
8、cat 查看文件内容
cat 【选项】——要查看的文件
选项
-n——显示所有行的行号,包括空行。
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cat initial-setup-ks.cfg
...
[root@hadoop100 ~]# cat -n initial-setup-ks.cfg
行号...
9、more 文件内部分屏查看器
more指令是一个基于VI编译器的文本过滤器,它以全屏膜的方式按页显示文本文件的内容。more 指令中内置快捷键,详见操作说明。
more——要查看的文件
操作说明
空格(space)—— 代表向下翻页
Enter —— 代表向下翻一行
q —— 代表立刻离开 more,不再显示改文件内容。
Ctrl + F —— 向下滚动一屏
Ctrl + B —— 返回上一屏
= ——输出当前行的行号
:f —— 输出文件名和当前行的行号
[root@hadoop100 ~]# more initial-setup-ks.cfg
10、less 分屏显示文件内容
less 指令用来分屏查询文件内容,它的功能与more指令类似,但是比 more 指令更加强大,支持各种显示终端,less指令在显示文件内容是,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。
less —— 要查看的文件
操作
空格(space)—— 代表向下翻页
[pagedown]—— 代表向下翻页
[pageup] —— 代表向上翻页
/字串 —— 向下搜寻【字串】的功能;n:向下查找;N:向上查找;
?字串 —— 向上搜寻【字串】的功能;n:向下查找;N:向上查找;
q —— 退出less
[root@hadoop100 ~]# less xzhdx.txt
11、echo
echo【选项】【输出内容】
选项
-e —— 支持反斜线控制的字符转换
操作
\\ —— 输出\本身
\n —— 换行符
\t —— 制表符,也就是 Tab 键
[root@hadoop100 ~]# echo hello,world
hello,woeld
[root@hadoop100 ~]# echo hello world
hello world
[root@hadoop100 ~]# echo hello world
hello world
[root@hadoop100 ~]# echo "hello world"
hello world
[root@hadoop100 ~]# echo "hello \ world"
hello \ world
[root@hadoop100 ~]# echo "hello \n world"
hello \n world
[root@hadoop100 ~]# echo -e "hello\nworld"
hello
world
[root@hadoop100 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ll
总用量 8
-rw-------. 1 root root 1867 5月 31 20:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 1895 5月 31 20:28 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 6月 11 00:45 xzhdx.txt
drwxr-xr-x. 2 root root 6 5月 31 12:29 公共
drwxr-xr-x. 2 root root 6 5月 31 12:29 模板
drwxr-xr-x. 2 root root 6 5月 31 12:29 视频
drwxr-xr-x. 2 root root 6 5月 31 12:29 图片
drwxr-xr-x. 2 root root 6 5月 31 12:29 文档
drwxr-xr-x. 2 root root 6 5月 31 12:29 下载
drwxr-xr-x. 2 root root 6 5月 31 12:29 音乐
drwxr-xr-x. 2 root root 100 6月 1 09:47 桌面
[root@hadoop100 ~]# ll > info
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cat info
总用量 8
-rw-------. 1 root root 1867 5月 31 20:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 6月 11 00:49 info
-rw-r--r--. 1 root root 1895 5月 31 20:28 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 6月 11 00:45 xzhdx.txt
drwxr-xr-x. 2 root root 6 5月 31 12:29 公共
drwxr-xr-x. 2 root root 6 5月 31 12:29 模板
drwxr-xr-x. 2 root root 6 5月 31 12:29 视频
drwxr-xr-x. 2 root root 6 5月 31 12:29 图片
drwxr-xr-x. 2 root root 6 5月 31 12:29 文档
drwxr-xr-x. 2 root root 6 5月 31 12:29 下载
drwxr-xr-x. 2 root root 6 5月 31 12:29 音乐
drwxr-xr-x. 2 root root 100 6月 1 09:47 桌面
[root@hadoop100 ~]# ls > info
[root@hadoop100 ~]# cat info
anaconda-ks.cfg
info
initial-setup-ks.cfg
xzhdx.txt
公共
模板
视频
图片
文档
下载
音乐
桌面
[root@hadoop100 ~]# echo "hello,linux" >> info
[root@hadoop100 ~]# cat info
anaconda-ks.cfg
info
initial-setup-ks.cfg
xzhdx.txt
公共
模板
视频
图片
文档
下载
音乐
桌面
hello,linux
[root@hadoop100 ~]# echo $ # 双击tab
Display all 111 possibilities? (y or n)
...
[root@hadoop100 ~]# echo $USER
root
[root@hadoop100 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@hadoop100 ~]# echo $HOSTNAME >> info
[root@hadoop100 ~]# cat info
anaconda-ks.cfg
info
initial-setup-ks.cfg
xzhdx.txt
公共
模板
视频
图片
文档
下载
音乐
桌面
hello,linux
hadoop100
12、head 显示文件头部内容
head 文件————(功能描述:查看文件头10内容)
head -n 5 文件————(功能描述:查看文件头5行内容,5可以是任意行数)
选项
-n<行数>——指定显示头部内容的行数
[root@hadoop100 ~]# head initial-setup-ks.cfg # 默认十行
[root@hadoop100 ~]# head -n 20 initial-setup-ks.cfg
13、tail 输出文件尾部内容
tail 文件 —— (功能描述:查看文件尾部10行内容)
tail -n 5 文件 ——(功能描述:查看文件尾部5行内容,5可以是任意行数)
tail -f 文件 —— (功能描述:实时追踪改文档的所有更新)
选项
-n<行数>——输出文件尾部n行的内容
-f —— 显示文件最新追加的内容,监视文件变化
[root@hadoop100 ~]# tail initial-setup-ks.cfg
[root@hadoop100 ~]# tail -n 5 initial-setup-ks.cfg
[root@hadoop100 ~]# tail -f info
initial-setup-ks.cfg
xzhdx.txt
公共
模板
视频
图片
文档
下载
音乐
桌面
hello # Crtl + s暂停
1
2
3 # Crtl + q继续
14、输出重定向和 >> 追加
ls -l > 文件——(功能描述:列表的内容写入文件a.txt中(覆盖写))
ls -al >> 文件 ——(功能描述:列表的内容追加到文件aa.txt的末尾)
ls -l > 文件——(功能描述:将文件1的内容覆盖到文件2)
echo “内容” >> 文件
新建连接
[root@hadoop100 ~]# echo hello >> info # >>追加
[root@hadoop100 ~]# echo 1 >> info
[root@hadoop100 ~]# echo 2 >> info
[root@hadoop100 ~]# echo 3 >> info
[root@hadoop100 ~]# vim info
[root@hadoop100 ~]# ls -i info
67669410 info # 67669410 索引节点号
15、ln 软连接
软链接也称为符号链接,类似于 windows 里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。
基本语法
ln -s[原文件或目录] [软链接名]——(功能描述:给原文件创建一个软链接)
经验技巧
删除软链接:rm -rf软链接名,而不是rm -rf软链接名
如果使用rm -rf软链接名/删除,会把软链接对应的真实目录下内容删掉
查询:通过 ll 就可以查看,列表属性弟1位是1,尾部会有位置指向。
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ln -s /root/info myInfo
[root@hadoop100 langchao]# ls
myInfo
[root@hadoop100 langchao]# ls -l
总用量 0
lrwxrwxrwx. 1 root root 10 6月 11 01:51 myInfo -> /root/info
[root@hadoop100 langchao]# cd -
/root
[root@hadoop100 ~]# ll
总用量 12
...
[root@hadoop100 ~]# cat info
...
[root@hadoop100 ~]# cd -
/home/langchao
[root@hadoop100 langchao]# cat myInfo
...
[root@hadoop100 ~]#
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# mkdir folder
[root@hadoop100 ~]# touch folder/file
[root@hadoop100 ~]# ls folder/
file
[root@hadoop100 ~]# cd -
/home/langchao
[root@hadoop100 langchao]# ln -s /root/folder/ /home/langchao/myFolfer
[root@hadoop100 langchao]# ls
myFolfer myInfo
[root@hadoop100 langchao]# ll
总用量 0
lrwxrwxrwx. 1 root root 13 6月 11 01:58 myFolfer -> /root/folder/
lrwxrwxrwx. 1 root root 10 6月 11 01:51 myInfo -> /root/info
[root@hadoop100 langchao]#
[root@hadoop100 langchao]# cd -
/root
[root@hadoop100 ~]# cd folder/
[root@hadoop100 folder]# ls
file
[root@hadoop100 folder]# pwd
/root/folder
[root@hadoop100 folder]# cd /home/langchao/
[root@hadoop100 langchao]# ls
myFolfer myInfo
[root@hadoop100 langchao]# cd myFolfer
[root@hadoop100 myFolfer]# ls
file
[root@hadoop100 myFolfer]# pwd
/home/langchao/myFolfer
[root@hadoop100 myFolfer]# pwd -P
/root/folder
[root@hadoop100 myFolfer]# cd /root/
cd: 用法:cd [-L|[-P [-e]]] [dir]
[root@hadoop100 ~]# cd -P /home/langchao/myFolfer/
[root@hadoop100 folder]# cd -
/root
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ls
myFolfer myInfo
[root@hadoop100 langchao]# rm myInfo
rm:是否删除符号链接 "myInfo"?y
[root@hadoop100 langchao]# ls
myFolfer
[root@hadoop100 langchao]# rm -rf myFolfer/
[root@hadoop100 langchao]# ls
myFolfer
[root@hadoop100 langchao]# cd -
/root
[root@hadoop100 ~]# ls
anaconda-ks.cfg folder info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd -
/home/langchao
[root@hadoop100 langchao]# ln -s /root/folder/ myFolfer
[root@hadoop100 langchao]# ls
myFolfer
[root@hadoop100 langchao]# rm -rf myFolfer/
[root@hadoop100 langchao]# ls
myFolfer
[root@hadoop100 langchao]# cd -
/root
[root@hadoop100 ~]# ls
anaconda-ks.cfg folder info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cd folder/
[root@hadoop100 folder]# ls
[root@hadoop100 myFolfer]# pwd -P
/root/folder
[root@hadoop100 folder]# cd
[root@hadoop100 ~]# rm -rf folder/
[root@hadoop100 ~]# cd /home/langchao/myFolfer/
-bash: cd: /home/langchao/myFolfer/: 没有那个文件或目录
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ls
myFolfer
[root@hadoop100 langchao]# ll
总用量 0
lrwxrwxrwx. 1 root root 13 6月 11 01:58 myFolfer -> /root/folder/
[root@hadoop100 langchao]# rm myFolfer
rm:是否删除符号链接 "myFolfer"?y
[root@hadoop100 langchao]# ls
16、history查看已经执行过的命令
[root@hadoop100 langchao]# history
[root@hadoop100 langchao]# history
[root@hadoop100 langchao]# !251
ls
[root@hadoop100 langchao]# history -c # 清除
三、时间日期类
1、显示当前时间
data —— 展示时间
选项
-d<时间字符串>——显示指定的”时间字符串“表示的时间,而非当前时间
-s<日期时间>——设置系统时间
[root@hadoop100 ~]# date
2022年 06月 11日 星期六 12:52:54 CST
[root@hadoop100 ~]# date +%Y
2022
[root@hadoop100 ~]# date +%y
22
[root@hadoop100 ~]# date +%m
06
[root@hadoop100 ~]# date +%d
11
[root@hadoop100 ~]# date +%Y-%m-%d-%H:%M:%S
2022-06-11-12:55:19
[root@hadoop100 ~]# date "+%Y-%m-%d %H:%M:%S"
2022-06-11 12:56:16
[root@hadoop100 ~]# date +%S
01
[root@hadoop100 ~]# date +%s
1654923603 # 时间戳
[root@hadoop100 ~]# date +%s
1654923617
2、设置系统时间
[root@hadoop100 ~]# date -d "1 days ago"
2022年 06月 10日 星期五 13:00:42 CST
[root@hadoop100 ~]# date -d "-1 days ago"
2022年 06月 12日 星期日 13:01:17 CST
[root@hadoop100 ~]# date -d "-1 hours ago"
2022年 06月 11日 星期六 14:02:05 CST
[root@hadoop100 ~]# date -s "2018-06-10 20:64:20"
date: 无效的日期"2018-06-10 20:64:20"
[root@hadoop100 ~]# date -s "2018-06-10 20:45:20"
2018年 06月 10日 星期日 20:45:20 CST
[root@hadoop100 ~]# date -s "2028-06-10 20:45:20"
2028年 06月 10日 星期六 20:45:20 CST
3、cal 查看日历
[root@hadoop100 ~]# date
2028年 06月 10日 星期六 20:46:45 CST
[root@hadoop100 ~]# al
bash: al: 未找到命令...
[root@hadoop100 ~]# cal
六月 2028
日 一 二 三 四 五 六
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
[root@hadoop100 ~]# cal -3
五月 2028 六月 2028 七月 2028
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
[root@hadoop100 ~]# cal -m
六月 2028
一 二 三 四 五 六 日
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
[root@hadoop100 ~]# cal 2022
....
用户管理命令
1、useradd 添加新用户
[root@hadoop100 ~]# cd /home/
[root@hadoop100 home]# ls
langchao
[root@hadoop100 home]# useradd tony
[root@hadoop100 home]# ls
langchao tony
[root@hadoop100 home]# useradd -d /home/dave daveid
[root@hadoop100 home]# ls
dave langchao tony
2、passwd 设置用户密码
[root@hadoop100 home]# passwd tony
更改用户 tony 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@hadoop100 home]# passwd daveid
更改用户 daveid 的密码 。
新的 密码:
无效的密码: 密码未通过字典检查 - 过于简单化/系统化
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@hadoop100 home]# id tony
uid=1001(tony) gid=1001(tony) 组=1001(tony)
3、id 查看用户是否存在
[root@hadoop100 home]# id tony
uid=1001(tony) gid=1001(tony) 组=1001(tony)
[root@hadoop100 home]# id daveid
uid=1002(daveid) gid=1002(daveid) 组=1002(daveid)
[root@hadoop100 home]# id dave
id: dave: no such user
4、cat /etc/passwd 查看创建了哪些用户
[root@hadoop100 home]# less /etc/passwd
<<<
langchao:x:1000:1000:langchao:/home/langchao:/bin/bash
tony:x:1001:1001::/home/tony:/bin/bash
daveid:x:1002:1002::/home/dave:/bin/bash
>>>
5、su 切换用户
[langchao@hadoop100 home]$ ls
dave langchao tony
[langchao@hadoop100 home]$ cd ~
[langchao@hadoop100 ~]$ pwd
/home/langchao
[langchao@hadoop100 ~]$ su tony
密码:
[tony@hadoop100 langchao]$ ls
ls: 无法打开目录.: 权限不够
[tony@hadoop100 ~]$ cd ~
[tony@hadoop100 ~]$ pwd
/home/tony
[tony@hadoop100 ~]$ su root
密码:
[tony@hadoop100 ~]$ exit
exit
[langchao@hadoop100 ~]$ ls
[langchao@hadoop100 ~]$ pwd
/home/langchao
[langchao@hadoop100 ~]$ ls ../tony/
ls: 无法打开目录../tony/: 权限不够
[langchao@hadoop100 ~]$ exit # 返回上级用户
exit
6、who 查看登录用户信息
[root@hadoop100 home]# ls
dave langchao tony
[root@hadoop100 home]# ls tony/
[root@hadoop100 home]# ls langchao/
[root@hadoop100 home]# who am i
root pts/2 2022-06-11 12:46 (192.168.111.1)
[root@hadoop100 home]# whoami
root
[root@hadoop100 home]# su daveid
[daveid@hadoop100 home]$ whoami
daveid
[daveid@hadoop100 home]$ who am i
root pts/2 2022-06-11 12:46 (192.168.111.1)
[daveid@hadoop100 home]$ su tony
密码:
[tony@hadoop100 home]$ who am i
root pts/2 2022-06-11 12:46 (192.168.111.1)
[tony@hadoop100 home]$ who
root :0 2022-06-10 15:10 (:0)
root pts/0 2022-06-10 15:11 (:0)
root pts/1 2022-06-10 23:05 (192.168.111.1)
root pts/2 2022-06-11 12:46 (192.168.111.1)
7、获取root权限和删除用户
1、sudo 设置普通用户具有root权限
[tony@hadoop100 home]$ cd /root
bash: cd: /root: 权限不够
[tony@hadoop100 root]$ exit
exit
[root@hadoop100 ~]# su tony
[tony@hadoop100 root]$ ls
ls: 无法打开目录.: 权限不够
[tony@hadoop100 root]$ sudo ls
[sudo] tony 的密码:
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
2、userdel 删除用户
[root@hadoop100 ~]# userdel tony
[root@hadoop100 ~]# cd /home/
[root@hadoop100 home]# ls
dave langchao tony
[root@hadoop100 home]# cat /etc/passwd
[root@hadoop100 home]# rm -rf tony/
[root@hadoop100 home]# userdel -r daveid
[root@hadoop100 home]# ls
langchao
[root@hadoop100 home]# id daveid
id: daveid: no such user
用户组命令管理
每个用户都有一个用户组,系统可以对一个用户组中的所有用户进行集中管理。不顾听Linux系统对用户组的规定有所不同。
如Linux下的用户属于与它同名的用户组,这个用户组在创建用户时同时创建。
用户组的管理涉及用户组的添加、删除和修改。组的增加、删除和修改实际上就是对/etc/group文件的更新。
1、groupadd 新增组和修改组
[langchao@hadoop100 root]$ groupadd meifa
groupadd: Permission denied.
groupadd:无法锁定 /etc/group,请稍后再试。
[langchao@hadoop100 root]$ exit
exit
您在 /var/spool/mail/root 中有新邮件
[root@hadoop100 ~]# group meifa
bash: group: 未找到命令...
[root@hadoop100 ~]# groupadd meifa
[root@hadoop100 ~]# cat /etc/group # 查看有哪些组
meifa:x:1003:
[root@hadoop100 ~]# usermod -g meifa tony # 修改用户所在用户组
[root@hadoop100 ~]# usermod -g meifa david
[root@hadoop100 ~]# id tony
uid=1001(tony) gid=1003(meifa) 组=1003(meifa)
[root@hadoop100 ~]# id david
uid=1002(david) gid=1003(meifa) 组=1003(meifa)
[root@hadoop100 ~]# groupmod -n haircut meifa # 重命名用户组
[root@hadoop100 ~]# id tony
uid=1001(tony) gid=1003(haircut) 组=1003(haircut)
[root@hadoop100 ~]# id david
uid=1002(david) gid=1003(haircut) 组=1003(haircut)
2、groupdel删除用户组
[root@hadoop100 ~]# groupdel tony
[root@hadoop100 ~]# groupdel david
[root@hadoop100 ~]# cat /etc/group
...
haircut:x:1003:
文件权限类
1、文件属性
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限。为了保护系统的安全性,Linux 系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定。在Linux中我们可以使用 ll 或者ls -l命令来显示一个文件的属性以及文件所属的用户和组。
[root@hadoop100 ~]# cp ./initial-setup-ks.cfg /home/langchao/
[root@hadoop100 ~]# cp ./anaconda-ks.cfg /home/langchao/
[root@hadoop100 ~]# su langchao
[langchao@hadoop100 root]$ cd ~
[langchao@hadoop100 ~]$ ls
anaconda-ks.cfg initial-setup-ks.cfg
[langchao@hadoop100 ~]$ ll
总用量 8
-rw-------. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 1895 6月 10 22:42 initial-setup-ks.cfg
[langchao@hadoop100 ~]$ cat initial-setup-ks.cfg
...
[langchao@hadoop100 ~]$ cat anaconda-ks.cfg
cat: anaconda-ks.cfg: 权限不够
- 如果查看到是文件:链接数指的是硬链接个数。
- 如果查的看是文件夹:链接数指的是子文件夹个数。
2、chmod 改变权限
[root@hadoop100 ~]# cd /home/langchao/
[root@hadoop100 langchao]# ls
anaconda-ks.cfg initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-------. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 1895 6月 10 22:42 initial-setup-ks.cfg
[root@hadoop100 langchao]# chmod u+x initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-------. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rwxr--r--. 1 root root 1895 6月 10 22:42 initial-setup-ks.cfg
[root@hadoop100 langchao]# chmod a=rw initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-------. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rw-rw-rw-. 1 root root 1895 6月 10 22:42 initial-setup-ks.cfg
[root@hadoop100 langchao]# chmod 644 anaconda-ks.cfg
[root@hadoop100 langchao]# chmod 777 initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-r--r--. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rwxrwxrwx. 1 root root 1895 6月 10 22:42 initial-setup-ks.cfg
[root@hadoop100 langchao]# chown -R 777 xiyou/ # 修改整个文件夹,给它777权限
3、chown 改变所有者
[root@hadoop100 langchao]# chown langchao initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-r--r--. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rwxrwxrwx. 1 langchao root 1895 6月 10 22:42 initial-setup-ks.cfg
[root@hadoop100 langchao]# cat initial-setup-ks.cfg
[root@hadoop100 langchao]# su langchao
[langchao@hadoop100 ~]$ ll
总用量 8
-rw-r--r--. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rwxrwxrwx. 1 langchao root 1895 6月 10 22:42 initial-setup-ks.cfg
4、chgrp 改变所属组
[langchao@hadoop100 ~]$ chgrp haircut initial-setup-ks.cfg
chgrp: 正在更改"initial-setup-ks.cfg" 的所属组: 不允许的操作
[langchao@hadoop100 ~]$ exit
exit
[root@hadoop100 langchao]# chgrp haircut initial-setup-ks.cfg
[root@hadoop100 langchao]# ll
总用量 8
-rw-r--r--. 1 root root 1867 6月 10 22:40 anaconda-ks.cfg
-rwxrwxrwx. 1 langchao haircut 1895 6月 10 22:42 initial-setup-ks.cfg
5、综合应用案例
注意:看不懂没事,到时候用到的时候回来复习。这里一个是给用户的权限,一个是给组的权限。
[root@hadoop100 langchao]# groupadd bigdata
[root@hadoop100 langchao]# groupadd testing
[root@hadoop100 langchao]# cat /etc/group
bigdata:x:1004:
testing:x:1005:
[root@hadoop100 langchao]# useradd -g bigdata xiaoming
[root@hadoop100 langchao]# id xiaoming
uid=1003(xiaoming) gid=1004(bigdata) 组=1004(bigdata)
[root@hadoop100 langchao]# useradd -g bigdata xiaoliang
[root@hadoop100 langchao]# id xiaoliang
uid=1004(xiaoliang) gid=1004(bigdata) 组=1004(bigdata)
[root@hadoop100 langchao]# useradd -g testing xiaohong
[root@hadoop100 langchao]# useradd -g testing xiaolang
[root@hadoop100 langchao]# id xiaohong
uid=1005(xiaohong) gid=1005(testing) 组=1005(testing)
[root@hadoop100 langchao]# id xiaolang
uid=1006(xiaolang) gid=1005(testing) 组=1005(testing)
[root@hadoop100 langchao]# cd /home
[root@hadoop100 home]# ls
dave langchao tony xiaohong xiaolang xiaoliang xiaoming
[root@hadoop100 home]# su xiaoming
[xiaoming@hadoop100 home]$ cd ~
[xiaoming@hadoop100 ~]$ ls
[xiaoming@hadoop100 ~]$ pwd
/home/xiaoming
[xiaoming@hadoop100 ~]$ vim import_code
[xiaoming@hadoop100 ~]$ ll
总用量 4
-rw-r--r--. 1 xiaoming bigdata 12 6月 10 23:48 import_code
xiaoming@hadoop100 ~]$ exit
exit
[root@hadoop100 home]# su xiaoliang
[xiaoliang@hadoop100 home]$ cd ~
[xiaoliang@hadoop100 ~]$ cd ../xiaoming/
bash: cd: ../xiaoming/: 权限不够
[xiaoliang@hadoop100 ~]$ exit
exit
[root@hadoop100 home]# ls
dave langchao tony xiaohong xiaolang xiaoliang xiaoming
[root@hadoop100 home]# ll
总用量 0
drwx------. 3 david haircut 78 6月 10 21:52 dave
drwx------. 5 langchao langchao 179 6月 10 22:42 langchao
drwx------. 3 tony haircut 78 6月 10 21:51 tony
drwx------. 3 xiaohong testing 78 6月 10 23:46 xiaohong
drwx------. 3 xiaolang testing 78 6月 10 23:46 xiaolang
drwx------. 5 xiaoliang bigdata 128 6月 10 23:52 xiaoliang
drwx------. 5 xiaoming bigdata 163 6月 10 23:49 xiaoming
[root@hadoop100 home]# chmod g+x xiaoming/
[root@hadoop100 home]# chmod g+r xiaoming/
[root@hadoop100 home]# ll
总用量 0
drwx------. 3 david haircut 78 6月 10 21:52 dave
drwx------. 5 langchao langchao 179 6月 10 22:42 langchao
drwx------. 3 tony haircut 78 6月 10 21:51 tony
drwx------. 3 xiaohong testing 78 6月 10 23:46 xiaohong
drwx------. 3 xiaolang testing 78 6月 10 23:46 xiaolang
drwx------. 5 xiaoliang bigdata 128 6月 10 23:52 xiaoliang
drwxr-x---. 5 xiaoming bigdata 163 6月 10 23:49 xiaoming
[root@hadoop100 home]# su xiaoliang
[xiaoliang@hadoop100 home]$ cd xiaoming
[xiaoliang@hadoop100 xiaoming]$ ll
总用量 4
-rw-r--r--. 1 xiaoming bigdata 12 6月 10 23:48 import_code
[xiaoliang@hadoop100 xiaoming]$ cat import_code
hello world
[xiaoliang@hadoop100 xiaoming]$ exit
exit
[root@hadoop100 home]# su xiaoming
[xiaoming@hadoop100 home]$ cd ~
[xiaoming@hadoop100 ~]$ ll
总用量 4
-rw-r--r--. 1 xiaoming bigdata 12 6月 10 23:48 import_code
[xiaoming@hadoop100 ~]$ chmod g+w import_code
[xiaoming@hadoop100 ~]$ exit
exit
[root@hadoop100 home]# su xiaoliang
[xiaoliang@hadoop100 home]$ cd xiaoming/
[xiaoliang@hadoop100 xiaoming]$ vim import_code
[xiaoliang@hadoop100 xiaoming]$ cat import_code
hello world !!!
[xiaohong@hadoop100 home]$ exit
exit
[root@hadoop100 home]# chmod 755 xiaoming
[root@hadoop100 home]# ll
总用量 0
drwx------. 3 david haircut 78 6月 10 21:52 dave
drwx------. 5 langchao langchao 179 6月 10 22:42 langchao
drwx------. 3 tony haircut 78 6月 10 21:51 tony
drwx------. 5 xiaohong testing 128 6月 11 00:04 xiaohong
drwx------. 5 xiaolang testing 128 6月 11 00:01 xiaolang
drwx------. 5 xiaoliang bigdata 144 6月 11 00:02 xiaoliang
drwxr-xr-x. 5 xiaoming bigdata 163 6月 10 23:49 xiaoming
[root@hadoop100 home]# su xiaohong
[xiaohong@hadoop100 home]$ cd xiaoming/
[xiaohong@hadoop100 xiaoming]$ ll
总用量 4
-rw-rw-r--. 1 xiaoming bigdata 16 6月 11 00:02 import_code
[xiaohong@hadoop100 xiaoming]$ cat import_code
hello world !!!
[xiaohong@hadoop100 xiaoming]$ exit
exit
[root@hadoop100 home]# usermod -g bigdata xiaolang
[root@hadoop100 home]# id xiaolang
uid=1006(xiaolang) gid=1004(bigdata) 组=1004(bigdata)
[root@hadoop100 home]# su xiaolang
[xiaolang@hadoop100 home]$ cd xiaoming/
[xiaolang@hadoop100 xiaoming]$ ls
import_code
[xiaolang@hadoop100 xiaoming]$ vim import_code
[xiaolang@hadoop100 xiaoming]$ cat import_code
hello world !!!
hello world !!!
搜索查找类
1、find 查找文件或者目录
find 指令将从指定目录向下递归地遍历各个字目录,将满足条件的文件显示在终端。
选项
-name<查询方式>——按照指定的文件名查找模式查找文件
-user<用户名>——查找属于指定用户名所有文件
-size<文件大小>——按照文件大小查找文件,单位:b-块(512字节),c-字节,w-字(2字节)…
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# find -name info
./info
[root@hadoop100 ~]# touch 公共/info
[root@hadoop100 ~]# find -name info
./公共/info
./info
[root@hadoop100 ~]# find /root -name info
/root/公共/info
/root/info
[root@hadoop100 ~]# find /root/公共 -name info
/root/公共/info
[root@hadoop100 ~]# find /root -name "*.cfg"
/root/.config/yelp/yelp.cfg
/root/.local/share/telepathy/mission-control/accounts.cfg
/root/.local/share/telepathy/mission-control/accounts-goa.cfg
/root/桌面/initial-setup-ks.cfg
/root/initial-setup-ks.cfg
/root/anaconda-ks.cfg
[root@hadoop100 ~]# find /home -user tony
/home/tony
/home/tony/.mozilla
/home/tony/.mozilla/extensions
/home/tony/.mozilla/plugins
/home/tony/.bash_logout
/home/tony/.bash_profile
/home/tony/.bashrc
2、locate 快速定位文件路径
[root@hadoop100 ~]# updatedb
[root@hadoop100 ~]# locate tmp
...
[root@hadoop100 ~]# locate xzhdx
/root/xzhdx.txt
[root@hadoop100 ~]# which ls # 查找当前的命令到底在哪里
alias ls='ls --color=auto'
/usr/bin/ls
[root@hadoop100 ~]# which locate
/usr/bin/locate
[root@hadoop100 ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/alias
/usr/bin/which
[root@hadoop100 ~]# whereis locate
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
3、grep 过滤查找及“ | ”管道符
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# grep -n boot initial-setup-ks.cfg
3:xconfig --startxonboot
12:# Run the Setup Agent on first boot
13:firstboot --enable
23:network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --activate
31:# System bootloader configuration
32:bootloader --location=mbr --boot-drive=sda
37:part /boot --fstype="xfs" --ondisk=sda --size=1024
[root@hadoop100 ~]# ls | grep .cfg
anaconda-ks.cfg
initial-setup-ks.cfg
[root@hadoop100 ~]# cat info
anaconda-ks.cfg
info
initial-setup-ks.cfg
xzhdx.txt
公共
模板
视频
图片
文档
下载
音乐
桌面
hello world
1
2
3
[root@hadoop100 ~]# wc info
16 17 120 info # 行数 单词数 字节数 文件名
[root@hadoop100 ~]# grep -n boot initial-setup-ks.cfg | wc
7 30 304
压缩和解压缩类
1、gzip/gunzip 压缩
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# gzip xzhdx.txt
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt.gz 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# gunzip xzhdx.txt.gz
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
2、zip/unzip 压缩
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# zip -r myroot.zip /root
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg myroot.zip xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# ls -lh
总用量 64M
-rw-------. 1 root root 1.9K 5月 31 2022 anaconda-ks.cfg
-rw-r--r--. 1 root root 120 6月 11 2022 info
-rw-r--r--. 1 root root 1.9K 5月 31 2022 initial-setup-ks.cfg
-rw-r--r--. 1 root root 55M 6月 11 01:19 myroot.zip
-rw-r--r--. 1 root root 0 6月 11 2022 xzhdx.txt
drwxr-xr-x. 2 root root 18 6月 11 00:49 公共
drwxr-xr-x. 2 root root 6 5月 31 2022 模板
drwxr-xr-x. 2 root root 6 5月 31 2022 视频
drwxr-xr-x. 2 root root 6 5月 31 2022 图片
drwxr-xr-x. 2 root root 6 5月 31 2022 文档
drwxr-xr-x. 2 root root 6 5月 31 2022 下载
drwxr-xr-x. 2 root root 6 5月 31 2022 音乐
drwxr-xr-x. 2 root root 100 6月 1 2022 桌面
[root@hadoop100 ~]# unzip -d /tmp myroot.zip
[root@hadoop100 ~]# cd /tmp
[root@hadoop100 tmp]# ls
...
[root@hadoop100 tmp]# cd root
[root@hadoop100 root]# ls
anaconda-ks.cfg info initial-setup-ks.cfg xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
3、tar 打包
选项
-c——产生.tar打包文件
-v——显示详细信息
-f——指定压缩后的文件名
-z——打包同时压缩
-x——解包.tar文件
-C——解压到指定目录
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg myroot.zip xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# tar -zcvf temp.tar.gz initial-setup-ks.cfg xzhdx.txt info 公共/
initial-setup-ks.cfg
xzhdx.txt
info
公共/
公共/info
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg myroot.zip temp.tar.gz xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# tar -zxvf temp.tar.gz -C /tmp
initial-setup-ks.cfg
xzhdx.txt
info
公共/
公共/info
[root@hadoop100 ~]# cd /
磁盘查看和分区类
1、du 查看文件和目录占用的磁盘空间
[root@hadoop100 ~]# yum install tree
...yy...
[root@hadoop100 ~]# tree ./
[root@hadoop100 ~]# du
...
选项
-h——以人们较易阅读的CBytes,MBytes,KBytes等格式自行显示;
-a——不仅查看子目录大小,还要包括文件
-c——显示所有的文件和子目录大小后,显示总和
-s——只显示总和
–max-depth=n——指定统计子目录的深度为第n层
[root@hadoop100 ~]# du -sh
144M .
[root@hadoop100 ~]# du --max-depth=1 -ah
2、df 查看磁盘空间使用情况
[root@hadoop100 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 21M 1.9G 2% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda3 35G 4.8G 31G 14% /
/dev/sda1 1014M 208M 807M 21% /boot
tmpfs 394M 40K 394M 1% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /run/media/root/CentOS 7 x86_64
[root@hadoop100 ~]# free -h
total used free shared buff/cache available
Mem: 3.8G 856M 1.9G 32M 1.1G 2.7G
Swap: 4.0G 0B 4.0G
3、lsblk 查看设备挂载情况
[root@hadoop100 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 35G 0 part /
sr0 11:0 1 4.4G 0 rom /run/media/root/CentOS 7 x86_64
[root@hadoop100 ~]# ls /dev/ | grep sr0
sr0
[root@hadoop100 ~]# ll /dev/ | grep sr0
lrwxrwxrwx. 1 root root 3 6月 10 2022 cdrom -> sr0
brw-rw----+ 1 root cdrom 11, 0 6月 10 2022 sr0
[root@hadoop100 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs a10568c0-4088-4b20-8450-364aa34ccf45 /boot
├─sda2 swap 95f4ce6c-5817-403d-bb03-6d6c3ce5d96e [SWAP]
└─sda3 xfs c713ec5d-8378-4602-9efb-e944fcf4791a /
sr0 iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00 /run/media/root/CentOS 7 x86_64\
4、mount/omount 挂载/卸载
[root@hadoop100 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 35G 0 part /
sr0 11:0 1 4.4G 0 rom /run/media/root/CentOS 7 x86_64
[root@hadoop100 ~]# ls /run/media/root/CentOS\ 7\ x86_64/
[root@hadoop100 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 35G 0 part /
sr0 11:0 1 4.4G 0 rom
[root@hadoop100 ~]# ls /run/media/root/CentOS\ 7\ x86_64/
ls: 无法访问/run/media/root/CentOS 7 x86_64/: 没有那个文件或目录
[root@hadoop100 ~]# mkdir /mnt/cdrom
[root@hadoop100 ~]# mount /dev/cdrom /mnt/cdrom
mount: /dev/sr0 写保护,将以只读方式挂载
[root@hadoop100 ~]# ls /mnt/cdrom
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
[root@hadoop100 ~]# vim /etc/fstab
磁盘分区
1、fdisk分区
[root@hadoop100 ~]# fdisk -l
磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000985fc
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris
/dev/sda3 10487808 83886079 36699136 83 Linux
[root@hadoop100 ~]# lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
sda1 8:1 0 1G 0 part /boot
sda2 8:2 0 4G 0 part [SWAP]
sda3 8:3 0 35G 0 part /
sr0 11:0 1 4.4G 0 rom /mnt/cdrom
[root@hadoop100 ~]# reboot
...后面太多了,自己去看尚硅谷Linux(2020)54集
一个磁盘最多划分分区为:四个主分区
进程管理类
1、ps 查看当前系统进程
选项
a——列出带有终端的所有用户的进程
x——列出当前用户的所有进程,包括没有终端进程
u——面向用户友好的显示风格
-e——列出所有进程
-u——列出某个用户关联的所有进程
-f——显示完整格式的进程列表
[root@hadoop100 ~]# ls /usr/lib/systemd/system
...
[root@hadoop100 ~]# ls /usr/lib/systemd/system | grep d.service
...
root@hadoop100 ~]# ps aux
...
[root@hadoop100 ~]# ps aux | less
[root@hadoop100 ~]# ps -ef
...
[root@hadoop100 ~]# ps -ef | less
技巧
如果想查看京城的CPU占用率和内存占用率,可以使用 aux ;
如果想查看进程的父进程 ID 可以使用 ef ;
2、查看远程登录进程
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 22:27 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 22:28 ? 00:00:02 sshd: root@pts/0
root 2725 1744 0 23:35 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# systemctl status sshd
...
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 22:27 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 22:28 ? 00:00:02 sshd: root@pts/0
root 2786 1087 2 23:40 ? 00:00:01 sshd: root@pts/1
root 2885 1744 0 23:40 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 22:27 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 22:28 ? 00:00:02 sshd: root@pts/0
root 2786 1087 0 23:40 ? 00:00:01 sshd: root@pts/1
root 2923 1087 2 23:44 ? 00:00:01 sshd: langchao [priv]
langchao 2932 2923 0 23:44 ? 00:00:00 sshd: langchao@pts/2
root 2985 1744 0 23:45 pts/0 00:00:00 grep --color=auto sshd
3、kill 终止进程
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 22:27 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 22:28 ? 00:00:02 sshd: root@pts/0
root 2786 1087 0 23:40 ? 00:00:01 sshd: root@pts/1
root 2923 1087 2 23:44 ? 00:00:01 sshd: langchao [priv]
langchao 2932 2923 0 23:44 ? 00:00:00 sshd: langchao@pts/2
root 2985 1744 0 23:45 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# kill 2923
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 6月11 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 6月11 ? 00:00:02 sshd: root@pts/0
root 2786 1087 0 6月11 ? 00:00:02 sshd: root@pts/1
root 3169 1744 0 00:01 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# kill 2786
[root@hadoop100 ~]# ps -ef | grep sshd
root 1087 1 0 6月11 ? 00:00:00 /usr/sbin/sshd -D
root 1658 1087 0 6月11 ? 00:00:02 sshd: root@pts/0
root 3225 1087 9 00:03 ? 00:00:01 sshd: langchao [priv]
langchao 3231 3225 0 00:03 ? 00:00:00 sshd: langchao@pts/1
root 3279 1744 0 00:03 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# kill 1658
Connection closing...Socket close.
[root@hadoop100 ~]# systemctl status sshd
...停止
[root@hadoop100 ~]# systemctl start sshd
[root@hadoop100 ~]# systemctl status sshd
...启动
[root@hadoop100 ~]# ps -ef | grep sshd
root 3308 1 0 00:05 ? 00:00:01 sshd: root@pts/0
root 3413 1 0 00:09 ? 00:00:00 /usr/sbin/sshd -D
root 3446 3413 18 00:10 ? 00:00:01 sshd: langchao [priv]
langchao 3451 3446 0 00:10 ? 00:00:00 sshd: langchao@pts/1
root 3500 3316 0 00:10 pts/0 00:00:00 grep --color=auto sshd
[root@hadoop100 ~]# ps -ef | grep 3316
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
root 3509 3316 0 00:11 pts/0 00:00:00 ps -ef
root 3510 3316 0 00:11 pts/0 00:00:00 grep --color=auto 3316
[root@hadoop100 ~]#
[root@hadoop100 ~]# ps -ef | grep bash
root 737 1 0 6月11 ? 00:00:02 /bin/bash /usr/sbin/ksmtuned
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
langchao 3455 3451 0 00:10 pts/1 00:00:00 -bash
root 3544 3537 3 00:14 pts/2 00:00:00 -bash
root 3588 3316 0 00:14 pts/0 00:00:00 grep --color=auto bash
[root@hadoop100 ~]# kill 3455
[root@hadoop100 ~]# ps -ef | grep bash
root 737 1 0 6月11 ? 00:00:02 /bin/bash /usr/sbin/ksmtuned
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
langchao 3455 3451 0 00:10 pts/1 00:00:00 -bash
root 3544 3537 0 00:14 pts/2 00:00:00 -bash
root 3607 3316 0 00:16 pts/0 00:00:00 grep --color=auto bash
[root@hadoop100 ~]# kill -l
...
[root@hadoop100 ~]# kill -9 3455
[root@hadoop100 ~]# ps -ef | grep bash
root 737 1 0 6月11 ? 00:00:02 /bin/bash /usr/sbin/ksmtuned
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
root 3544 3537 0 00:14 pts/2 00:00:00 -bash
root 3628 3316 0 00:17 pts/0 00:00:00 grep --color=auto bash
[root@hadoop100 ~]# ps -ef | grep bash
root 737 1 0 6月11 ? 00:00:02 /bin/bash /usr/sbin/ksmtuned
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
root 3544 3537 0 00:14 pts/2 00:00:00 -bash
langchao 3649 3644 0 00:18 pts/1 00:00:00 -bash
root 3702 3316 0 00:19 pts/0 00:00:00 grep --color=auto bash
[root@hadoop100 ~]# kill -9 3544
[root@hadoop100 ~]# ps -ef | grep bash
root 737 1 0 6月11 ? 00:00:02 /bin/bash /usr/sbin/ksmtuned
root 3316 3308 0 00:05 pts/0 00:00:00 -bash
langchao 3649 3644 0 00:18 pts/1 00:00:00 -bash
root 3731 3316 0 00:20 pts/0 00:00:00 grep --color=auto bash
[root@hadoop100 ~]# kill -9 3316
[root@hadoop100 ~]# killall sshd # 未成年人禁止使用
shell连不上了,打开虚拟机终端输入指令
[root@hadoop100 ~]# systemctl start sshd
4、查看进程树
[root@hadoop100 ~]# pstree
...
[root@hadoop100 ~]# pstree | less
[root@hadoop100 ~]# pstree -p | less
[root@hadoop100 ~]# pstree -u | less
5、top 实时监控系统进程状态
[root@hadoop100 ~]# top # 实时刷新,默认三秒一次
[root@hadoop100 ~]# top -d 1 # 设置为一秒刷新一次
6、netstat 显示网络状态和端口占用信息
语法
netstat -anp | grep 进程号 ——(功能描述:查看进程网络信息)
netstat -nlp |grep 端口号——(功能描述:查看网络端口号占用情况)
选项
-a —— 显示所有正在监听(listen)和未监听的套接字(socket)
-n —— 拒绝显示别名,能显示数字的全部转化成数字
-l —— 仅列出在监听的服务状态
-p —— 表示显示哪个进程在调用
[root@hadoop100 ~]# ifconfig
[root@hadoop100 ~]# ping 192.168.122.1
[root@hadoop100 ~]# netstat -nap
crontab 系统定时任务
1、crontab 服务管理
-e —— 编辑crontab定时任务
-l —— 查询crontab 任务
-r —— 删除当前用户所有的crontab任务
[root@hadoop100 ~]# systemctl status crond
[root@hadoop100 ~]# crontab -l
no crontab for root
[root@hadoop100 ~]# crontab -e
no crontab for root - using an empty one
[1]+ 已停止 crontab -e
[root@hadoop100 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@hadoop100 ~]# ls
anaconda-ks.cfg info initial-setup-ks.cfg myroot.zip temp.tar.gz xzhdx.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@hadoop100 ~]# cat hello
cat: hello: 没有那个文件或目录
[root@hadoop100 ~]# crontab -e
crontab: installing new crontab
[root@hadoop100 ~]# cat hello
hello,world
[root@hadoop100 ~]# tail -f hello
hello,world
hello, world
^C
[root@hadoop100 ~]# crontab -l
*/1 * * * * echo "hello, world" >> /root/hello
[root@hadoop100 ~]# crontab -r
[root@hadoop100 ~]# crontab -l
no crontab for root
软件包管理
一、RPM概述
RPM(RedHat Package Manager),RedHat软件包管理工具,类似Windows里面的setup.exe
是Linux这系列操作系统里面的打包安装工具,它虽然是RedHat的标志,但是理念是通用的。
1、RPM查询命令(rpm -qa)
[root@hadoop100 ~]# rpm -qa
[root@hadoop100 ~]# rpm -qa | grep firefox
firefox-91.9.0-1.el7.centos.x86_64
[root@hadoop100 ~]# rpm -qi firefox
Name : firefox
Version : 91.9.0
Release : 1.el7.centos
Architecture: x86_64
Install Date: 2022年06月01日 星期三 17时07分33秒
Group : Unspecified
Size : 275744152
License : MPLv1.1 or GPLv2+ or LGPLv2+
Signature : RSA/SHA256, 2022年05月06日 星期五 21时36分49秒, Key ID 24c6a8a7f4a80eb5
Source RPM : firefox-91.9.0-1.el7.centos.src.rpm
Build Date : 2022年05月06日 星期五 00时58分23秒
Build Host : x86-02.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : https://www.mozilla.org/firefox/
Summary : Mozilla Firefox Web browser
Description :
Mozilla Firefox is an open-source web browser, designed for standards
compliance, performance and portability
2、RPM卸载命令(rpm -e)
[root@hadoop100 ~]# rpm -e firefox
[root@hadoop100 ~]# rpm -qi firefox
未安装软件包 firefox
3、RPM安装命令(rpm -ivh)
选项
-i —— install,安装
-v —— - -verbose,显示详细信息
-h —— - -hash,进度条
– nodeps —— 安装前不检查依赖
[root@hadoop100 Packages]# rpm -ivh firefox-68.10.0-1.el7.centos.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:firefox-68.10.0-1.el7.centos ################################# [100%]
[root@hadoop100 Packages]# rpm -qi firefox
二、YUM 仓库配置
1、YUM 概述
YUM(全称为 Yellow dog Updater,Modified)是一个在Fedora和RedHat以及CenOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖关系,并且一次安装所有依赖的软件包,无须繁琐地一次 次下载、安装。
2、YUM的常用命令
[root@hadoop100 Packages]# yum remove firefox
[root@hadoop100 Packages]# yum -y install firefox
3、修改网络YUM源
省略64集结尾