L007- linux系统优化进阶课堂小节

时间:2022-05-03 20:53:25

首先把这节课所讲的大概引锁一下,然后下面详细列举。

1.填加普通用户,通过sudo管理。

2.更改默认的SSH服务端口及禁止root用户远程连接。

3.定时自动更新服务器时间

4.关闭防火墙(iptables)

5.调整文件描述符的数量

6.linux内核参数优化/etc/sysctl.conf,   sysctl -p生效

7.更改字符集,支持中文,但建议还是用英文字符集,不然会有乱码问题,特殊情况除外。

8.锁定关键系统文件
9.清空/etc/issue,去除系统及内核版本登录前的屏幕显示,防止黑客了解系统版本。

10.在使用普通用户出现命令找不到的情况可以更改$PTAH环境变量

11.查看网络参数以及端口

12.检查磁盘

以上,是L007本堂课的内容。

1.增加普通用户,通过sudo管理

useradd [name]    //增加普通用户

 [root@lianxi ~]# useradd blog
[root@lianxi ~]# passwd blog
Changing password for user blog.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

visudo      //管理sudo

 [root@lianxi ~]# visudo
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command. ## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
"/etc/sudoers.tmp" 119L, 4040C

进入visudo文件后,根据下面的这个格式写入文件可以管理普通用户权限

 命令:visudo
配置文件
user MACHINE= COMMANDS
root ALL=(ALL) ALL
oldboy ALL=(ALL) /usr/sbin/useradd
用户 机器=(授权哪个角色的权利) /usr/sbin/useradd
%用户组 机器=(授权哪个角色的权利) /usr/sbin/useradd
多个命令用逗号分隔

然后用sudo -l来查看当前用户都开通了什么权限

 [root@lianxi ~]# sudo -l
Matching Defaults entries for root on this host:
requiretty, !visiblepw, always_set_home, env_reset,
env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG
LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION
LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME
LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL
LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin User root may run the following commands on this host:
(ALL) ALL

  之所以要这么做事因为远程连接状态下,尽量不要使用root,权限大,当然安全隐患也就多,所以尽量以普通用户登录,如果需要使用root,可以在普通用户下输入su - 或者su - root。

L007- linux系统优化进阶课堂小节

2.更改默认的SSH服务端口及禁止root用户远程连接。

  ssh的默认端口为22,因为是默认,所以是十分不安全的,所以我们要更换一个端口和禁止root用户来提升系统的安全性。

 [root@lianxi ~]# vi /etc/ssh/sshd_config           //ssh配置文件
# $OpenBSD: sshd_config,v 1.80 // :: djm Exp $ # This is the sshd server system-wide configuration file. See
# sshd_config() for more information. # This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin # The strategy used for options in the default sshd_config shipped wit
h
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
"/etc/ssh/sshd_config" 146L, 4044C

在ssh配置文件中,填加如下:

 ####SSHpeizhi####
Port
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
####SSHpeizhi####
翻译:
###################
端口:
使用root账户登录               no
使用空密码            no
用DNS认证            no
是否在用户退出登录后自动销毁用户凭证缓存。默认值是”yes”。仅用于SSH-。 no
##################

3.定时自动更新服务器时间

ntpdate    时间服务器命令

 [root@lianxi ~]# which ntpdate                //查询ntpdate路径
/usr/sbin/ntpdate
[root@lianxi ~]# /usr/sbin/ntpdate time.nist.gov //同步时间
Mar :: ntpdate[]: adjust time server 216.229.0.179 offset 0.102953 sec
[root@lianxi ~]# echo '*/5 * * * * /usr/sbin/stpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root //循环更新时间
[root@lianxi ~]# crontab -l //查询循环
*/ * * * * /usr/sbin/nptdate time.nist.gov >/dev/null >&

