五、Linux的其他常用操作

五、Linux的其他常用操作

5.1 虚拟机的克隆

步骤1)先克隆机器

先关掉要克隆的机器,然后
右键点击机器名称--->管理-->克隆-->虚拟机的当前状态-->创建完整克隆-->虚拟机名称--完成

步骤2)修改IP和主机名以及映射文件,然后重启

[root@qianfeng01 ~]# hostnamectl set-hostname qianfeng02
[root@qianfeng01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@qianfeng01 ~]# vim /etc/hosts
[root@qianfeng01 ~]# reboot

步骤3)测试是否可以通信

[root@qianfeng01 ~]# ping qianfeng02
[root@qianfeng02 ~]# ping qianfeng01

5.2 SCP指令

作用:用于远程拷贝文件
语法:  scp [-r] filename....  远程用户名@远程机器IP:pathname 

案例演示:

[root@qianfeng01 ~]# echo "gaoyuanyuan is my friend" > nvshen.txt
​
eg1:将nvshen.txt文件以michael的身份拷贝到qianfeng02机器上的家里
[root@qianfeng01 ~]# scp nvshen.txt michael@qianfeng02:/home/michael/
​
eg2:  将nvshen.txt文件以root身份拷贝到qianfeng02机器上的家里
[root@qianfeng01 ~]# scp nvshen.txt root@qianfeng02:~/
​
eg3:  因为qianfeng02的root用户和当前机器的用户都是root。因此可以简写
[root@qianfeng01 ~]# scp nvshen.txt qianfeng02:~/
​
eg4:  如果拷贝的位置和当前位置一致,可以使用$PWD
[root@qianfeng01 ~]# scp nvshen.txt qianfeng02:$PWD

5.3 免密登录认证(重要)

5.3.1 准备工作

假如机器A想要免密登录机器B,那么需要提前在机器A上生成一对秘钥文件:公钥和私钥文件。而且机器A的公钥文件里的公钥信息还需要拷贝到机器B的连接用户的主目录下的隐藏目录.ssh/下的authoried_keys里
  1. 先在机器A上生成秘钥

[root@qianfeng01 ~]#  ssh-keygen -t rsa    一路回车下去
​
说明:最后会在当前用户的家里的.ssh隐藏目录下产生两个文件
     id_rsa:   私钥文件
     id_rsa.pub  : 公钥文件

2、将机器A的公钥信息,拷贝到机器B的相关用户的家里的隐藏目录.ssh下的authoried_keys里

注意:先确认机器B里是否有.ssh目录,如果没有需要提前创建
     在机器B上,执行 ssh localhost, 就会产生.ssh目录
[root@qianfeng01 .ssh]# scp id_rsa.pub qianfeng02:~/.ssh/authoried_keys       

5.3.2 免密登录认证的原理流程

认证原理(步骤):参考图中的1,2,3,4,5,6,7

 

5.3.3 准备工作的简化方式

1. 生成秘钥
[root@qianfeng01 ~]#  ssh-keygen -t rsa    一路回车下去
2. 将公钥拷贝到远程机器上
[root@qianfeng01 ~]#  ssh-copy-id -i qianfeng02
ssh-copy-id的作用:将公钥文件拷贝到远程机器的.ssh隐藏目录下,并更名成authorized_keys文件

问题:集群可能有多台机器,都要进行免密登录认证,如果每个机器都有自己的一套秘钥,拷贝赋值非常麻烦,所以这些机器可以用同一套秘钥。

1. 生成秘钥
[root@qianfeng01 ~]#  ssh-keygen -t rsa    一路回车下去
2. 对自己进行免密登录认证
[root@qianfeng01 ~]#  ssh-copy-id -i qianfeng01
3. 其他机器如果想要这一套钥匙,则需要拷贝id_rsa,id_rsa.pub,authorized_keys到相应的目录下
[root@qianfeng01 ~]#  scp ~/.ssh/{id*,authorized_keys} qianfeng02:~/.ssh/

5.4 定时器crontab(熟悉)

5.4.1 crontab的简介

- 在Linux中,周期性的执行任务一般由cron这个守护进程来处理,它是一个linux下 的定时任务执行工具,可以在无需人工干预的情况下运行作业。
   [ps -ef|grep cron]
- cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间。
- cron的配置文件称为“crontab”,是“cron table”的简写。

5.4.2 cron服务

