LINUX 使用ssh客户端

本文介绍了在Linux系统中实现远程访问的方案。首先准备两台可互通的虚拟机A、B,采用克隆方式创建B。接着对虚拟机进行环境准备,包括关闭相关服务。然后更改B的网卡名称和主机名,最后完成从A以root和student身份登入B的远程访问操作。

问题
1)准备两台虚拟机A、B
2)主机A的IP地址为 192.168.8.128/24
3)主机B的IP地址为 192.168.8.15/24,与A可互通
4)完成以下远程访问操作
5)从主机A上以root身份登入主机B
6)在主机B上创建用户student,设置密码redhat
7)从主机A上以用户student登入主机B
方案
SSH的服务名为sshd,它是一个独立服务。Linux默认安装并自启动的一个服务。
准备两台虚拟机,我们采用的最简单的方式为克隆一台B。将原有的虚拟机关机名称命名为A,克隆一台B。
注意:两台虚拟机需互相通信,为了方便所以提前要把A的网络类型,更改为VMnet8。再进行克隆。
克隆后B需要更改网卡名称,这里涉及到第四阶段udev规则的知识。我们这里简单理解,udev是动态的管理设备,可以为设备起一个唯一的名字。说的简单点就是设备名字的命名规则。
步骤
实现此案例需要按照如下步骤进行。
步骤一:虚拟机克隆
将原有的虚拟机关机名称命名为A。如图-1所示,右击虚拟机A选项卡–>管理–>克隆。
在这里插入图片描述
图-1
如图-2所示选择“下一步”。
在这里插入图片描述
图-2
如图-3所示,选择虚拟机中的当前状态–>下一步。
在这里插入图片描述
图-3
如图-4所示,选择创建链接克隆–>下一步。
在这里插入图片描述
图-4
如图-5所示,克隆虚拟机名字命名成B–>更改存放位置–>完成。
在这里插入图片描述
图-5
如图-6所示,克隆完成选择“关闭”。
在这里插入图片描述
图-6
将两台虚拟机全部开机。
注意:准备环境,两台虚拟机都要操作:关闭NetworkManager、关闭防火墙、关闭SELinux。
命令操作如下所示:

    [root@svr5 /]# /etc/init.d/NetworkManager stop     	//当前关闭
    [root@svr5 /]# chkconfig NetworkManager off         //配置自起状态
    [root@svr5 /]# iptables –F                            //清空防护墙策略
    [root@svr5 /]# /etc/init.d/iptables save             //将空策略的防火墙保存
    iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
    [root@svr5 /]# /etc/init.d/iptables stop             //停止防火墙服务
    [root@svr5 /]# chkconfig iptables off                //配置自起状态
    [root@svr5 /]# getenforce                             //查看SELinux状态
    Enforcing
    [root@svr5 /]# setenforce 0                           //当前关闭SELinux
    [root@svr5 /]# getenforce                  //查看SELinux是否关闭Permissive为不启用
    Permissive
    [root@svr5 /]# cat /etc/selinux/config    //永久关闭SELinux,需修改配置文件
    ......

#     permissive - SELinux prints warnings instead of enforcing.
......
SELINUX=permissive 
......

步骤二:主机B的IP地址为 192.168.8.15/24,与A可互通
分析: 会发现B网卡识别错误,没有eth0,更改udev规则重新命名网卡名称。
将B的主机名更改为pc15.tarena.com方便区分,在B上操作。
命令操作如下所示:

[root@pc15 桌面]# hostname pc15.tarena.com
[root@pc15 桌面]# vim /etc/sysconfig/network
[root@pc15 桌面]# grep pc15 /etc/sysconfig/network
HOSTNAME=pc15.tarena.com
[root@pc15 桌面]#

修改udev规则,重新命名网卡名称。
分析: 以MAC地址为准,将ifconfig命令所识别到的MAC、网卡配置文件、udev配置文件做到统一即可,以ifconfig命令所识别到的MAC为准。
命令操作如下所示:

[root@pc15 桌面]# ifconfig                           //可以看到没有eth0
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:720 (720.0 b)  TX bytes:720 (720.0 b)

[root@pc15 桌面]# ifconfig eth1              //查看ifconfig识别的网卡MAC地址
eth1      Link encap:Ethernet  HWaddr 00:0C:29:2F:32:FA  //可以复制此处MAC方便更改
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
//修改网卡配置文件(注意要与上条命令检测的MAC地址一致)
[root@pc15桌面]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:2F:32:FA
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.8.15             //ip地址保证不要冲突
NETMASK=255.255.255.0
//修改udev规则(保留与ifconfig -a eth1 所识别的MAC一致的一行,其余全部删除或注释)修改网卡名
 [root@pc15桌面]# cat /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net",ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0C:29:2F:32:FA ", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
