PS:文章最后提后提供一个作者写的一键安全搭建的脚本
简介
SSH—安全外壳协议,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题
同时SCP和SFTP也是SSH一部分。SCP可提供远程复制和上传;SFTP叫做安全文件传输协议,与FTP相比:比FTP安全很多,因为对文件数据进行了加密传输,但同时速度上比FTP要慢,加密解密都需要时间。同时,由于加密后的数据占用更多的字节,因此也比FTP占用更多的网络资源。虽然有这些缺点,但从安全性考虑还是推荐使用SSH协议
搭建即使用
SSH服务搭建很简单,有不少linux默认安装SSH服务,如果没有安装,配置好yum源的服务器直接输入命令:
yum install sshd -y
就能进行安装完成,安装完成就可以直接使用
#开启SSH服务,开启的方法有好几种,就只写一种了
/etc/init.d/sshd start
#关闭SSH
/etc/init.d/sshd stop
#连接ssh服务器linux下直接使用ssh USER@IP 即可,如
[root@promote ~]# ssh [email protected]
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is d2:7a:d2:e2:50:17:6e:6a:e3:90:8b:43:dd:67:13:13.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Tue Jan 24 22:46:03 2017 from 192.168.73.1
[root@promote ~]# exit
登出
Connection to 127.0.0.1 closed.
#windows可以用putty,xshell等工具进行ssh连接
SSH服务默认端口是22,当然如果你想改成其他端口多开nmap这类软件的常规端口扫描,也是很轻松的,后面会进行介绍
SSH安装完成后就有SCP和SFTP这2个工具,SFTP使用基本和FTP一样
#SCP使用也很简单 【scp 文件源 文件目的】 如:
[root@promote ~]# scp [email protected]:/root/a /root/b
root@127.0.0.1's password:
a 100% 0 0.0KB/s 00:00
[root@promote ~]# scp /root/b/a [email protected]:/root/c
[email protected]'s password:
a 100% 0 0.0KB/s 00:00
以上就是SSH服务的基本介绍和使用,下面开始说说重点SSH的配置文件,安装完SSH服务在/etc/ssh/目录下都有一个叫sshd_config的配置文件
配置文件
SSH版本不一样配置文件内容可能存在稍微一些不同,但影响不大。配置文件修改后记得重启才会生效
下面逐行对配置文件内容进行介绍:
13 # If you want to change the port on a SELinux system, you have to tell
14 # SELinux about this change.
15 # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
16 #
17 #Port 22
18 #AddressFamily any
19 #ListenAddress 0.0.0.0
17行Port 22是SSH服务的监听端口,可以看到后面的数字是22,也就是默认端口。如果你想修改SSH的端口只需把前面的#去掉,把22改成其他数字即可。建议修改成大于5000的端口。如果系统带有SELinux,那么还要输入semanage port -a -t ssh_port_t -p tcp 端口号即可。
18行AddressFamily any 表示监听的协议是所有
19行ListenAddress 0.0.0.0 表示监听的网络地址,0.0.0.0代表网络所有地址
23 #Protocol 2
24
25 # HostKey for protocol version 1
26 #HostKey /etc/ssh/ssh_host_key
27 # HostKeys for protocol version 2
28 HostKey /etc/ssh/ssh_host_rsa_key
29 #HostKey /etc/ssh/ssh_host_dsa_key
30 HostKey /etc/ssh/ssh_host_ecdsa_key
31 HostKey /etc/ssh/ssh_host_ed25519_key
32
33 # Lifetime and size of ephemeral version 1 server key
34 #KeyRegenerationInterval 1h
35 #ServerKeyBits 1024
23行为ssh采用的协议版本,有1和2两种,向下兼容
25~26行为协议1的主机密钥文件位置
26~31行为协议2的主机私钥文件位置,提供了更多的加密方式
建议协议类型设置为2,安全性上比1要高很多
34行KeyRegenerationInterval 1h 表示如果协议是SSH-1,那么密钥将每小时重新生成一次
35行 ServerKeyBits 1024 表示服务器私钥的为1024位
42 #SyslogFacility AUTH
43 SyslogFacility AUTHPRIV
44 #LogLevel INFO
45
46 # Authentication:
47
48 #LoginGraceTime 2m
49 #PermitRootLogin yes
50 #StrictModes yes
51 #MaxAuthTries 6
52 #MaxSessions 10
43行 SyslogFacility AUTHPRIV 表示日志记录范围为包括提权用户的所有认证记录
44行 LogLevel INFO 表示日志等级为提供消息
48行 LoginGraceTime 2m 表示用户登陆2不成功,在切断连接之前服务器需要等待2分钟
49行 PermitRootLogin yes 表示允许root登陆,建议设置为no,不允许root直接登陆,可以使用普通用户登陆然后在su到root权限下,以增加安全性
50行 StrictModes yes 表示ssh在接收登录请求之前检查用户家目录和rhosts文件的权限和所有权
51行 MaxAuthTries 6 表示最多认证次数为6次
52行 MaxSessions 10 表示最多开启10个会话
59 AuthorizedKeysFile .ssh/authorized_keys
66 # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
67 #RhostsRSAAuthentication no
68 # similar for protocol version 2
69 #HostbasedAuthentication no
70 # Change to yes if you don't trust ~/.ssh/known_hosts for
71 # RhostsRSAAuthentication and HostbasedAuthentication
72 #IgnoreUserKnownHosts no
73 # Don't read the user's ~/.rhosts and ~/.shosts files
74 #IgnoreRhosts yes
75
76 # To disable tunneled clear text passwords, change to no here!
77 #PasswordAuthentication yes
78 #PermitEmptyPasswords no
79 PasswordAuthentication yes
59行 AuthorizedKeysFile .ssh/authorized_keys 表示公钥文件存放位置
67行 RhostsRSAAuthentication no 表示rhost不使用RSA加密认证
74行 IgnoreRhosts yes 表示忽略rhosts文件
78行 PermitEmptyPasswords no 表示不允许空密码登陆,建议为no
79行 PasswordAuthentication yes 表示进行密码认证
81 # Change to no to disable s/key passwords
82 #ChallengeResponseAuthentication yes
83 ChallengeResponseAuthentication no
84
85 # Kerberos options
86 #KerberosAuthentication no
87 #KerberosOrLocalPasswd yes
88 #KerberosTicketCleanup yes
89 #KerberosGetAFSToken no
90 #KerberosUseKuserok yes
91
92 # GSSAPI options
93 GSSAPIAuthentication yes
94 GSSAPICleanupCredentials no
95 #GSSAPIStrictAcceptorCheck yes
96 #GSSAPIKeyExchange no
97 #GSSAPIEnablek5users no
83行 ChallengeResponseAuthentication no 表示不使用应答模式
93~94行 GSSAPIAuthentication yes 表示允许使用GSSAPI接口的认证
GSSAPICleanupCredentials no 表示退出后不销毁用户缓存,建议改成yes
110 UsePAM yes
111
112 #AllowAgentForwarding yes
113 #AllowTcpForwarding yes
114 #GatewayPorts no
115 X11Forwarding yes
116 #X11DisplayOffset 10
117 #X11UseLocalhost yes
118 #PermitTTY yes
119 #PrintMotd yes
120 #PrintLastLog yes
121 #TCPKeepAlive yes
122 #UseLogin no
123 UsePrivilegeSeparation sandbox # Default for new installations.
124 #PermitUserEnvironment no
125 #Compression delayed
126 #ClientAliveInterval 0
127 #ClientAliveCountMax 3
128 #ShowPatchLevel no
133 #ChrootDirectory none
134 #VersionAddendum none
135
136 # no default banner path
137 #Banner none
138
139 # Accept locale-related environment variables
140 AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
141 AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
142 AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
143 AcceptEnv XMODIFIERS
144
145 # override default of no subsystems
146 Subsystem sftp /usr/libexec/openssh/sftp-server
147
148 # Example of overriding settings on a per-user basis
149 #Match User anoncvs
150 # X11Forwarding no
151 # AllowTcpForwarding no
152 # PermitTTY no
153 # ForceCommand cvs server
113行 AllowTcpForwarding yes 表示允许TCP转发
115行 X11Forwarding yes 表示允许X11转发
118 #PermitTTY yes
119行 PrintMotd yes 表示设置sshd是否在用户登录的时候显示“/etc/motd”中的信息
120行 PrintLastLog yes 表示是否打印最后一次登陆信息,建议设置为no
121行 TCPKeepAlive yes 表示向客户端发送 TCP keepalive 消息
137行 Banner none 表示 banner无
146行 Subsystem sftp /usr/libexec/openssh/sftp-server 表示sftp启动路径
以上就是所有配置文件的基本内容
脚本执行
下面是我写的一个安全执行脚本,不能算上好吧,抛砖引玉。如果大家有更好的欢迎贴出来,一起学习学习
filename="/etc/ssh/sshd_config"
settings_to_yes=( [0]='PermitRootLogin' [1]='PermitEmptyPasswords' [2]='PrintMotd' [3]='PrintLastLog' )
is_error=0
is_exec=0
for param in $@ ;do
if [ $is_error -eq 2 -o $is_error -lt 0 ] ;then
echo 'error synax'
break
fi
case $param in
'--safe')
if [ $is_error -ne 0 ] ;then
let is_error++
fi
;;
'-h')
if [ $is_error -ne 0 ] ;then
let is_error++
fi
;;
'-p')
let is_error++
;;
*)
let is_error--
;;
esac
done
if [ $is_error -eq 0 ] ;then
if [ -e $filename ] ;then
if [ -w $filename ] ;then
while [ $# -gt 0 ] ;do
case $1 in
'--safe')
shift
for value in ${settings_to_yes[@]} ;do
sed -ri "s@#?[[:space:]]?($value[[:space:]]+)yes@\1no@g" $filename
done
is_exec=1
;;
'-h')
shift
echo "usage: $0 [--safe] [-p portNum]"
;;
'-p')
shift
sed -ri "s@#?[[:space:]]?(Port[[:space:]]+)22@\1$1@g" $filename
semanage port -a -t ssh_port_t -p tcp $1 &> /etc/null
shift
is_exec=1
;;
esac
done
if [ $is_exec -eq 1 ] ;then
/etc/init.d/sshd restart
fi
else
echo "Permission denied"
fi
else
echo "The file isn't exists"
fi
else
echo "unknown option:\n
usage: $0 [--safe] [-p portNum]"
fi
其他
SSH除了用可令认证登陆外,也可以使用密钥无密码登陆,比密码更加安全,但前提是密钥的安全。密钥泄露就可能直接造成服务器被拿下,所以使用密钥来登陆一定一定要注意自己的密钥
[root@promote .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
82:d6:64:be:89:8c:6c:fb:32:62:30:0b:f7:ff:20:b1 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| o |
| * |
| + + S |
|+..+ + + |
|o++.E + |
|ooo... . |
|...+..... |
+-----------------+
[root@promote .ssh]#ssh-copy-id ip地址即可