systemctl start crond   //临时启动服务
systemctl stop crond     //临时关闭服务
systemctl restart crond //重启服务
systemctl disable crond //禁用启动项
systemctl enable crond   //启动开启启动项
systemctl stutas crond  //查看服务状态 

5.4.3 cron的配置文件位置:

1. /var/spool/cron/ 
2. 说明这个目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名,比如tom建的crontab任务对应的文件就是/var/spool/cron/tom。一般一个用户最多只有一个crontab文件。

5.4.4 crontab命令格式

作用:用于生成cron进程所需要的crontab文件 
格式:crontab  [-u username] -e
​
-u:用于指定其他用户的定时任务设置

5.4.5 查看定时器的格式

[root@qianfeng01 cron]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
​
# For details see man 4 crontabs
​
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
​
​
即:
格式如下:
*   *   *   *   *   user-name command to be executed
共有六部分组成,分别表示:
分   时   日   月   星期  要运行的命令
解析:
    minute:     一小时中的哪一分钟 [0~59] 
    hour:       一天中的哪个小时 [0~23] 
    day:        一月中的哪一天 [1~31] 
    month:      一年中的哪一月 [1~12] 
    week:       一周中的哪一天 [0~6] 0表示星期天 
    commands:   执行的命令
​
书写注意事项 
    1.全都不能为空,必须填入,不知道的值使用通配符*表示任何时间 
    2.每个时间字段都可以指定多个值,不连续的值用,间隔,连续的值用-间隔。 
    3.命令应该给出绝对路径 
    4.用户必须具有运行所对应的命令或程序的权限
    5. */num   表示频率

5.4.6 常用案例演示

1. 每天早上6点 
    0 6 * * * echo "Good morning." >> /tmp/test.txt 
    //注意 如果不进行追加 ,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
2. 每两个小时 
    0 */2 * * * echo "Have a break now." >> /tmp/test.txt  
3. 晚上11点到早上8点之间每两个小时和早上八点 
    0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt
4. 周一到周五下午,5点半提醒学生15分钟后关机
    30 17 * * 1-5 /usr/bin/wall < /etc/issue
    45 17 * * 1-5 /sbin/shutdown -h now
5. 学校的计划任务, 12点到14点,每两分钟,检查apache服务是否启动
    */2 12-14 * 3-6,9-12 1-5
6. 每月1、10、22日的4:45运行/apps/bin目录下的backup.sh
    45 4 1,10,22 * * /apps/bin/backup.sh
7. 每周六、周日的 1 : 10运行一个find命令
    10 1 * * 6,0 /bin/find -name "core" -exec rm {} \;
8. 在每天 18:00至23 :00之间每隔30分钟运行/apps/bin目录下的dbcheck.sh
    0,30 18-23 * * * /apps/bin/dbcheck.sh
9.每星期六的 11:00 pm运行/apps/bin目录下的qtrend.sh
    0 23 * * 6 /apps/bin/qtrend.sh

5.5 时间同步服务器的搭建

5.5.1 时间同步

在生产环境中的集群里,每台机器都有自己的时间,有的时候机器可能宕机,恢复后,时间不准确,也就是和其他机器时间不一致(不同步)。有些在集群上运行的作业对整个集群上所有机器的时间是有要求的,比如hbase集群要求所有机器的时间差不能超出30s。因此需要同步时间

5.5.2 如何同步时间

使用ntpdate指令进行同步时间,前提是只能同步时间服务器的时间。

用法: ntpdate -u timeserver
​
网络上的常用时间服务器:
阿里云时间服务器:ntp1.aliyun.com
微软时间服务器:time.windows.com
​
如果ntpdate命令不存在,需要安装ntpdate.x86_64
[root@qianfeng01 ~]# yum -y install ntpdate.x86_64

如果生产环境中的集群没有连接外网,那么则需要搭建一个局域网的时间服务器

5.5.3 搭建局域网时间服务器

1. 选择集群中的某一台机器qianfeng01作为时间服务器
2. 保证这台服务器安装了ntp.x86_64。
    [root@qianfeng01 ~]# yum list installed | grep ntp    查询
    [root@qianfeng01 ~]# yum -y install ntp.x86_64
3. 保证ntpd 服务运行......(开机自启动)
    [root@qianfeng01 ~]# systemctl start ntpd
    [root@qianfeng01 ~]# systemctl enable ntpd
    [root@qianfeng01 ~]# systemctl status ntpd
