Linux ssh远程连接

1.使用ssh客户端
问题
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选项卡–>管理–>克隆。
Linux ssh远程连接_第1张图片
图-1
如图-2所示选择“下一步”。
Linux ssh远程连接_第2张图片
图-2
如图-3所示,选择虚拟机中的当前状态–>下一步。
Linux ssh远程连接_第3张图片
图-3
如图-4所示,选择创建链接克隆–>下一步。
Linux ssh远程连接_第4张图片
图-4
如图-5所示,克隆虚拟机名字命名成B–>更改存放位置–>完成。
Linux ssh远程连接_第5张图片
图-5
如图-6所示,克隆完成选择“关闭”。
Linux ssh远程连接_第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 [email protected]
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?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 [email protected] //再次尝试登录
[email protected]’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 [email protected]
[email protected]’s password:
Last login: Mon Mar 2 13:01:16 2015
[student@pc15 ~]$ hostname
pc15.tarena.com
[student@pc15 ~]$ pwd
/home/student
[student@pc15 ~]$
2.访问非默认端口、图形转发
问题
1)修改主机B的sshd服务端口
2)将端口号改为 8022
3)重启 sshd 服务,确认监听状态
4)从主机A远程登入主机B
5)恢复主机B的sshd服务端口
6)从主机A再次远程登入主机B,且支持图形程序
方案
对于ssh服务,它有两个配置文件一个是/etc/ssh/sshd_config服务端配置文件,另一个是/etc/ssh/ssh_config客户端配置文件,经常用到是/etc/ssh/sshd_config服务端配置文件。
步骤
实现此案例需要按照如下步骤进行。
步骤一:修改主机B的sshd服务端口
在B上操作,将端口号改为 8022,命令操作如下所示:
[root@pc15 桌面]# vim /etc/ssh/sshd_config //注意配置文件为服务器端配置文件
[root@pc15 桌面]# grep 8022 /etc/ssh/sshd_config //注意此字段原先是被注释的
Port 8022 //打开注释更改
[root@pc15 桌面]#
在B上操作,重启 sshd 服务,确认监听状态,命令操作如下所示:
[root@pc15 桌面]# netstat -anptu | grep 8022
[root@pc15 桌面]# /etc/init.d/sshd restart //需重启服务才能神效
停止 sshd: [确定]
正在启动 sshd: [确定]
[root@pc15 桌面]# netstat -anptu | grep 8022
tcp 0 0 0.0.0.0:8022 0.0.0.0:* LISTEN 3052/sshd
tcp 0 0 :::8022 ::? LISTEN 3052/sshd
[root@pc15 桌面]#
步骤二:从主机A远程登入主机B
在A上操作,命令操作如下所示:
[root@svr5 ~]# ssh [email protected]
ssh: connect to host 192.168.8.15 port 22: Connection refused
[root@svr5 ~]# ssh -p 8022 [email protected] //需加上-p选项和对应的端口号
[email protected]’s password:
Last login: Tue Mar 3 10:43:50 2015 from svr5.tarena.com
[root@pc15 ~]# //访问成功
恢复主机B的sshd服务端口,在B上操作,命令操作如下所示:
[root@pc15 桌面]# vim /etc/ssh/sshd_config
[root@pc15 桌面]# grep 8022 /etc/ssh/sshd_config //将其注释即可,遵循默认端口22
#Port 8022
[root@pc15 桌面]# /etc/init.d/sshd restart
停止 sshd: [确定]
正在启动 sshd: [确定]
[root@pc15 桌面]# netstat -anptu | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3139/sshd
tcp 0 0 :::22 ::? LISTEN 3139/sshd
[root@pc15 桌面]#
从主机A再次远程登入主机B,且支持图形程序,在A上操作,命令操作如下所示:
[root@svr5 桌面]# ssh -X [email protected] //加上-X选项
[email protected]’s password:
Last login: Tue Mar 3 11:25:59 2015 from svr5.tarena.com
/usr/bin/xauth: creating new authority file /root/.Xauthority
[root@pc15 ~]# firefox
……
3.使用scp远程复制工具
问题
1)在主机A上使用scp下载文档
2)将主机B上的/root/install.log文件复制到/opt下
3)将主机B上的/boot目录复制到本地的/opt下
4)在主机A上使用scp上传文档
5)确保主机B上有本地用户lisi
6)将本地的/root/install.log文件复制到主机B上用户lisi的家目录下,以用户lisi的密码验证
方案
scp是非常方便的远程复制工具:
上传:scp [-r] 用户名@服务器:路径 本地路径
下载:scp [-r] 本地路径 用户名@服务器:路径
上传下载时要注意,权限问题。
上传:本地要有读取和执行权限,对远程主机要有读取、执行、写入。
下载:本地要有读取、执行、写入,对远程主机要有读取和执行权限。
步骤
实现此案例需要按照如下步骤进行。
步骤一:在主机A上使用scp下载文档
将主机B上的/root/install.log文件复制到/opt下,命令操作如下所示:
[root@svr5 ~]# ls /opt/
cpuburn-in README rh
[root@svr5 ~]# scp [email protected]:/root/install.log /opt/
[email protected]’s password:
install.log 100% 38KB 38.4KB/s 00:00
[root@svr5 ~]# ls /opt/
cpuburn-in install.log README rh
[root@svr5 ~]#
将主机B上的/boot目录复制到本地的/opt下,命令操作如下所示:
[root@svr5 ~]# ls /opt/
cpuburn-in install.log README rh
[root@svr5 ~]# scp -r [email protected]:/boot/ /opt/
[email protected]’s password:
vmlinuz-2.6.32-431.el6.x86_64 100% 4032KB 3.9MB/s 00:00
.vmlinuz-2.6.32-431.el6.x86_64.hmac 100% 166 0.2KB/s 00:00
System.map-2.6.32-431.el6.x86_64 100% 2459KB 2.4MB/s 00:00
……
[root@svr5 ~]# ls /opt/
boot cpuburn-in install.log README rh
[root@svr5 ~]#
步骤二:在主机A上使用scp上传文档
确保主机B上有本地用户lisi,在B上操作,命令操作如下所示:
[root@pc15 桌面]# id lisi
id: lisi:无此用户
[root@pc15 桌面]# useradd lisi
[root@pc15 桌面]# passwd lisi
更改用户 lisi 的密码 。
新的 密码:
无效的密码: WAY 过短
无效的密码: 是回文
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@pc15 桌面]#
将本地的/root/install.log文件复制到主机B上用户lisi的家目录下,以用户lisi的密码验证。
在A上操作,命令操作如下所示:
[root@svr5 ~]# scp /root/install.log [email protected]:/home/lisi
[email protected]’s password:
install.log 100% 38KB 38.4KB/s 00:00
[root@svr5 ~]#
在B上操作,查看验证。命令操作如下所示:
[root@pc15 桌面]# ls /home/lisi/
install.log
[root@pc15 桌面]#

你可能感兴趣的:(原创)