[root@pc15桌面]# modprobe -rv e1000       //卸载网卡驱动
[root@pc15桌面]# modprobe -v e1000        //挂载网卡驱动
[root@pc15桌面]# /etc/init.d/network restart
[root@pc15 桌面]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2F:32:FA  
          inet addr:192.168.8.15  Bcast:192.168.8.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2f:32fa/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:636 (636.0 b)

[root@pc15 桌面]#

在B上操作,测试与A通信。
命令操作如下所示:

[root@pc15 桌面]# ping -c 3 192.168.8.128
PING 192.168.8.128 (192.168.8.128) 56(84) bytes of data.
64 bytes from 192.168.8.128: icmp_seq=1 ttl=64 time=12.6 ms
64 bytes from 192.168.8.128: icmp_seq=2 ttl=64 time=0.188 ms
64 bytes from 192.168.8.128: icmp_seq=3 ttl=64 time=0.257 ms

--- 192.168.8.128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 0.188/4.350/12.606/5.837 ms
[root@pc15 桌面]#

步骤三:完成以下远程访问操作
从主机A上以root身份登入主机B。
分析: 使用ssh远程连接时输入的用户名与密码永远都是对方的,本题为B的。
在A上操作,命令操作如下所示:

[root@svr5 ~]# ssh root@192.168.8.15
The authenticity of host '192.168.8.15 (192.168.8.15)' can't be established.
RSA key fingerprint is e3:48:b7:e0:d5:9d:47:01:2b:06:be:1c:ab:c0:98:63.
Are you sure you want to continue connecting (yes/no)? yes   //首次访问会提示
Warning: Permanently added '192.168.8.15' (RSA) to the list of known hosts.
//SSH服务端默认会进行DNS解析,因此为了提升速度建议编写hosts配置文件
按Ctrl+c直接结束
[root@svr5 ~]# vim /etc/hosts
[root@svr5 ~]# tail -n 1 /etc/hosts
192.168.8.15 pc15.tarena.com 
[root@svr5 ~]# ssh root@192.168.8.15    	//再次尝试登录
root@192.168.8.15's password:            	//输入密码不显示
Last login: Tue Mar  3 10:07:46 2015
[root@pc15 ~]# hostname                   	//登录成功
pc15.tarena.com
[root@pc15 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2F:32:FA  
          inet addr:192.168.8.15  Bcast:192.168.8.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2f:32fa/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:241 errors:0 dropped:0 overruns:0 frame:0
          TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:21391 (20.8 KiB)  TX bytes:9896 (9.6 KiB)
[root@pc15 ~]# exit                      //退出
logout
Connection to 192.168.8.15 closed.
[root@svr5 ~]#

在主机B上创建用户student,设置密码redhat。
在B上操作,命令操作如下所示:

[root@pc15 桌面]# id student
uid=500(student) gid=500(student) 组=500(student)
[root@pc15 桌面]# passwd student
更改用户 student 的密码 。
新的 密码:
无效的密码: WAY 过短
无效的密码: 是回文
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@pc15 桌面]#

从主机A上以用户student登入主机B,在A上操作,命令操作如下所示:

[root@svr5 ~]# ssh student@192.168.8.15
student@192.168.8.15's password: 
Last login: Mon Mar  2 13:01:16 2015
[student@pc15 ~]$ hostname 
pc15.tarena.com
[student@pc15 ~]$ pwd
/home/student
[student@pc15 ~]$
1、查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH: $ ssh -V OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 下面的例子表明该系统正在使用SSH2: $ ssh -V ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu 2、用SSH登录到远程主机 当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下: 1 2 3 4 5 6 7 8 localhost$ ssh -l jsmith remotehost.example.com Host key not found from database. Key fingerprint: xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile. Are you sure you want to continue connecting (yes/no)? Yes Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$ 因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。 1 2 3 localhost$ ssh -l jsmith remotehost.example.com jsmith@remotehost.example.com password: remotehost.example.com$ 由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因: o 系统管理员在远程主机上升级或者重新安装了SSH服务器 o 有人在进行一些恶意行为,等等。 在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the- middle attack)! It is also possible that the host key has just been changed. Please contact your system administrator. Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message. Received server key's fingerprint: xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum You can get a publ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值