linux学习之路

配置网络

vi /etc/sysconfig/network-scripts/ifcfg-ens33

systemctl restrat network.service

vim

在这里插入图片描述
编辑模式:

# 编辑模式
# 回到第一行
gg
# 回到最后一行
G

# 跳到第num行
num gg;
# 删除一行
dd

# 跳到下一个词
w

# 删除一个单词
dw
# 复制一行
yy
# 粘贴一行
 p
 # 复制一个单词
 yw

输入模式:
末行模式:

		: set nu #显示行号
		:wq!
		# 字符串查找,查找成功后按下n显示下一个查找结果
		: /xxx
		# 字符串替换,要求改行包含xxx字符串
		: s/xxx/liyuan
		# 指定行指定字符串全部替换
		: s/xxx/liyuan/g
		# 全局替换
		: g/xxx/s//liyuan/g

文件传输

(1) rz

# 注意需要远程客户端下载xhsell以支持该命令
yum install lrzsz -y
# 上传
rz
# 下载
sz

(2) scp

# 文件传输
scp [[user@]host1:]file1 [[user@]host2:]file2
# 文件夹传输
scp -r [[user@]host1:]dir [[user@]host2:]dir

文件占用空间

# 查看分区信息
df -h

# 查看指定目录大小
du -h --max-depth=1 /etc

文件解压缩

# 解压缩extract
tar -zxvf xxx.tar.gz
# 压缩compress
tar -zcvf  data.tar.gz /root/data

# 压缩
zip -r data.zip /root/data
# 解压缩
unzip data.zip

网络

# 查看网络状态信息
netstat 
 
# 查看与目标ip地址是否能够连通
ping

# 与目标指定端口能否连通
telnet [ip] [port]


# 防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service


# 查看防火墙状态
firewalld-cmd --state

# 重新载入配置,比如添加规则之后,需要执行此命令
firewalld-cmd --reload

# 列出支持的zone
firewalld-cmd --get-zones

# 列出支持的服务,在列表中的服务是放行的
firewall-cmd --get-services

# 查看ftp服务是否支持
firewall-cmd --query-service ftp

# 临时开放ftp服务
firewall-cmd --add-service=ftp

# 永久开放
firewall-cmd --add-service --permanent

# 永久移除ftp服务
firewall-cmd --remove-service=ftp --permanent 

# 永久添加80端口
firewall-cmd --add-port=80/tcp --permanent

# 永久移除public端口中的80端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent

加密算法

非对称加密

RSA
HTTPS(ssl)

对称加密

使用相同的密钥进行加解密;
AES,DES

主机间相互免密钥

将本机的公钥发送给从机,即可免密登录从机。

# 生成密钥
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
# 发送公钥给从机
ssh-copy-id -i ~/.ssh/id_rsa.pub root@x.x.x.x

# 自动加入ip
vim /etc/ssh/ssh_config
# 最后面添加
 StrictHostKeyChecking no
 UserKnownHostsFile /dev/null

日期时间

# 修改时间
date -s 11:11:11
date -s 2019-11-11
date -s '2019-11-11 11:11:11'
管道
# 将前面的结果作为参数传递给后面
head -10 /etc/profile | tail -l
重定向
# 改变数据输出的位置
# 将输出数据覆盖到目标文件
ll /opt > test

# 将输出数据追加到目标文件
# 错误输出,abcd目录不存在
ll /abcd 2> test

# 结合使用
ls /etc/abc >> lucky 2>&1

# 信息黑洞
ls /etc/abc >> /dev/null 2>&1

linux进程

ps -ef|grep nginx
ps -aux # 获取所有信息
ps -aux --sort -pcpu 
top
kill -12 {{PID}}

# 后台挂起
ping www.baidu.com >> baidu &
# 后台挂起一个应用,输出无论正确或错误信息
nohup ping www.baidu.com >> baidu 2>&1 &
jobs
环境变量
# 系统变量
cat /etc/profile

# 用户变量
cat $HOME/.base_profile

# 环境变量路径
echo $PATH

软件安装

# rpm redhat; install software
rpm -ivh jdk-7u67-linux-x64.rpm

# 查询软件
rpm -qa|grep jdk
rpm -q jdk
# 移除软件
rpm -e jdk-1.7.0_67-fcs.x86_64

export JAVA_HOME=/usr/java/jdk1.7.0_67
export PATH=$JAVA_HOME/bin:$PATH

yum
# 管理RPM包

# 更换yum源
cd /etc/yum.repo.d/

vi /etc/yum.repo.d/CentOS-Base.repo

# (1) 备份yum源
mv /etc/yum/repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# (2)获取yum源配置文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/CentOS-6.repo
# 或
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/CentOS-7.repo

yum clean all
yum makecache

# mysql config
set global validate_password.policy=LOW;
set global validate_password.length=6;
alter user 'root'@'localhost' identified by '123456' password expire never;
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
flush privileges;


