为什么要修改SSH默认的22端口?
修改SSH默认的22端口主要是出于安全考虑。
SSH是一种标准的网络协议,可用于大多数UNIX操作系统,能够实现字符界面的远程登录管理。它默认使用22号端口,采用密文的形式在网络中传输数据,相对于通过明文传输的Telnet,具有更高的安全性。然而,由于22端口是默认的SSH端口,因此可能会有潜在的安全风险。如果有人知道或猜到你的服务器正在使用SSH,他们可能会尝试暴力破解你的密码。
通过修改SSH默认的22端口为其他随机端口号,可以增加服务器和用户的安全性。即使攻击者知道你的服务器正在使用SSH,他们也需要花费更多的时间和精力去猜测或搜索你使用的随机端口号,从而增加了他们攻击成功的难度。
另外,如果服务器的22端口由于某种原因无法使用或被阻塞,而你有其他需要远程登录服务器的场景,那么你还可以使用新修改的端口号进行连接,从而提供了更多的灵活性。
总的来说,修改SSH默认的22端口是一种常见的安全最佳实践,可以提高服务器的安全性,并减少潜在的攻击风险。
vi /etc/ssh/sshd_config
在#Port 22追加一行Port 2222或者是其它端口,如果需要多个端口就再加一行。
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
Port 2222
Port 22222
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
重启ssh服务
systemctl restart sshd.service
如果是云服务器开放相应的端口,则可能还需要在控制台开放相应的端口,以及Linux防火墙开放相应的端口。
scp -P 2222 local_file app@host:/dir
adduser admin
passwd admin
输入密码:123456
修改/etc/sudoers文件权限为可读写
chmod u+w /etc/sudoers
授权全权限
vim /etc/sudoers
root ALL=(ALL) ALL下面添加一行
admin ALL=(ALL) ALL
如需新用户使用sudo时不用输密码,把最后一个ALL改为NOPASSWD:ALL即可。
还原/etc/sudoers为只读
chmod u-w /etc/sudoers
vi /etc/ssh/sshd_config
输入/PermitRootLogin,回车,把PermitRootLogin改成no并把最前面的#号去掉
PermitRootLogin no
:wq保存并退出
使配置文件生效
systemctl restart sshd
退出远程登录的root用户,再次尝试连接,发现已经连接不上了。
查看可以登录系统的用户
cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1
查看可以登录系统的用户组
cat /etc/group | grep -v /sbin/nologin | cut -d : -f 1
创建安全组和审计组
groupadd safe
groupadd auditor
创建用户并添加到指定组
root用户也可以直接用echo “123456” | passwd --stdin safe改密码
adduser safe -g safe
passwd safe
密码:123456
adduser auditor -g safe
passwd auditor
密码:123456
添加用户auditor到safe组
usermod -g safe auditor
修改/data目录归属人和组,以及子目录也归safe组
chown safe:safe /data/mariadb/
chown -R safe:safe /data/mariadb/
切换用户
su safe
授权:当前登录用户可以读写/data,本组人可以读写/data,其它组读的权限
safe用户对/data/mariadb:chmod 700 /data/mariadb(/data/mariadb文件只能自己看,组和其它组都无法看)
safe用户对/data/mariadb/mariadb.log:chmod 740 /data/mariadb/mariadb.log(auditor用户加入到safe组,和safe因为同组,可以读数据)
chmod u=rwx,g=rx,o=x /data
chmod u=rwx,g=rx,o=x /data/mariadb.log
只允许ip为192.168.56.122的登录
vim /etc/hosts.allow
追加内容(如果是网段,如:192.168.56.*表示192.168.56.1~255都可以登录)
sshd:192.168.56.122:allow
vim /etc/hosts.deny
追加内容
sshd:ALL
重启sshd服务使配置生效
systemctl restart sshd.service
只允许某个用户使用某个IP登录
vim /etc/ssh/sshd_config
追加内容
allowusers root@192.168.56.122
重启sshd服务使配置生效
systemctl restart sshd.service
#改前关闭
/usr/sbin/setenforce 0
chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
lsattr /etc/passwd /etc/shadow
#改完还原
/usr/sbin/setenforce 1
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
lsattr /etc/passwd /etc/shadow
vi /etc/pam.d/system-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth required pam_env.so
auth required pam_faildelay.so delay=2000000
auth sufficient pam_fprintd.so
auth sufficient pam_unix.so nullok try_first_pass
auth requisite pam_succeed_if.so uid >= 1000 quiet_success
auth required pam_deny.so
auth required pam_tally2.so deny=5 unlock_time=180 even_deny_root_account audit
account required pam_unix.so
account sufficient pam_localuser.so
account sufficient pam_succeed_if.so uid < 1000 quiet
account required pam_permit.so
#password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password required pam_deny.so
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=6 ucredit=-2 lcredit=-1 dcredit=-4 ocredit=-1
session optional pam_keyinit.so revoke
session required pam_limits.so
-session optional pam_systemd.so
session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session required pam_unix.so
把上面那行注释去掉,改完密码还原注释。
userdel auditor
groupdel auditor
usermod –G auditor auditor