4. 配置相应文件:vi /etc/ntp.conf
    ......省略....
    restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap    添加集群中的网络段位
    server 127.127.1.0     -qianfeng01作为服务器
   
   注意:
    -- 需要重启机器,或者重启服务 
    -- 服务器的防火墙要关闭
​
5. 其他机器要保证安装ntpdate.x86_64
6. 在其他机器上进行测试,是否可以同步
   ntpdate -u qianfeng01
7. 其他机器要使用root定义定时器
     */1 * * * * /usr/sbin/ntpdate -u qianfeng01

5.6 YUM源的更换

本地的yum信息在/etc/yum.repo.d/下。

[root@qianfeng01 ~]# cd /etc/yum.repos.d/
[root@qianfeng01 yum.repos.d]# ll
总用量 32
-rw-r--r--. 1 root root 1664 9月   5 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 9月   5 2019 CentOS-CR.repo
-rw-r--r--. 1 root root  649 9月   5 2019 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 9月   5 2019 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 9月   5 2019 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 9月   5 2019 CentOS-Sources.repo
-rw-r--r--. 1 root root 6639 9月   5 2019 CentOS-Vault.repo

5.6.1 网络yum源的更换

由于CentOS系统默认的网络YUM源的地址是国外的,网速相对较慢,因此可以更换成国内的YUM源,比如阿里云、网易的等

这里选择更换成阿里云的yum源

步骤1)确保wget指令或者使curl指令存在

如果不在,就安装以下
yum -y install wget* curl*

步骤2)备份原有的yum源

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

步骤3)下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
​
或者是:
​
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

步骤4)清理原有的缓存

yum clean all

步骤5)重新构建缓存

yum makecache

5.6.2 本地yum源的搭建

1.确保虚拟机连接了DVD.iso影像文件,开机后,此文件位于/dev/cdrom
​
2.创建挂载目录/mnt/dvd
[root@scott ~]# mkdir /mnt/dvd
​
3.将映像文件挂载到/mnt/dvd下, 软件都在Packages目录下
[root@scott ~]# cd /mnt/dvd
[root@scott dvd]# mount -t iso9660  /dev/cdrom -o loop /mnt/dvd
[root@scott dvd]# ll
​
-t:用来指定文件类型,   光盘的类型是:iso9660
-o: 用来指定挂载方式,  loop 用来把一个文件当成硬盘分区挂接上系统 
​
4.将原有的yum源设置为失效状态(修改后缀即可)
[root@scott Packages]# cd /etc/yum.repos.d/
[root@scott yum.repos.d]# rename .repo .repo.bak  ./*.repo
​
解析:rename  [要修改的扩展名] [修改后的扩展名] [源文件]
​
5.创建local.repo文件,配置本地yum源信息
[root@scott yum.repos.d]# vi local.repo
​
                        <-----------0表示禁用本资源库,1表示开启本资源库
​
6.清空yum源缓存
[root@scott yum.repos.d]# yum clean all
​
7.查询yum源软件列表
[root@scott yum.repos.d]# yum list all

注意:重启机器后,需要重写挂载,也就是重新执行mount指令。

5.6.3 局域网yum源的搭建

步骤1)选择局域网内的一台机器作为YUM源服务器,安装HTTP服务进程

[root@qianfeng01 ~]# systemctl status httpd
[root@scott yum.repos.d]# yum -y install httpd.x86_64

步骤2)启动httpd服务项

[root@qianfeng01 ~]# systemctl start httpd
[root@qianfeng01 ~]# systemctl enable httpd
[root@qianfeng01 ~]# systemctl status httpd

步骤3)配置HTTP服务管理YUM源目录

# HTTP服务进程管理的目录是/var/www/html
​
#在管理目录下,创建软连接,指向YUM源目录
]# ln -s /mnt/dvd /var/www/html/repo        <===都是用绝对路径,保没错

步骤4)其他机器只需要把自己本地YUM源的路径指向局域网YUM源地址即可

[root@qianfeng02 yum.repos.d]# rename .repo .repo.bak ./*.repo
[root@qianfeng02 yum.repos.d]# vi local.repo                #编辑文件,后缀必须是.repo
​
[local]                                 <===YUM源仓库名
name=localrepo                          <===昵称
baseurl=http://局域网IP/repo               <===局域网YUM仓库位置
enabled=1                               <===1 表示启用,0表示禁用
gpgcheck=1                              <===1 表示 校验
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7     <===校验钥匙
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值