1、添加用户:sudo useradd -m username
useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G <群组>][-s ][-u ][用户帐号]
参数说明:
-m 自动建立用户的登录目录
-M 不要自动建立用户的登录目录
-n 取消建立以用户名称为名的群组
-r 建立系统账号
-s 指定用户登录后所使用的shell
-u 指定用户ID
然后设置密码 passwd 用户名
2、在新用户的home目录下创建一个.ssh的文件夹,该文件夹的权限是700,owner是$username;
3、将新用户的公钥上传到服务器上,拷贝到/home/$username/.ssh/目录下,并重命名为authorized_keys
4、将authorized_keys文件权限修改为644,同时修改所有者为$username。
配置公钥密钥的步骤:
配置公钥密钥的步骤:
1、用哪个ssh-keygen生成id_rsa(私钥)、id_rsa.pub(公钥)
ssh-keygen -t rsa -c “email@example.com”
命令行会提示让你指定秘钥的名称,按回车键将 SSH Key 保存到默认文件名即可:
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
接下来会询问你为 SSH Key 设置密码
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
2、touch authorized_keys
cat id_rsa.pub >> authorized_keys(把相应的用户的id_rsa.pub加入权限验证)
设置vim /etc/passwd /bin/bash
5、设置超级用户
chmod u+w /etc/sudoers
username ALL=(ALL) ALL
chmod u-w /etc/sudoers
6、常用的SSH服务端远程登录配置与实践
配置文件 /etc/ssh/sshd_config
Port #可修改ssh访问端口,默认为22
PermitEmptyPasswords #是否允许密码为空的用户远程登录。默认为"no"
PermitRootLogin #是否允许ssh远程登录root帐号,建议为no
ClientAliveInterval 0 #客户端连接数做限制,根据需求设置
UseDNS #指定sshd是否应该对远程主机名进行方向解析,以检查此主机名是否与其IP地址真实对应。默认值为"yes"。个人建议改成no,否则可能会导致ssh连接很慢
GSSAPIAuthentication no #解决Linux之间使用SSH远程连接慢的问题
#限制可登录用户的办法如下:
AllowUsers user1 user2 user3 #允许ssh登录用户
DenyUsers #禁止ssh登陆用户
AllowGroups #允许ssh登录用户组
DenyGroups #禁止ssh登陆用户组
更高级的SSH安全策略
修改/etc/hosts.allow和/etc/hosts.deny 这两个文件
1)更改SSH监听的IP,使其监听内网IP段
[root@localhost ssh]# cat sshd_config|grep Listen
ListenAddress 192.168.1.8:52168
2)通过防火墙限制仅能使用内网IP连接此服务器。限制命令如下
iptables -I INPUT -p -tcp --dport 52168 -s 192.168.1.0/24 -j ACCEPT
3)通过拨号到×××服务器,然后从局域网访问这些服务器,提升安全性。
重启ssh服务:sudo /etc/init.d/ssh restart