CentOs 7 初始安全设置——18年3月29日海盗整理编辑上传
在Vultr服务器 CentOs 7下:
第一篇 更换SS端口
一、检查系统版本
[root@localhost ~]# cat /etc/redhat-release
输出类似:CentOS Linux release 7.1.1503 (Core)
二、运行yum命令进行系统升级
[root@localhost ~]# yum clean all
[root@localhost ~]# yum update
三、重启系统
[root@localhost ~]# reboot
四、查看现在系统版本
[root@localhost ~]# cat /etc/redhat-release
五、修改ssh默认22端口
[root@localhost ~]# vi /etc/ssh/sshd_config
键盘输入 i 进行编辑
在Port 22下面加一行,以端口58258为例,Port 58258
即如下:
#Port 22
Port 58258
键盘ESC键 。退出编辑, 输入 :wq 保存退出。
六、重启ssh服务
[root@localhost ~]# systemctl restart sshd.service
七、用 ss 命令检查 ssh 监听的端口,有 58258 证明修改成功
[root@localhost ~]# ss -tnlp | grep ssh
提示
ssh_port_t tcp 58258, 22
八、防火墙中放行新加入端口58258
[root@localhost ~]# firewall-cmd --permanent --add-port=58258/tcp
九、重启防火墙使修改生效
[root@localhost ~]# systemctl restart firewalld
十、确认防火墙是否开启端口58258
[root@localhost ~]# firewall-cmd --permanent --query-port=58258/tcp
输出 yes 为成功
十一、用新端口重新登陆SSH 如登陆成功
[root@localhost ~]# vi /etc/ssh/sshd_config
键盘输入 i 进行编辑
找到
#Port 22
Port 58258
删除
#Port 22
键盘ESC键 。退出编辑, 输入 :wq 保存退出。
十二、重启ssh服务使之生效
[root@localhost ~]# systemctl restart sshd.service
十三、确认是否删除22端口。只显示58258端口为已删除22端口
[root@localhost ~]# ss -tnlp | grep ssh
输出
LISTEN 0 128 :58258 : users:(("sshd",pid=2261,fd=3))
LISTEN 0 128 :::58258 ::: users:(("sshd",pid=2261,fd=4))
十四、最后重启防火墙
[root@localhost ~]# systemctl restart firewalld
######################################################
第二篇 禁用root权限 创建一个新用户
第一步 - 创建一个新用户
登录root。添加用于登录的新用户帐户。
创建一个名为“ucaptain”的新用户
[root@localhost ~]# adduser ucaptain
为新用户分配一个密码
[root@localhost ~]# passwd ucaptain
输入一个强大的密码,然后再次重复验证
第二步 - 根权限
新用户添加到“wheel”组
[root@localhost ~]# gpasswd -a ucptain wheel
第三步 - 添加公钥认证(推荐)
生成新的密钥对
[root@localhost ~]# ssh-keygen
如下所示的输出
ssh-keygen outputGenerating public/private rsa key pair. Enter file in which to save the key (/Users/localuser/.ssh/id_rsa):
使用ssh-copy-id
[root@localhost ~]# ssh-copy-id ucaptain@SERVER_IP_ADDRESS
第四步 - 配置SSH守护进程
[root@localhost ~]# vi /etc/ssh/sshd_config
通过SSH禁用root登录
找到
#PermitRootLogin yes
修改成。 注意去掉 #注释
PermitRootLogin no
重新加载SSH
[root@localhost ~]# systemctl reload sshd
第五步、重启防火墙
[root@localhost ~]# systemctl restart firewalld
第六步、设置防火墙开机自启动
[root@localhost ~]# systemctl enable firewalld
最后退出SSH 重新用新ID 登陆切换到ROOT 账户以便进行操作
[ucaptain@localhost~]$ su root
提示
Password:
输入ROOT 密码
以上经过 Centos 7 中实测
Leave CentOs 7 初始安全设置——18年3月29日海盗整理编辑上传 to:
Read more #centos7 posts
Best Posts From ucaptain
We have not curated any of ucaptain's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.