• linux创建用户和组、授权、禁止root远程登录、限制SSH的IP登录


    修改远程登录默认22端口为其它端口

    为什么要修改SSH默认的22端口?

    修改SSH默认的22端口主要是出于安全考虑。

    SSH是一种标准的网络协议,可用于大多数UNIX操作系统,能够实现字符界面的远程登录管理。它默认使用22号端口,采用密文的形式在网络中传输数据,相对于通过明文传输的Telnet,具有更高的安全性。然而,由于22端口是默认的SSH端口,因此可能会有潜在的安全风险。如果有人知道或猜到你的服务器正在使用SSH,他们可能会尝试暴力破解你的密码。

    通过修改SSH默认的22端口为其他随机端口号,可以增加服务器和用户的安全性。即使攻击者知道你的服务器正在使用SSH,他们也需要花费更多的时间和精力去猜测或搜索你使用的随机端口号,从而增加了他们攻击成功的难度。

    另外,如果服务器的22端口由于某种原因无法使用或被阻塞,而你有其他需要远程登录服务器的场景,那么你还可以使用新修改的端口号进行连接,从而提供了更多的灵活性。

    总的来说,修改SSH默认的22端口是一种常见的安全最佳实践,可以提高服务器的安全性,并减少潜在的攻击风险。

    编辑SSH配置文件

    vi /etc/ssh/sshd_config
    
    • 1

    在#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 ::
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    重启ssh服务

    systemctl restart sshd.service
    
    • 1

    如果是云服务器开放相应的端口,则可能还需要在控制台开放相应的端口,以及Linux防火墙开放相应的端口。

    scp复制文件到目标服务器指定端口

    scp -P 2222 local_file app@host:/dir
    
    • 1

    创建全权限用户

    adduser admin
    passwd  admin
    输入密码:123456
    
    • 1
    • 2
    • 3

    修改/etc/sudoers文件权限为可读写

    chmod u+w /etc/sudoers
    
    • 1

    授权全权限

    vim /etc/sudoers
    
    • 1

    root ALL=(ALL) ALL下面添加一行

    admin    ALL=(ALL)     ALL
    
    • 1

    如需新用户使用sudo时不用输密码,把最后一个ALL改为NOPASSWD:ALL即可。
    还原/etc/sudoers为只读

    chmod u-w /etc/sudoers
    
    • 1

    禁止root远程登录

    vi /etc/ssh/sshd_config
    
    • 1

    输入/PermitRootLogin,回车,把PermitRootLogin改成no并把最前面的#号去掉

    PermitRootLogin no
    
    • 1

    :wq保存并退出
    使配置文件生效

    systemctl restart sshd
    
    • 1

    退出远程登录的root用户,再次尝试连接,发现已经连接不上了。

    用户

    查看可以登录系统的用户

    cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1
    
    • 1

    查看可以登录系统的用户组

    cat /etc/group | grep -v /sbin/nologin | cut -d : -f 1
    
    • 1

    组和授权

    创建安全组和审计组

    groupadd safe
    groupadd auditor
    
    • 1
    • 2

    创建用户并添加到指定组
    root用户也可以直接用echo “123456” | passwd --stdin safe改密码

    adduser safe -g safe
    passwd  safe
    密码:123456
    
    adduser auditor -g safe
    passwd  auditor
    密码:123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    添加用户auditor到safe组

    usermod -g safe auditor
    
    • 1

    修改/data目录归属人和组,以及子目录也归safe组

    chown safe:safe /data/mariadb/
    chown -R safe:safe /data/mariadb/
    
    • 1
    • 2

    切换用户

    su safe
    
    • 1

    授权:当前登录用户可以读写/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
    
    • 1
    • 2

    限制SSH IP登录

    只允许ip为192.168.56.122的登录

    vim /etc/hosts.allow
    
    • 1

    追加内容(如果是网段,如:192.168.56.*表示192.168.56.1~255都可以登录

    sshd:192.168.56.122:allow
    
    • 1
    vim /etc/hosts.deny
    
    • 1

    追加内容

    sshd:ALL
    
    • 1

    重启sshd服务使配置生效

    systemctl restart sshd.service
    
    • 1

    只允许某个用户使用某个IP登录

    vim /etc/ssh/sshd_config
    
    • 1

    追加内容

    allowusers root@192.168.56.122
    
    • 1

    重启sshd服务使配置生效

    systemctl restart sshd.service
    
    • 1

    防止修改密码报错先临时关闭,然后再开启

    #改前关闭
    /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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    vi /etc/pam.d/system-auth
    
    • 1
    #%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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    把上面那行注释去掉,改完密码还原注释。

    删除用户和用户组

    userdel auditor
    groupdel auditor
    usermod –G auditor auditor
    
    • 1
    • 2
    • 3
  • 相关阅读:
    企业微信把人移出会有显示吗?如何移出?
    Linux开源IM GGTalk 8.0发布,支持在统信UOS、银河麒麟上运行!
    在win上配置pytorch用到的一些命令(PyCharm & Anaconda)
    Tableau/Power BI 是什么
    浅谈react-redux
    基于SpringBoot的校园志愿者管理系统
    CNCF Keith Chan:分布式云时代,云原生社区的发展与趋势
    ERP对接淘宝/天猫/京东/拼多多商品详情数据API接口
    go里面的基本知识
    实验3 字符串类型
  • 原文地址:https://blog.csdn.net/weixin_43933728/article/details/127347165