1. vim 练习
命令窗口直接输入vimtutor 进行练习。
vimtutor
2. 用户管理
添加用户
useradd tom
useradd -d 指定目录 新的用户名
用户密码
passwd tom
删除用户
//不删除用户目录
userdel tom
// 删除用户目录
userdel -r tom
查询用户信息
id tom
切换用户
su root
查看当前登录用户
[root@hspedu ~]# whoami
root
[root@hspedu ~]# who am i
root pts/0 2022-04-01 08:40 (192.168.247.1)
新增组
groupadd 用户名
删除组
groupdel 用户名
修改组
usermod -g 用户组名 用户名
用户和组的相关文件
/etc/passwd: 记录用户信息
/etc/shodow: 记录口令
/etc/group: 记录用户组
3. 实用指令
3.0 运行级别
可以查看 cnetos7 /etc/inittab 文件中
0 关机
1 找回用户密码
2 多用户状态下没有网络
3 多用户状态有网络
4 系统未使用保留给用户
5 图形界面
6 系统重启
关机系统
[root@hspedu ~]# init 0
重启系统
[root@hspedu ~]# init 6
获取默认运行级别,此时为5
[root@hspedu ~]# systemctl get-default
graphical.target
改为运行级别 3
[root@hspedu ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
重启
[root@hspedu ~]# init 6
改为运行级别5
[root@hspedu ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
重启
[root@hspedu ~]# reboot
3.1找回root 用户密码
- 首先,启动系统,进入开机界面,在界面中按“e”进入编辑界面。如图
- 进入编辑界面,使用键盘上的上下键把光标往下移动,找到以““Linux16”开头内容所在的行数”,在行的最后面输入:init=/bin/sh。如图
- 接着,输入完成后,直接按快捷键:Ctrl+x 进入单用户模式。
- 接着,在光标闪烁的位置中输入:mount -o remount,rw /(注意:各个单词间有空格),完成后按键盘的回车键(Enter)。如图
- 在新的一行最后面输入:passwd, 完成后按键盘的回车键(Enter)。输入密码,然后再次确认密码即可(韩顺平提示: **密码长度最好8****位以上,**但不是必须的), 密码修改成功后,会显示passwd…的样式,说明密码修改成功
- 接着,在鼠标闪烁的位置中(最后一行中)输入:touch /.autorelabel(注意:touch与 /后面有一个空格),完成后按键盘的回车键(Enter)
- 继续在光标闪烁的位置中,输入:exec /sbin/init(注意:exec与 /后面有一个空格),完成后按键盘的回车键(Enter),等待系统自动修改密码(韩顺平提示:这个过程时间可能有点长,耐心等待),完成后,系统会自动重启, 新的密码生效了
3.2 文件目录常用指令
ls 指令
ls -a
ls -l
cd 指令
cd ~
cd .
cd …
cd /home
mkdr 指令
创建多级目录
mkdir -p /home/animal/tiger
cp 指令
复制文件
cp hello.txt /home/bbb
复制整个文件夹
cp -r /home/bbb /opt
rm 指令
rm -rf 删除文件或目录
删除整个文件不提示
rm -f
mv 指令
重命名
mv oldFileName newFileName
复制文件到指定目录
mv 文件路径 目标文件路径
cat 指令
查看文件
cat /etc/profile
-n 显示行号
使用管道命令组合
cat /etc/profile | more
less 指令
查看大文件
less 倚天屠龙记.txt
head 指令
默认显示前10行
head /etc/profile
显示前5行
head -n 5 /etc/profile
tail 指令
显示后10行
tail /etc/profile
显示前5行
tail -n 5 /etc/profile
监控文件
tail -f /home/mydate.txt
>
和 >>
指令
案例一,将home 文件下的目录写入/home/info.txt
ls -l /home > /home/info.txt
案例二,将当前日历信息追加到一个文件中
ls -l /home > /home/info.txt
再次执行
ls -l /home > /home/info.txt
文件内容增量追加
案例三快速复制文件
cat /etc/profile > /home/myprofile
软连接
相当于win 中的快捷方式
在home目录建立一个软连接指向 root 目录
ln -s /root /home/myroot
使用软连接
cd /home/myroot
删除软连接
rm /home/myroot
history 指令
history
histroy 5
执行曾经执行过的命令
通过history 查找命令找到行号
!行号
如下
!4
3.3 日期指令
date日期指令
date
date +%Y
date +%m
date +%d
date “+%Y-%m-%d %H:%M:%S”
重置系统日期指令
date -s “2021-2-28 20:20:20”
cal 日历指令
查看当月指令
cal
查看某年日历
cal 2022
3.4 搜索查找类
find 指令
案例一 查找/home 目录下名字为hello.txt 的文件
find /home -name hello.txt
案例二 查找/opt 目录下用户名为root 所有的文件
find /opt -user root
find /opt -user root | more
案例三 查找整个 linux 系统下大于 200M 的文件(+n 大于 -n 小于 n 等于, 单位有 k,M,G)
find / -size +200M
locate 指令
快速定位文件路径
第一次使用
updatedb
locate 要查找的文件
which 指令
可以查看某个指令在哪一个目录下
which ls
grep 指令和 管道符号 |
案例 1: 请在 hello.txt 文件中,查找"yes"所在行,并且显示行号
写法一
cat /home/hello.txt | grep -n “yes”
写法二
grep -n “yes” /home/hello.txt
3.5 压缩和解压缩
gzip 和 gunzip 压缩单个文件
压缩后原文件消失
gzip /home/hello.txt
解压后解压文件消失
gunzip /home/hello.txt.gz
zip 和 unzip
压缩
zip -r myhome.zip /home
解压
unzip -d /opt/tmp myhome.zip
tar 指令
-c :产生tar 打包文件
-v 显示详细信息
-f 指定压缩文件名
-z 打包同时压缩
-x 解包.tar文件
案例一: 把/home/cat.txt和/home/pig.txt 压缩成pc.tar.gz
tar -zcvf pc.tar.gz /home/pig.txt /home/cat.txt
案例二:将/home 文件夹压缩成 myhome.tar.gz
tar -zcvf myhome.tar.gz /home/
案例三 将pc.tar.gz 解压到当前目录
tar -xzvf pc.tar.gz
案例四将 myhome.tar.gz 解压到/opt/tmp
mkdir /opt/tmp
tar -xzvf myhome.tar.gz -C /opt/tmp
4. 组管理和权限管理
chmod 指令
chmod u=rwx,g=rx,o=x aa.txt
chmod 751 abc
chmod g+r+w 增加权限
chown 指令
新的所有者 文件/目录
chown tom abc
chown tom:tom abc
chown -R tom /home/test
chgrp 指令
新的组 文件/目录
chgrp tom abc
groupadd shaolin
chgrp -R shaolin /home/test
usermod 指令
改变用户所在组
usermod -g 新组 用户名
对目录而言
x: 表示可以进入到该目录, 比如 cd
r: 表示可以 ls , 将目录的内容显示
w: 表示可以在该目录,删除或者创建文件
5.定时任务调度
crontab 指令
案例一: 设置一个简单的定时调度任务
crontab –e
*/1 * * * * ls –l /home/ > /tmp/to.txt
每隔一分钟把当前日期追加打mydate 文件中
案例二
crontab –e
*/1 * * * * date >> /tmp/mydate
案例三
在home/目录下my.sh
date >> /home/mycal
cal >> /home/mycal
添加执行权限
chmod u+x my.sh
定时调度
crontab –e
每隔一分钟执行一次
*/1 * * * * /home/my.sh
查看输出结果
cat /home/mycal
案例四:每天凌晨2点定时备份mysql 数据库
crontab -e
0 2 * * * mysqldump -u root -proot testdb > /home/db.bak
查看任务调度
crontab -l
终止任务
crontab -r
at 指令
一次性的定时计划任务
- at 命令是一次性定时计划任务,at 的守护进程 atd 会以后台模式运行,检查作业队列来运行。
- 默认情况下,atd 守护进程每 60 秒检查作业队列,有作业时,会检查作业运行时间,如果时间与当前时间匹配,则运行此作业。
- at 命令是一次性定时计划任务,执行完一个任务后不再执行此任务了
- 在使用 at 命令的时候,一定要保证 atd 进程的启动 , 可以使用相关指令来查看
ps -ef | grep atd
//可以检测 atd 是否在运行
案例一
[root@hspedu ~]# at now + 2 minutes
at> date > /root/date001.log<EOT>
job 6 at Thu Apr 7 20:11:00 2022
查看at 定时任务 atq | at -l
at -l
移除at 定时任务 atrm | at -d
at -d 任务编号
6. 磁盘分区挂载
查看磁盘
[root@hspedu ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 59513321-8031-4f3a-982e-4bbd1dfa60eb /boot
├─sda2 swap 127e60e3-ac0a-488e-b26f-990ba6a27bf3 [SWAP]
└─sda3 ext4 e3f97ff3-686c-413c-afe7-5e136e024487 /
sr0
1.新增硬盘
在虚拟机中添加一个1G硬盘。并重启系统,再次查看
[root@hspedu ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 59513321-8031-4f3a-982e-4bbd1dfa60eb /boot
├─sda2 swap 127e60e3-ac0a-488e-b26f-990ba6a27bf3 [SWAP]
└─sda3 ext4 e3f97ff3-686c-413c-afe7-5e136e024487 /
sdb
sr0
2 增加分区
[root@hspedu dev]#
fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。Device does not contain a recognized partition table
使用磁盘标识符 0x5314d9f1 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):
m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)命令(输入 m 获取帮助):
n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):p
分区号 (1-4,默认 1):1
起始 扇区 (2048-2097151,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-2097151,默认为 2097151):
将使用默认值 2097151
分区 1 已设置为 Linux 类型,大小设为 1023 MiB命令(输入 m 获取帮助):
命令(输入 m 获取帮助):w
The partition table has been altered!Calling ioctl() to re-read partition table.
正在同步磁盘。
3 格式化分区
mkfs -t ext4 /dev/sdb1
4 挂载分区
命令行挂载
mkdir /newdisk
mount /dev/sdb1 /newdisk
用命令行挂载,重启后会失效
永久挂载
vim /etc/fstab
磁盘情况查看
df -h
查询指定目录的磁盘占用情况
du -h
查询指定目录的磁盘占用情况,默认为当前目录
-s 指定目录占用大小汇总
-h 带计量单位
-a 含文件
–max-depth=1
子目录深度
-c 列出明细的同时,增加汇总值
应用实例
查询 /opt 目录的磁盘占用情况,深度为 1
du -hac --max-depth=1 /opt
磁盘情况-工作实用指令
统计/opt 文件夹下文件的个数
ls -l /opt | grep "^-" | wc -l
统计/opt 文件夹下目录的个数
[root@hspedu opt]#
ls -l /opt | grep "^d" | wc -l
4
统计/opt 文件夹下文件的个数,包括子文件夹里的
[root@hspedu opt]#
ls -lR /opt | grep "^-" | wc -l
1543
统计/opt 文件夹下目录的个数,包括子文件夹里的
[root@hspedu opt]#
ls -lR /opt | grep "^d" | wc -l
517
以树状显示目录结构 tree 目录 , 注意,如果没有 tree ,则使用 yum install tree 安装
tree /home
7 网络配置
window ipconfig
liunx ifconfig
虚拟机ip 192.168.247.129
V8 ip 192.168.247.1
指定虚拟机ip
vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
IPADDR=192.168.247.129
GATEWAY=192.168.247.2
DNS1=192.168.247.2
在配置一下虚拟机的
重启网络服务或者重启系统生效
service network restart
reboot
设置主机名
查看主机名
[root@hspedu ~]# hostname
hspedu
修改主机名
vim /etc/hostname
编辑主机名
hspedu001
reboot
配置hosts 映射
Liunx 主机名 hspedu001
windows 配置host 映射
C:\Windows\System32\drivers\etc\hosts
192.168.247.129 hspedu001
Liunx 配置host 映射
vim /etc/hosts
添加 主机名随便起,
192.168.247.1 zhuji
对应设置文件里的内容(关闭window 防火墙的前提下能ping通)
ping zhuji
DNS
应用实例: 用户在浏览器输入了 www.baidu.com
1 浏览器先检查浏览器缓存中有没有该域名解析 IP 地址,有就先调用这个 IP 完成解析;如果没有,就检查 DNS 解析
器缓存,如果有直接返回 IP 完成解析。这两个缓存,可以理解为 本地解析器缓存
2 一般来说,当电脑第一次成功访问某一网站后,在一定时间内,浏览器或操作系统会缓存他的 IP 地址(DNS 解析记
录)
如 在 cmd 窗口中输入
ipconfig /displaydns
//DNS 域名解析缓存ipconfig /flushdns
//手动清理 dns 缓存
3 如果本地解析器缓存没有找到对应映射,检查系统中 hosts 文件中有没有配置对应的域名 IP 映射,如果有,则完成
解析并返回。
4 如果 本地 DNS 解析器缓存 和 hosts 文件 中均没有找到对应的 IP,则到域名服务 DNS 进行解析域
8 进程管理
ps 指令
ps -aux | more
==================ps aux 或 lax 输出的解释============
au(x) 输出格式 :
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
USER: 进程所有者
PID: 进程ID
%CPU: 占用的 CPU 使用率
%MEM: 占用的内存使用率
VSZ: 占用的虚拟内存大小
RSS: 占用的内存大小
TTY: 终端的次要装置号码 (minor device number of tty)
STAT: 进程状态:
START: 启动进程的时间;
TIME: 进程消耗CPU的时间;
COMMAND: 命令的名称和参数;
STAT
D 不可中断 uninterruptible sleep (usually IO)
R 运行 runnable (on run queue)
S 中断 sleeping
T 停止 traced or stopped
Z 僵死 a defunct (”zombie”) process
ps -ef | more
终止进程
ps -ef | grep 进程号
kill 进程号
killall 进程号
- 踢掉某个非法登录用户
[root@hspedu001 ~]# ps -aux | grep sshd
root 14929 1.4 0.1 156636 5424 ? Ss 12:04 0:00 sshd: tom [priv]
[root@hspedu001 ~]# kill 14929
- 终止远程登录服务
ps -ef | grep sshd
root 12736 0.0 0.1 112756 4312 ? Ss 11:46 0:00 /usr/sbin/sshd -D
kill 12736
重新开启远程登录服务
/bin/systemctl start sshd.service
- 终止多个gredit
killall gedit
- 强制杀掉一个终端
ps -ef | grep bash
kill -9 bash对应的进程号
pstree 指令
pstree -p
pstree -u
9 服务管理
service 指令
service network status
service network stop
service network start
systemctl 指令
查看systemctl 的服务
ls -l /usr/lib/systemd/system
设置服务自启动状态
systemctl list-unit-files [ | grep 服务名] (查看服务开机启动状态, grep 可以进行过滤)
systemctl list-unit-files | grep firewall
systemctl is-enabled 服务名 (查询某个服务是否是自启动的)
防火墙
systemctl status firewalld
systemctl stop firewalld
systemctl start firewalld
systemctl restart firewalld
这些命令是临时的,重启系统后恢复之前的状态。
要想永久启动或者关闭防火墙。
systemctl enable 服务名 (设置服务开机启动)
systemctl disable 服务名 (关闭服务开机启动)
防火墙开放端口
firewall-cmd --permanent --add-port=端口号/协议号
如果不知道协议号
查看端口协议号
netstat -anp | grep 端口号
firewall-cmd --permanent --add-port=111/tcp
移除端口
firewall-cmd --permanent --remove-port=111/tcp
打开和关闭都需要重新载入才能生效
firewall-cmd --reload
查询开端口是否开放
firewall-cmd --query-port=111/tcp
动态监控
top 指令
每隔3s 刷新一次 输入小写u 输入用户名监控用户状态
top
案例一监控用户
top
输入小写u 输入用户名如tom
案例二 终止指定进程,如tom 的登录
k:然后输入“k”回车,再输入要结束的进程 ID 号
案例三
每10s 刷新一次
top -d 10
网络监控
netstat 指令
netstat -anp | grep sshd
ping 主机ip 检测联通
10 Rpm 管理和 Yum 管理
Rpm
查看是否安装firefox
rpm -qa | grep firefox
查询软件包信息
rpm -qi firefox
查询软件包中的文件
rpm -ql firefox
查询文件所属软件包
rpm -qf /etc/passwd
rpm -qf /root/install.log
查看安装后文件安装在哪里
rpm -ql | grep firefox
卸载软件包
rpm -e RPM 包的名称
强制卸载软件包
rpm -e --nodeps 软件包
安装软件
rpm -ivh 软件包名称
yum
yum list | grep 软件名称
安装
yum install 软件名称