Linux 服务器基本必备的安全设置

沙海 2021年5月18日04:55:27Java评论94字数 1895阅读6分19秒阅读模式
摘要

Linux 服务器基本必备的安全设置 程序员自修室

Linux 服务器基本必备的安全设置

程序员自修室 文章源自JAVA秀-https://www.javaxiu.com/23224.html

文章源自JAVA秀-https://www.javaxiu.com/23224.html

修改 ssh 登陆配置

打开 ssh 配置文件文章源自JAVA秀-https://www.javaxiu.com/23224.html

vim /etc/ssh/sshd_config#修改以下几项Port 10000#更改SSH端口,最好改为10000以上,别人扫描到端口的机率也会下降。防火墙要开放配置好的端口号,如果是阿里云服务器,你还需要去阿里云后台配置开发相应的端口才可以,否则登不上哦!如果你觉得麻烦,可以不用改Protocol 2#禁用版本1协议, 因为其设计缺陷, 很容易使密码被黑掉。PermitRootLogin no#尝试任何情况先都不允许 Root 登录. 生效后我们就不能直接以root的方式登录了,我们需要用一个普通的帐号来登录,然后用su来切换到root帐号,注意 su和su - 是有一点小小区别的。关键在于环境变量的不同,su -的环境变量更全面。PermitEmptyPasswords no#禁止空密码登陆。

最后需要重启 sshd 服务文章源自JAVA秀-https://www.javaxiu.com/23224.html

service sshd restart

禁止系统响应任何从外部/内部来的ping请求

echo “1”> /proc/sys/net/ipv4/icmp_echo_ignore_all

其默认值为 0文章源自JAVA秀-https://www.javaxiu.com/23224.html

用户管理

下面是基本的用户管理命令文章源自JAVA秀-https://www.javaxiu.com/23224.html

  • 查看用户列表:cat /etc/passwd文章源自JAVA秀-https://www.javaxiu.com/23224.html

  • 查看组列表:cat /etc/group文章源自JAVA秀-https://www.javaxiu.com/23224.html

  • 查看当前登陆用户:who文章源自JAVA秀-https://www.javaxiu.com/23224.html

  • 查看用户登陆历史记录:last文章源自JAVA秀-https://www.javaxiu.com/23224.html

一般需要删除系统默认的不必要的用户和组,避免被别人用来爆破:文章源自JAVA秀-https://www.javaxiu.com/23224.html

userdel syncuserdel shutdown# 需要删除的多余用户共有:sync shutdown halt uucp operator games gophergroupdel admgroupdel games# 需要删除的多余用户组共有:adm lp games dip

Linux中的帐号和口令是依据 /etc/passwd 、/etc/shadow、 /etc/group 、/etc/gshadow 这四个文档的,所以需要更改其权限提高安全性:文章源自JAVA秀-https://www.javaxiu.com/23224.html

chattr +i /etc/passwdchattr +i /etc/shadowchattr +i /etc/groupchattr +i /etc/gshadow

如果还原,把 +i 改成 -i ,再执行一下上面四条命令。文章源自JAVA秀-https://www.javaxiu.com/23224.html

注:i属性:不允许对这个文件进行修改,删除或重命名,设定连结也无法写入或新增数据!只有 root 才能设定这个属性文章源自JAVA秀-https://www.javaxiu.com/23224.html

创建新用户

创建新用户命令:adduser username文章源自JAVA秀-https://www.javaxiu.com/23224.html

更改用户密码名:passwd username文章源自JAVA秀-https://www.javaxiu.com/23224.html

个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权。而经常需要root用户的权限,这时候sudo可以化身为root来操作。我记得我曾经sudo创建了文件,然后发现自己并没有读写权限,因为查看权限是root创建的。文章源自JAVA秀-https://www.javaxiu.com/23224.html

sudoers 只有只读的权限,如果想要修改的话,需要先添加w权限:chmod -v u+w /etc/sudoers文章源自JAVA秀-https://www.javaxiu.com/23224.html

然后就可以添加内容了,在下面的一行下追加新增的用户:文章源自JAVA秀-https://www.javaxiu.com/23224.html

wq保存退出,这时候要记得将写权限收回:chmod -v u-w /etc/sudoers文章源自JAVA秀-https://www.javaxiu.com/23224.html

赋予root权限文章源自JAVA秀-https://www.javaxiu.com/23224.html

方法一:修改 /etc/sudoers 文件,找到下面一行,把前面的注释(#)去掉文章源自JAVA秀-https://www.javaxiu.com/23224.html

## Allows people in group wheel to run all commands# 去掉下面一句的前面的注释 # %wheel ALL=(ALL) ALL# 然后修改用户,使其属于root组(wheel),命令如下:# usermod -g root uusama

修改完毕,现在可以用uusama帐号登录,然后用命令 su – ,即可获得root权限进行操作。文章源自JAVA秀-https://www.javaxiu.com/23224.html

方法二(推荐):修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:文章源自JAVA秀-https://www.javaxiu.com/23224.html

## Allow root to run any commands anywhereroot ALL=(ALL) ALLuusama ALL=(ALL) ALL

修改完毕,现在可以用uusama帐号登录,然后用命令 sudo -s ,即可获得root权限进行操作。文章源自JAVA秀-https://www.javaxiu.com/23224.html

方法三:修改 /etc/passwd 文件,找到如下行,把用户ID修改为 0 ,如下所示:文章源自JAVA秀-https://www.javaxiu.com/23224.html

uusama:x:500:500:tommy:/home/uusama:/bin/bash# 修改后如下uusama:x:0:500:tommy:/home/uusama:/bin/bash

保存,用uusama账户登录后,直接获取的就是root帐号的权限。文章源自JAVA秀-https://www.javaxiu.com/23224.html

作者:悠悠  来源:http://uusama.com/69.html文章源自JAVA秀-https://www.javaxiu.com/23224.html

文章源自JAVA秀-https://www.javaxiu.com/23224.html
继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定