本文共 4107 字,大约阅读时间需要 13 分钟。
SSH 为 Secure Shell 的缩写,SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。
1、ssh安全的加密协议,用于远程连接服务器。2、默认端口22,安全协议版本ssh2.3、服务端包含两个服务功能:ssh远程连接;SFTP服务。4、ssh客户端半酣ssh连接命令,以及远程拷贝scp命令等。利用ssh远程登陆另一台机器:
[root@ycz-computer ~]# ssh -p port user@x.x.x.x-p(小写)接端口,ssh默认端口22;[root@ycz-computer ~]# ssh -p 22 chunzi@192.168.146.143The authenticity of host '192.168.146.143 (192.168.146.143)' can't be established.ECDSA key fingerprint is da:71:20:ad:03:53:ac:b7:ed:a8:ca:2d:d1:93:2e:25.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.146.143' (ECDSA) to the list of known hosts.chunzi@192.168.146.143's password: Last login: Sun Jun 3 19:41:12 2018welcome to yczcomputer!!![chunzi@ycz-computer ~]$
[chunzi@ycz-computer ~]$ logoutConnection to 192.168.146.143 closed.[root@ycz-computer ~]# ls -l ~/.sshtotal 4-rw-r--r--. 1 root root 177 Jun 3 19:42 known_hosts[root@ycz-computer ~]# cat ~/.ssh/known_hosts 192.168.146.143ecdsa-sha2-nistp256AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBwvK+r3suNV22luTg392mzjomP6P1ACFcNtqOcDtZKmvV5W9ulQBxb9OZXBNePoIs9OmDnuqZ9odVhGrVMaOxY=
ssh远程连接直接执行命令:
[root@ycz-computer ~]# ssh -p22 chunzi@192.168.146.143 /usr/bin/free -m #全路径chunzi@192.168.146.143's password: total used free shared buff/cache availableMem: 1823 112 1550 8 161 1536Swap: 4095 0 4095[root@ycz-computer ~]#
将本地/lab/data/scp.txt拷贝到192.168.146.143机器chunzi用户下/lab目录下[root@ycz-computer lab]# scp -P22 /lab/data/scp.txt chunzi@192.168.146.143:/labchunzi@192.168.146.143's password: scp.txt 100% 0 0.0KB/s 00:00
[root@ycz-computer /]# cd lab[root@ycz-computer lab]# ll scp.txt -rw-r--r--. 1 chunzi chunzi 0 Jun 4 11:38 scp.txt[root@ycz-computer lab]#
注意:远端/lab目录需要有其他用户rwx权限才能成功。
[root@ycz-computer lab]# scp -P22 chunzi@192.168.146.143:/lab/scp2.txt ./ chunzi@192.168.146.143's password: scp2.txt 100% 0 0.0KB/s 00:00 [root@ycz-computer lab]# ll scp2.txt -rw-r--r--. 1 root root 0 Jun 4 11:48 scp2.txt[root@ycz-computer lab]#
ssh -p 小写 scp -P 大写 拷贝目录加参数-r
[root@ycz-computer ~]# scp -P22 -r chunzi@192.168.146.143:/lab/shiyan/ ./chunzi@192.168.146.143's password: c2 100% 26 0.0KB/s 00:00 c.sh 100% 16 0.0KB/s 00:00 hhhh 100% 0 0.0KB/s 00:00 yangchunzi 100% 11 0.0KB/s 00:00 time.sh 100% 31 0.0KB/s 00:00 b 100% 55 0.1KB/s 00:00 xin.py 100% 301 0.3KB/s 00:00 1.py 100% 183 0.2KB/s 00:00 [root@ycz-computer ~]# lsanaconda-ks.cfg services_2018-05-14-16.tar.gzmbr.bin shiyan
[root@ycz-computer ~]# sftp -o port=22 chunzi@192.168.146.143chunzi@192.168.146.143's password: Connected to 192.168.146.143.sftp> put ./shiyan/1.py #将./shiyan/目录下1.py文件上传到远端家目录Uploading ./shiyan/1.py to /home/chunzi/1.py./shiyan/1.py 100% 183 0.2KB/s 00:00[chunzi@ycz-computer ~]$ ls #验证1.py a b sftp> get a #将远端家目录下面的文件a下载到本地Fetching /home/chunzi/a to asftp> ^D[root@ycz-computer ~]# ll a #验证-rw-r--r--. 1 root root 0 Jun 4 16:20 a
注意:
Uploading ./shiyan/1.py to /home/chunzi/1.pyremote open("/home/chunzi/1.py"): Permission denied若出现这种问题,则说明远端家目录没有写w的权限。默认/tmp目录有权限。可以将文件上传至远端/tmp目录下之后在经行操作。如果想直接上传至远端家目录,需要增加/home/user写w的权限。sftp> put "E:\wind.txt" #将windows本地E盘文件上传Linux家目录,路径双引号Uploading wind.txt to /root/wind.txt100% 0 bytes 0 bytes/s 00:00:00 [root@ycz-computer ~]# ll wind.txt #验证-rw-r--r--. 1 root root 0 Jun 4 16:58 wind.txtsftp> get a.txt #将Linux家目录文件a.txt下载到windows本地Downloading a.txt from /root/a.txt100% 0 bytes 0 bytes/s 00:00:00
转载于:https://blog.51cto.com/13701875/2124709