4.关闭防火墙(iptables

 [root@lianxi ~]# /etc/init.d/iptables stop    //外网时需要打开防火墙

5.调整文件描述符的数量

  文件描述符是一个简单的整数,用以标明每一个被进程所打开的文件和socket。第一个打开的文件是0,第二个是1,依此类推。Linux 操作系统通常给每个进程能打开的文件数量强加一个限制。更甚的是,Linux 通常有一个系统级的限制。当用完所有的文件描述符后,它不能接收用户新的连接。也就是说,用完文件描述符导致拒绝服务。所以我们要加大文件描述符。

 [root@lianxi ~]# ulimit -n            //查询文件描述符的数量

 [root@lianxi ~]# ulimit -HSn       //修改最大文件描述符为65535
65535是最大范围,不能大于65535,调大也是为了黑客虚假空排。

  大部分命令行的修改都是重启后复原的,ulimit -HSn [数量] 也一样,所以下面我们来永久改动。

 [root@lianxi ~]# echo '*       -    nofile   65535' >>/etc/security/limits.conf                                          //写入文件
[root@lianxi ~]# cat /etc/security/limits.conf //查询
#* soft core
#* hard rss
#@student hard nproc
#@faculty soft nproc
#@faculty hard nproc
#ftp hard nproc
#@student - maxlogins # End of file
* - nofile

6.linux内核参数优化/etc/sysctl.conf,   sysctl -p生效

  内核参数文件在vi /etc/sysctl.conf

 [root@lianxi ~]# vi /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, is disabled, is enabled. See sysctl() and
# sysctl.conf() for more details. # Controls IP packet forwarding
net.ipv4.ip_forward = # Controls source route verification
net.ipv4.conf.default.rp_filter = # Do not accept source routing
"/etc/sysctl.conf" 61L, 1898C

  把下列参数复制到文件中。

net.ipv4.tcp_fin_timeout =
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_syncookies =
net.ipv4.tcp_keepalive_time =
net.ipv4.ip_local_port_range =
net.ipv4.tcp_max_syn_backlog =
net.ipv4.tcp_max_tw_buckets =
net.ipv4.route.gc_timeout =
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_synack_retries =
net.core.somaxconn =
net.core.netdev_max_backlog =
net.ipv4.tcp_max_orphans =

  如果有防火墙,请把一下复制进去

net.nf_conntrack_max =
net.netfilter.nf_conntrack_max =
net.netfilter.nf_conntrack_tcp_timeout_established =
net.netfilter.nf_conntrack_tcp_timeout_time_wait =
net.netfilter.nf_conntrack_tcp_timeout_close_wait =
net.netfilter.nf_conntrack_tcp_timeout_fin_wait =

  如下为重新开始sysctl服务。如果防火墙关闭状态可能报错,偶尔也不会报错。

 [root@lianxi ~]# sysctl -p
net.ipv4.ip_forward =
net.ipv4.conf.default.rp_filter =
net.ipv4.conf.default.accept_source_route =
kernel.sysrq =
kernel.core_uses_pid =
net.ipv4.tcp_syncookies =
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb =
kernel.msgmax =
kernel.shmmax =
kernel.shmall =
net.ipv4.tcp_fin_timeout =
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_syncookies =
net.ipv4.tcp_keepalive_time =
net.ipv4.ip_local_port_range =
net.ipv4.tcp_max_syn_backlog =
net.ipv4.tcp_max_tw_buckets =
net.ipv4.route.gc_timeout =
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_synack_retries =
net.core.somaxconn =
net.core.netdev_max_backlog =
net.ipv4.tcp_max_orphans =
error: "net.nf_conntrack_max" is an unknown key
error: "net.netfilter.nf_conntrack_max" is an unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_established" is an unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_time_wait" is an unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_close_wait" is an unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_fin_wait" is an unknown key
[root@lianxi ~]#

7.更改字符集,支持中文,但建议还是用英文字符集,不然会有乱码问题,特殊情况除外。

查看计算机内的字符集

 [root@lianxi ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

加入中文字符集

 [root@lianxi ~]# echo 'LANG="zh_CN,GB18030"' >> /etc/sysconfig/i18n

再用VI给之前的那个字符集用#注释掉。

设置好后需要执行文件才可以更新字符集

 [root@lianxi ~]# $LANG

8.锁定关键系统文件

chattr +i    锁定

chattr -i    解锁

 [root@lianxi ~]#chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab
锁定 用户文件 用户密码文件 用户主文件 主密码文件 开机启动文件

  上锁以后这些功能就不能正常使用了,所以再使用时需要解锁

[root@lianxi ~]#chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

  之所以上锁就是因为防止黑客破坏系统,但是你会上锁,黑客更会解锁,所以可以在上锁后删除chattr命令,等下次需要时再上传,或者更名

 [root@moban ~]# which chattr
/usr/bin/chattr
[root@moban ~]# mv /usr/bin/chattr /usr/bin/oldboy //oldboy为自己起的一个名字
[root@moban ~]# chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab //再次使用解除命令
-bash: /usr/bin/chattr:没有那个文件或目录                                //系统提示无此命令,成功,不过此时把chattr变换城oldboy会成功。你猜猜是为什么呢?是不是很巧妙?

 lsattr      查看某某命令是否已经加锁

 [root@lianxi ~]# lsattr /etc/passwd
----i--------e- /etc/passwd //i为加锁

9.清空/etc/issue,去除系统及内核版本登录前的屏幕显示,防止黑客了解系统版本。

  系统内核信息存储在issue下,使用如下命令清空

 [root@moban ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
[root@moban ~]# > /etc/issue //清空版本

这么做主要是迷惑黑客,不让其知道版本,也就不知道版本的漏洞了。

10.在使用普通用户出现命令找不到的情况可以更改$PTAH环境变量

  在使用普通用户时,可能输入一些命令却找不到,那么这时就需要输入跟命令或者是填加环境变量。

1)根命令

使用which找到命令的跟,如需使用mv,那么先which mv,查出mv的根路径/bin/mv。

2)查看$PATH,如果没有/bin的路径,可以手动加进去

[root@moban ~]# $PATH        //查看环境变量
-bash: /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/root/bin: No such file or directory
[root@moban ~]# PATH=/bin:$PATH //加入环境变量/oldboy/
[root@moban ~]# $PATH
-bash: /oldboy/:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin: No such file or directory //看到了加进去的

但是这种情况,重启服务器后会消失(大多数都是,在命令行修改都属于临时生效,只有放在文件里才会永久更改。)

 [root@moban ~]# echo 'PATH="/bin:$PATH"' >> /etc/profile     //加入到变量文件中
[root@moban ~]# source /etc/profile //source使其生效

PATH 环境变量大写,所有包含在环境变量里面的路径,都可以直接敲出来执行。
全局生效/etc/profile。 普通用户生效~/.bash_profile或者~/.bashrc

11.查看网络参数以及端口

netstat        查看网络状态 参数:lntup或an

lsof -i :port(端口)  查看端口

 [root@lianxi ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd
tcp ::: :::* LISTEN /sshd

[root@lianxi ~]# lsof -i :
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP *: (LISTEN)
sshd root 4u IPv6 0t0 TCP *: (LISTEN)
sshd root 3r IPv4 0t0 TCP bogon:->bogon: (ESTABLISHED)
sshd lichaoran 3u IPv4 0t0 TCP bogon:->bogon: (ESTABLISHED)
[root@lianxi ~]#

12.检查磁盘

df -h    查看物理空间

 [root@lianxi ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 .9G .6G .9G % /
tmpfs 932M 932M % /dev/shm
/dev/sda1 194M 29M 155M % /boot

df -hi    查看物理空间以外的

 [root@lianxi ~]# df -hi
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 579K 55K 525K % /
tmpfs 233K 233K % /dev/shm
/dev/sda1 50K 50K % /boot