ssh免密登录及踢除

本文详细介绍如何在两台主机间配置SSH免密登录。包括生成密钥对、传输公钥到目标主机及验证过程。此外,还介绍了如何禁止密码认证、强制使用密钥认证的方法。

主机A使用ssh登录到主机B

  • 1、主机A操作如下
[root@A .ssh]# ssh-keygen  #生成秘钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #直接回车,默认存放路径
Enter passphrase (empty for no passphrase):   #设置秘钥密码,直接回车代表无
Enter same passphrase again:   #再次设置秘钥密码,直接回车代表无
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:l5zUtKOC1v7BAeG2O7m64ZVGBnye4gZTdVLzvovRpA8 root@fudanwuxinginxb
The key's randomart image is:
+---[RSA 2048]----+
|        +.+ .    |
|     . o + = .   |
|      + = . =    |
|     . B * = .   |
|    o + S B o    |
|     = = B = .   |
|      + O E o    |
|     o + + * .   |
|      +o. o o    |
+----[SHA256]-----+
[root@A .ssh]# ssh-copy-id root@192.168.10.158  #将秘钥传输到主机B(root为主机B上的用户,192.168.10.158为主机B的地址)
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.158's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.10.158'"
and check to make sure that only the key(s) you wanted were added.

[root@A .ssh]# ssh root@192.168.10.158  #使用ssh进行登录主机B
Enter passphrase for key '/root/.ssh/id_rsa':   #因为我设置了秘钥密码,所以此处需要输入秘钥密码,否则会直接进入系统
Last login: Sun Sep  2 14:27:10 2018 from 192.168.10.178
[root@fudanwuxi ~]# 
  • 2、主机B上进行查看(/root/.ssh目录下的authorized_keys文件)
[root@B ~]# cd /root/
[root@B ~]# ll -a
总用量 40
dr-xr-x---.  6 root root  253 9月   1 19:29 .
dr-xr-xr-x. 17 root root  224 5月  14 12:45 ..
-rw-------.  1 root root 1783 5月  14 12:46 anaconda-ks.cfg
-rw-------.  1 root root  305 9月   1 19:59 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------.  4 root root   31 5月  14 13:02 .cache
drwx------.  4 root root   30 5月  14 13:02 .config
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  3 root root   25 5月  14 12:52 .dbus
-rw-r--r--.  1 root root 1831 5月  14 12:53 initial-setup-ks.cfg
drwx------.  2 root root   29 9月   2 14:29 .ssh
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-------.  1 root root  595 9月   1 19:16 .viminfo
-rw-------.  1 root root  108 5月  14 13:02 .xauthBwvZib
[root@B ~]# cd .ssh
[root@B .ssh]# ll -a
总用量 4
drwx------. 2 root root  29 9月   2 14:29 .
dr-xr-x---. 6 root root 253 9月   1 19:29 ..
-rw-------. 1 root root 402 9月   2 14:29 authorized_keys
  • 扩展

1、只许秘钥验证,拒绝传统密码验证

[root@B .ssh]# vi /etc/ssh/ssh_config  #修改主机B的ssh配置文件
PasswordAuthentication yes  #将yes修改为no

2、在主机B上将主机A登陆的会话踢除

[root@B ~]# w
 14:39:37 up 1 day, 23:30,  4 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
guest    :0       :0               14May18 ?xdm?  19:10   0.53s /usr/libexec/g
guest    pts/0    :0               14May18 111days  0.60s  4.02s /usr/libexec/g
root     pts/1    192.168.10.17    Sat11    1.00s  0.23s  0.05s w
root     pts/2    192.168.10.178   14:29    9.00s  0.12s  0.12s -bash
[root@B ~]# pkill -kill -t pts/2  #将ssh登陆的用户踢除
[root@B ~]# w
 14:40:11 up 1 day, 23:31,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
guest    :0       :0               14May18 ?xdm?  19:11   0.53s /usr/libexec/g
guest    pts/0    :0               14May18 111days  0.60s  4.02s /usr/libexec/g
root     pts/1    192.168.10.17    Sat11    3.00s  0.19s  0.01s w
[root@B ~]# 
1.启动全分布模式Hadoop集群,守护进程包括NameNode、DataNode、SecondaryNameNode、ResourceManager、NodeManager和JobHistoryServer。 在Hadoop集群主节点上搭建MapReduce开发环境Eclipse。 查看Hadoop自带的MR-App单词计数源代码WordCount.java,在Eclipse项目MapReduceExample下建立新包com.mapreduce,模仿内置的WordCount示例,自己编写个WordCount程序,最后打包成JAR形式并在Hadoop集群上运行该MR-App,查看运行结果。 分别在自编MapReduce程序WordCount运行过程中和运行结束后查看MapReduce Web界面。 2. 有如下股票买卖文件,计算股票的资本损益,统计买卖的每个股票收益。(提示:将每个股票的名称作为key值,当操作为Buy时,value记为负的价格,当操作为Sell时,value记为正的价格,以这个key和value作为map阶段输出,reduce阶段的输入)。实验要求如下: (1)编写MapReduce代码 (2)编译并打包项目 (3)使用hadoop jar命令运行程序 (4)到控制台查看输出文件结果。 文件内容: Leetcode Buy 1000 Corona Buy 10 Leetcode Sell 9000 Handbags Buy 30000 Corona Sell 1010 Corona Buy 1000 Corona Sell 500 Corona Buy 1000 Handbags Sell 7000 Corona Sell 10000 输出结果是: Corona 9500 Handbags -23000 Leetcode 8000 3. 编程实现文件合并和去重操作 对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔其中重复的内容,得到个新的输出文件C。下面是输入文件和输出文件的个样例供参考。 输入文件A的样例如下: 20170101 x 20170102 y 20170103 x 20170104 y 20170105 z 20170106 x 输入文件B的样例如下: 20170101 y 20170102 y 20170103 x 20170104 z 20170105 y 根据输入文件A和B合并得到的输出文件C的样例如下: 20170101 x 20170101 y 20170102 y 20170103 x 20170104 y 20170104 z 20170105 y 20170105 z 三、实验过程结论 记录每步操作,要求贴出实验结果的截图。 给出我具体的操作示例,详细以及验证
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值