SSH access and remote control

SSH access and remote control Detailed

SSH is a secure channel protocol, mainly used for remote login, remote copy characters such as interface features. SSH protocol for communication data transmission processing both encrypted, wherein the input comprises a user password when a user logs. Compared with earlier Telent, RSH, RCP applications, SSH protocol offers better security.

A, sshd basic configuration

Now we are XShell used by the server connection, then it should be in service sshd running, we use the following | view "netstat -ntap grep 22" command.

Here we enter the sshd configuration file configuration line some more of them to understand.
Port 22: Listening port number
ListenAddress 0.0.0.0: listening address
LoginGraceTime 2m: login authentication time of 2 min
PermitRootLogin yes: allow root user logins
MaxAuthTries 6: verify the maximum number of retries is 6 log
MaxSessions 10: accessing maximum number of connections
PubkeyAuthentication yes : open the secret key to verify
open port 22 (the beginning of the line to remove the "#" sign)
enter the command "systemctl restart sshd" service sshd restart
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control

下面我们使用另一台服务器test02对服务器test01进行远程控制。
cd /opt/(在test02远程连接的情况下进入test01的opt目录下)
touch aaa.txt(并创建文件aaa.txt成功)
ls(查看文件是否创建成功)
cd /opt/(返回test01并进入opt目录下)
ls(查看远程创建结果)
但是root的权限毕竟太高,随便谁都可以远程连接,难免安全风险过高。所以下面我们设置root用户禁止登录。
首先进入sshd配置文件,修改配置行PermitRootLogin no(禁止root用户登录)
systemctl restart sshd(重启sshd服务)
创建一个新用户tom
useradd tom(创建用户tom)
passwd tom(设置密码)
此时我们再在另一台服务器上输入“ssh [email protected]”,root已经不能登录了。
但方法总比困哪多。我们可以先试用tom用户登录在切换到root
ssh [email protected]
su - root
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
所以光禁止root用户登录并不能完全解决我们的安全隐患。
我们可以进入pam.d模块进行相关安全的修改。(vim /etc/pam.d/su)
启动wheel组,删除行首的“#”号即可
从上面我们可以得知用户tom并不属于wheel组,所以此时wheel组已经不能再随意切换到root助理,只有属于wheel组的用户可以切换到root用户,无疑是增加了我们的服务器安全系数。
SSH access and remote control
SSH access and remote control
为了防止暴力破解,我们还可以对我们的验证登录的最大重试次数进行设置,我们首先进入sshd配置文件开启最大验证次数的配置行(去除行首的“#”即可)。
修改完配置文件记得重启服务。“systemctl restart sshd”
但是我们实际验证时发现,因为服务器默认的最大验证次数是3次,所以我们的设置并未起到作用。
若是想配置起到作用我们需要执行命令“ssh -o NumberOfPasswordPrompts=8 [email protected]”。

SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control

为了进一步加强我们的服务器安全,我们还可以设置黑白名单。
AllowUsers 用户1 用户2(允许登陆的用户)
或者 DenyUsers 用户1 用户2(禁止登录的用户)
两条命令不可以同时使用,还可以同时限制用户的登录地址
下面我们以白名单AllowUsers进行举例。
在sshd的配置文件中添加配置行“AllowUsers [email protected] jerry”(允许用户fan登录固定ip的服务器,允许用户jerry登录所有服务器)。
ssh [email protected](fan用户可以登录)
ssh [email protected](jerry用户可以登录)
ssh [email protected](tom用户无法登陆)
只有在白名单的用户才可以登录。
SSH access and remote control
SSH access and remote control

二、sshd密钥对登录

开启秘钥验证配置行“PubkeyAuthentication yes”。
此时我们的服务器用户目录中并没有秘钥的配置文件。
进入到客户单创建新用户kefu。
在客户端创建秘钥“ssh-keyqen -t ecdsa”
输入秘钥验证的密码:自己定义
ls(查看秘钥文件是否创建成功)
ssh-copy-id -i id-ecdsa.pub [email protected](把秘钥文件推送给服务端)
yes(确认推送)
输入用户fan的登录密码
这时我们在用户fan的家目录就可以找到.ssh秘钥文件了。
下面我们就可以尝试使用密钥登陆了,这时登录的验证方式的密码就是我们刚才设置的密钥对密码。
对于常用到的用户我们还可以对其设置免密码登录:ssh-agent bash
ssh-add
输入密钥对密码
以后我们的kefu用户就可以免密码登录远程服务器了。
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control

三、scp、sftp命令程序

在客户端分别创建文档“ssh_client”和文件夹“test”
之后我们把文档tssh_client和文件夹test远程复制到服务器端。
进入服务端的及目录查看复制结果
使用命令“sftp [email protected]”在客户端远程交互服务器的文档管理
get ssh_client :可以从远程服务器下载文档ssh_client。
up ssh_server:可以远程上传文档ssh_server到服务器。

SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control

四、TCP Wrappers的访问策略

TCP Wrappers object of protection mechanisms for a variety of network services procedures, access to services for the client address
access control. Policy files corresponding to two /etc/hosts.allow and /etc/hosts.deny, are allowed to set
policies and Xu rejected.

About mechanism of TCP Wrappers access policy, when applied in the following order and follow the principles: First check the /etc/hosts.allow file, if the policy match is found, access is allowed; otherwise, continue to check /etc/hosts.deny file, if found strategy to match, access is denied; if the file can not be found checking the above two strategies that match, access is granted.

Sshd service to only want to access from the host IP address 192.168.131.129, other address is denied, you can do the following
vim /etc/hosts.allow (enter whitelist policy file)
sshd: 192.168.131.129 (input allows the host to log IP)
: wq (save and exit)
vim /etc/hosts.deny (blacklisted policy file)
sshd: ALL (prohibit all)
: wq (save and exit)

SSH access and remote control
SSH access and remote control
SSH access and remote control
SSH access and remote control

Guess you like

Origin blog.51cto.com/14449528/2437352