Linux网络运维-SSH

ssh 介绍

SSH(安全外壳协议),为Secure Shell的缩写,SSH为建立在应用层和传输层基础上的安全协议。

默认端口:22

Linux中守护进程: sshd

安装服务:OpenSSH

服务端主程序:/usr/sbin/sshd

客户端主程序:/usr/bin/ssh

服务端配置文件:/etc/ssh/sshd_config

客户端配置文件:/etc/ssh/ssh_config

配置文件选项介绍 /etc/ssh/sshd_config

Port 22                                     # 监听端口
ListenAssress 0.0.0.0                       # 监听IP,0.0.0.0允许所有IP
Protocol 2                                  # SSH版本
HostKey /etc/ssh/ssh_host_rsa_key           # 私钥保存位置
ServerKeyBits 1024                          # 私钥的位数
SyslogFacility AUTH                         # 日志记录SSH登录情况
LogLevel INFO                               # 日志等级
GSSAAPIAuthentication yes                   # GSSAPI认证开启

# 安全设定部分
PermitRootLogin yes                         # 允许root通过ssh登录
PubkeyAuthentication yes                    # 是否使用公钥验证
AuthorizedKeysFile .ssh/authorized_keys     # 公钥保存位置
PasswordAuthentication yes                  # 允许使用密码验证登录
PermitEmptyPasswords no                     # 不允许空密码登录

常用SSH命令

远程登录:ssh

ssh 用户名@IP

远程复制:scp

# 下载
scp root@192.168.2.11:root/test.txt /tmp

# 上传
scp -r /root/123.txt/ root@192.168.2.11:/root

文件传输 sftp


[root@localhost ~]# sftp root@192.168.4.2


Connecting to 111.204.156.11:9033...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Your current local directory is
C:\Users\Jerry\Documents\NetSarang Computer\6\Xshell\Sessions

Type `help' to browse available commnands.
sftp:/root> 


进入ftp命令行之后,可以使用一下命令:

  • ls 查看服务器端数据
  • cd 切换服务器端口目录
  • lls 查看本地数据
  • lcd 切换本地目录
  • get 下载
  • put 上传

配置ssh秘钥登录步骤

  1. 步骤一:客户端生成密钥对,并将公钥拷贝到服务器端指定文件中
client端:
- ssh-keygen -t rsa

server端:
- 把公钥拷贝到服务器端(scp,sftp或ssh软件均可)
- cat is_rsa.pub >> /root/.ssh/authorized_keys
- chmod 600 /root/.ssh/authorized_keys
  1. 步骤二:修改服务器端ssh配置文件
RSAAuthentication yes                       # 开启RSA验证
PubkeyAuthentication yes                    # 使用公钥验证
AuthorizedKeysFile .ssh/authorized_keys     # 公钥保存位置
PasswordAuthentication no                   # 禁止使用密码验证登录
  1. 步骤三: 服务器端关闭SELinux服务
[root@localhost .ssh]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled        # 停用SELinux服务
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

注意:编辑完成后,需要重启系统。

  1. 步骤四: 重启ssh服务
systemctl restart sshd