# 设置远程登录
update user set set host='%' where user='root';
commit;
systemctl restart mysqld' 

linux 三剑客

# 使用指定规则切分文本
cut -d ':' -f1,2,3 /etc/passwd | grep root

# 按文本字母序进行排序
sort /etc/passwd
# 以:进行文本切分后使用第5列进行排序
sort -t ':' -k5
cut -d ':' -f1 /etc/passwd | sort

# 使用:切分每一行后取出第3,4,5列,然后将其传输给cut
# 使用:切分每一行后使用第一列进行排序
# -r 表示逆序
# -n 按照数字进行排序
cut -d ':' -f3,4,5 /etc/passwd | sort -r ':' -k1 -r -n


# 统计单词数量行,单词数(按空格换行符区分),字符数
wc /etc/passwd

# 对文本进行搜索
# -n 显示行号
# -nvi 忽略大小写
# -E "[1-9]+" 正则匹配
ps -ef | grep -n xxx


# sed : stream editor 流编辑器
# 增,向person.txt文本中的第二行后添加如下字符串
sed '2a 111,liyuan2314,CEO' person.txt
# 向person.txt文本中的第二行插入如下字符串
sed '2a 111,liyuan2314,CEO' person.txt

# 单引号:将单引号中内容原样输出,阻止所有字符转义
# 双引号:会先解析双引号中内容,若双引号中有命令,需使用¥(命令或变量)
sed "2i $PATH" person.txt

# 增加多行
sed "2a 222,liyuan,AAA\n333,yuanxi,SSS" person.txt

范围匹配

命令解释
10{sed command}对第10行进行操作
10,20{sed commands}对[10,20]将进行操作
10,+20{sed command}对[10,30]进行操作
1~2{sed command}对1,3,5,7…进行操作
10,${sed command}对10到最后一行进行操作
/liyuan/{sed command}对匹配到liyuan的行进行操作
/liyuan/,/yuanxi/{sed command}对第一次匹配到liyuan 到 第一次匹配到yuaxi的行将那些操作
# 删
# 删除第二行
sed '2d' person.txt

# 删除[2,5]行
sed '2,5d'person.txt

# 删除匹配到liyuan的行
sed '/liyuan/d' person.txt

# 1~2 从1开始,每次+2
# 2~3 从2开始,每次+3

# seq 10 | sed -n '2~3p'

# 删除1,3,5,7....奇数行
sed '1~2d' person.txt


# 打印除包含liyuan字符串的行的所有行
sed '/liyuan/d' person.txt
# 改 change 
# 将第2行还未如下
sed '2c 333,abc,CER' person.txt


# 替换
# s : 第一处匹配
# g:每行
# -i 直接修改文件内容

sed -i 's/目标内容/替换内容/g' person.txt
# 目标内容可使用正则表达式
sed -i 's#目标内容#替换内容#g' person.txt

# 将第三行第一个遇到的0替换成9
sed '3s#0#9#' person.txt

# 将第三行全部0替换成9
sed '3s#0#9#g' person.txt


# 进行备份
# 在-i命令后接上.xxx,即可实现先备份后修改
sed -i.bak -r ...
# 查 print
# 查出第二行
sed -n '2p' person.txt

# -r 正则匹配
# 打印出et0的ip地址,使用第一次正则匹配括号中的内容替换整个正则匹配中的内容
ifconfig eth0 | sed -n '2p' | sed -r 's#^.*inet(.*)netmask.*$#\1#g'

# & 源字符串
sed -r 's#(.*),(.*),(.*)#& ---- \1 \2 \3' person.txt

# 将当前目录下某种类型文件的修改
find ./ -name "*_f" | sed -r 's#^.*liyuan\_(.*)\_f#liyuan\_\1.txt#g'

# 匹配包含liyuan或yuanxi字符串的行
sed -nr '/liyuan|yuanxi/p' person.txt
# 获取总行数
# = 显示行数
sed -n '$=' person.txt

# 为文本中每一行添加行号
sed '=' person.txt | sed 'N;s#\n# #'

# 取出不连续的行
sed -n '1p;5p;3p' person.txt

shell/bash

# hello.sh
#! /bin/bash
echo "Hello world!"

./hello.sh
sh hello.sh :使用sh执行文件时,会新开一个子进程执行命令
source hello.sh :使用source执行文件时会以当前进程来执行

export可以将当前进程的变量传递给子进程

kernal: Linux内核主要是为了和硬件打交道
bourne shell: sh
bourne again shell: bash

shell变量

#! /bin/bash
name="liyuan"
# for file in `ls ./ -al`
for file in $(ls ./ -al);do
	echo "$file"
done


# 定义变量
name=liyuan

# 删除变量
unset name


# 字符串
# 双引号中可以有变量
# 双引号中可以出现转义字符
# 单引号不会解析其内容

