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.143
The 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)? yes
Warning: 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 2018
welcome to yczcomputer!!!
[chunzi@ycz-computer ~]$
- 退出ssh之后本地家目录出现认证口令:
[chunzi@ycz-computer ~]$ logout
Connection to 192.168.146.143 closed.
[root@ycz-computer ~]# ls -l ~/.ssh
total 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 available
Mem: 1823 112 1550 8 161 1536
Swap: 4095 0 4095
[root@ycz-computer ~]#
- scp命令的使用:
将本地/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:/lab
chunzi@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权限才能成功。
- 将远端/lab/scp.txt文件拷贝到本地当前目录。
[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 ~]# ls
anaconda-ks.cfg services_2018-05-14-16.tar.gz
mbr.bin shiyan
- ssh服务服务附带的sftp服务功能: get下载 put上传
[root@ycz-computer ~]# sftp -o port=22 chunzi@192.168.146.143
chunzi@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 a
sftp> ^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.py remote open("/home/chunzi/1.py"): Permission denied 若出现这种问题,则说明远端家目录没有写w的权限。默认/tmp目录有权限。可以将文件上传至远端/tmp目录下之后在经行操作。如果想直接上传至远端家目录,需要增加/home/user写w的权限。
- sftp与windows本地上传下载:
sftp> put "E:\wind.txt" #将windows本地E盘文件上传Linux家目录,路径双引号
Uploading wind.txt to /root/wind.txt
100% 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.txt
sftp> get a.txt #将Linux家目录文件a.txt下载到windows本地
Downloading a.txt from /root/a.txt
100% 0 bytes 0 bytes/s 00:00:00