Linux初始化工作

1. 用户管理 1.1 建用户

adduser usernamepasswd username

1.2 将新建用户添加至wheel组
  • root登录,修改 /etc/pam.d/su 文件,找到#auth required pam_wheel.so use_uid这一行,将行首的#去掉。
  • 执行usermod -G wheel username
1.3 配置新建用户可使用sudo命令
  • 执行#vim /etc/sudoers
  • 找到root ALL=(ALL) ALL,复制一行,将root修改为username
1.4 删除不需要的用户,不需要的组
【Linux初始化工作】注意:不建议直接删除,当你需要某个用户时,自己重新添加会很麻烦。
  • cp /etc/passwd /etc/passwdbak #修改之前先备份- vi /etc/passwd #编辑用户,在前面加上#注释掉此行
#adm:x:3:4:adm:/var/adm:/sbin/nologin #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin #sync:x:5:0:sync:/sbin:/bin/sync #shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown #halt:x:7:0:halt:/sbin:/sbin/halt #uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin #operator:x:11:0:operator:/root:/sbin/nologin #games:x:12:100:games:/usr/games:/sbin/nologin #gopher:x:13:30:gopher:/var/gopher:/sbin/nologin #ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin #注释掉ftp匿名账号

  • cp /etc/group /etc/groupbak #修改之前先备份- vi /etc/group #编辑用户组,在前面加上#注释掉此行
#news:x:9:13:news:/etc/news:

2.修改SSH连接
出于安全考虑,SSH服务配置做以下方面修改
  • 修改端口
  • 禁止root用户登陆
  • 支持密钥登陆
  • 禁用密码登陆
编辑/etc/ssh/sshd_config文件
Port 22 #自己定义一个端口 HostKey /etc/ssh/ssh_host_dsa_key #支持密钥登陆 PermitRootLogin no # 禁用root登陆 StrictModes yes # 采用putty等客户端登陆需要改成noRSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile.ssh/authorized_keysPasswordAuthentication no # 禁用密码登陆

调用ssh-keygen -t rsa生成密钥对,将公钥上id_rsa.pub上传至服务器~/.ssh目录,然后执行cat id_rsa.pub >> authorized_keys
注意,操作完成后,新开一个连接检测是否配置正常,不要关闭当前连接,否则你可能永远都连不上你的服务器了。。。。
3. 防火墙设置 3.1 禁止ping
  • 如果没有iptables禁止ping
echo 1 > /proc/sys/net/ipv4/icmp_echo_igore_all #开启 echo 0 > /proc/sys/net/ipv4/icmp_echo_igore_all #关闭

  • 利用iptables规则禁ping
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP

注意保存service iptables save和重启服务service iptables reload
4. 关闭selinux
/usr/sbin/setenforce 0# 0立刻关闭 1立刻启用 echo "/usr/sbin/setenforce 0" >> /etc/rc.local # 加到系统默认启动里面

5. 关掉不必要的服务
chkconfig --list # 查看系统所有的服务是打开还是关闭 chkconfig 服务名 on # 设置随机启动 chkconfig 服务名 off # 设置不随机启动

6. 修改系统参数
**cat** >> / etc/ sysctl.conf<< eofnet.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 10000 65000 net.ipv4.tcp_max_tw_buckets = 8000 eofsysctl - pulimit - HSn 65535 **echo** - ne "* soft nofile 65535* hard nofile 65535" >> / etc/ security/ limits.conf

    推荐阅读