# 获取字符串长度
name="liyuan"
echo ${#name}


# 数组
# 定义数组
list=("l1" "l2" "l3" "l4")
# 获取数组长度
echo ${#list[@]}
# 获取数组第一个元素
echo ${list[1]}

# 参数传递
参数参数说明
$#传递到脚本的参数个数
$*以一个单字符串显示所有向脚本传递的参数
$$当前脚本运行的进程ID号
$!后台运行的最后一个进程的ID号
$?显示最后命令的推出状态,0表示没有错误
$0执行的文件名
a=10
b=20

if [ $a -ge $b ];then
	echo "a>b"
else 
	echo "a<b"

# 文件测试运算符
file="/etc/node/test.sh"
if [-r $file ];then
	echo "file can be read"
fi

if [ -w $file ];then
	echo "file can be written"
fi

if [ -x $file ];then
	echo "file can be executed"
fi

if [ -f $file ];then
	echo "normal file"
fi

if [ -d $file ];them
	echo "dir"
fi

if [ -s $file ];then
	echo "file not empty"
fi

if [ -e $file ];then
	echo "file exist"
fi
# 算术运算符
a=10
b=20

val=`expr $a + $b`
val=`expr` $b % $a

# 若本机不存在python
yum info python && yum install python -y


echo `pwd`
a=10
b=20
if [ $a -ge $b ];then
	echo "a>b"
elif [ $a -eq $b ];then
	echo "a=b"
else
	echo "a<b"
fi

read num
case $num in
	1) echo "1"
	;;
	2) echo "2"
	;;
	3) echo "3"
	;;
	*) echo "xxxx"
esac
# for 循环
for i in 1 2 3 4 5
do
	echo "The value is: "$i
done

for s in 'liyuan' 'yuanxi'
do
	echo $s
done

猜数字游戏

# 猜数字游戏
rand=$(($RANDOM%100))
num=-1
echo "rand: "$rand
while :
do
        read -p "input num: " num
#       read num
        if [ $num -eq $rand ];then
                echo "success!"
                break
        elif [ $num -gt $rand ];then
                echo "please input smaller!"
        else
                echo "please input larger!"
        fi
done

函数

# linux shell 可以用户自定义函数,然后在shell脚本中可以随便调用
# 函数的返回值通过$?来获取
sum(){
	read a
	read b
	return $(($a+$b))
}
sum
echo $?


# 参数
funcParams(){
	echo "number:"$#
	echo "num1: "${1}
	echo "num3: "${3}
}

funcParams 1 2 3 4 

系统任务设置

系统启动流程

  • (1)启动计算机硬件(BIOS)
    • 读取时间
    • 选择对应的启动模式(USB HDD EFI)
  • (2)若为Linux系统,则回去找/boot目录,引导该系统启动
  • (3)计算机系统开始启动,读取初始化配置文件
    • vim /etc/inittab
    • 启动时控制着计算机的运行级别runlevel
    • 默认runlevel为3启动对应的服务和组件
  • (4)开始引导公共的组件或服务
    • vim /etc/rc.d/rc.sysint
  • (5)开始加载对应runlevel的服务
    • K:关机时需要关闭的服务
    • S:启动时需要开启的服务
    • 数字代表开启或关闭的顺序
    • 所有文件均为软链接,链接地址为/etc/init.d
级别作用
0halt
1single user mode
2multiuser, without NFS
3full multiuser mode
4unused
5X11用户界面模式
6reboot

在这里插入图片描述
按照设置的级别,进入不同的目录启动系统,其中init.d为公共组件
在这里插入图片描述

# 通过为该文件添加可执行权限
# 使得该文件可以被系统启动时自动执行
# 我们可以在其中写代码使我们的代码被自动执行
chmod +x /etc/rc.d/rc.local

在这里插入图片描述

# 查看当前虚拟机的服务
chkconfig

定时任务

# 查看cron状态
systemctl status crond.service

# 添加任务,编辑当前任务列表
crontab -e
# 分 时 日 月 周 command
# * * * * * command
00 17-19 * * * command # 每天17,18,19点整运行命令
30 3,19,21 * * * command # 每天凌晨3点和晚上19,21点半执行命令
*/5 * * * * command # 每隔5min执行一次

10 1 * * 6,0 command # 每周六,日1:10分执行

0,30 18-23 * * * command # 每天18-23点之间每隔30分钟

* 23-7/1 * * * command # 每天晚上23点到早上7点每隔1小时

0 11 4 * mon-wed command # 每月4号与每周一到周三11点


# 时间格式化
date "+%Y%m%d%H%M%S"

# 删除定时任务
crontab -r
# 免密钥登录
# 宿主机生成密钥
ssh-keygen -t rsa
# 将本机公钥拷贝给其他机器
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.31.xxx

unpacking files

tar -xvf ${filename}

# preview
tar tzvf ${filename}
formatcommand
*.xzxz -d ${filename}
*.tartar -xvf ${filename}
*.bz2bzip2 -cd ${filename}
make 
# install the files in their proper directions
make install
# remove stable object files
make clean
# preview the build prcess
make -n

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值