CentOS 6.x安裝完成後的優化

查看系統32位仍是64位

[root@i-92x8m5i3 logs]# uname -r
2.6.32-504.16.2.el6.x86_64
[root@i-92x8m5i3 logs]# uname -a
Linux i-92x8m5i3 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@i-92x8m5i3 logs]# ls -d /lib64
/lib64

[root@local-dev ~]# uname -m
x86_64
[root@local-dev ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)

配置網卡

使用setup命令或編輯/etc/sysconfig/network-scripts/ifcfg-eth0內容node

網卡配置完成後執行
ifup eth0 啓動網卡
ifconfig eth0 查看獲取的ip
·ping baidu.com· 檢測網卡是否暢通linux

儘可能不用/etc/init.d/network restart重啓網卡,這會影響物理機上的全部網卡ios

[root@localhost ~]# ifdown eth0 && ifup eth0 快速重啓shell

網絡暢通步驟一,查看網卡windows

[root@local-dev ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:94:6B:B4  
          inet addr:10.0.1.16  Bcast:10.0.1.255  Mask:255.255.254.0
          inet6 addr: fe80::250:56ff:fe94:6bb4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9005200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11334373 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1388103677 (1.2 GiB)  TX bytes:6820763127 (6.3 GiB)

網絡暢通步驟二,查看默認網關centos

[root@local-dev ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.254.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         10.0.0.1        0.0.0.0         UG    0      0        0 eth0

網絡暢通步驟三,查看dns設置安全

[root@local-dev ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 114.114.114.114

在centos6.6中,只在肯定的ifcfg-eth0網卡配置文件上配置dns,若是在/etc/resolv.conf上配置dns,使用命令/etc/init.d/network restart會清除/etc/resolv.conf的dns配置bash

通過網絡暢通三步驟應該就能夠上網了網絡

網卡的配置文件

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0       # 網卡名, eth1第二塊網卡名,以此類推
TYPE=Ethernet     # 上網類型,以太網
UUID=8d6bdf86-1fda-4334-99bb-74b634018e9d    # 惟一標誌碼
ONBOOT=yes        # 開機自啓動
NM_CONTROLLED=yes  # 是否經過NetworkManager管理網卡設備
BOOTPROTO=dhcp     # 啓動協議,none|bootp|dhcp三種選項
HWADDR=00:0C:29:50:98:80 # 網卡mac地址
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no     # 是否支持IP6
NAME="System eth0"
LAST_CONNECT=1486401226

IPADDR=10.0.1.16      # 固定IP
PREFIX=23
GATEWAY=10.0.0.1
NETMASK=255.255.255.0 #子網掩碼
DNS1=114.114.114.114  # 主DNS,默認會覆蓋/etc/resolv.conf的配置

更新系統,打補丁

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
yum clean all
yum makecache
yum update -y  # 更新系統,打補丁

安裝額外的工具軟件包

[root@localhost ~]# yum install tree telnet dos2unix sysstat lrzsz nc nmap -y

sysstat包含了iostat(cpu使用率和硬盤吞吐率)、mpstat(單個或多個)處理器相關的數據、sor(收集報告並存儲系統活躍信息)
yum grouplist 查看全部包名稱
yum groupinstall "Development Tools" 指定包組名安裝,注意須要雙引號ssh

鏈接不上服務排查

1、檢查物理鏈路是否有問題(客戶端執行)
ping 10.0.0.7 # 排查線路問題
windows:tracert -d 10.0.0.7 # 檢查線路是否暢通 -d 不進行反向解析
linux:traceroute 10.0.0.7 -n

2、服務是否開啓端口(客戶端執行)
telnet 10.0.0.7 22
nmap 10.0.0.7 -p 22 (linux環境,須要安裝)

3、是否防火牆阻擋(服務端執行)
/etc/init.d/iptables status

例如:檢查ssh服務是否開啓

[root@i-92x8m5i3 backend]# ps -ef | grep sshd | grep -v grep
root      1075     1  0 May04 ?        00:00:00 /usr/sbin/sshd
root      2100  1075  0 10:25 ?        00:00:00 sshd: root@pts/2 
root      5565  1075  0 12:21 ?        00:00:00 sshd: root@pts/3 
root     19821  1075  0 Jun26 ?        00:00:03 sshd: root@pts/0,pts/1
[root@i-92x8m5i3 backend]# netstat -lntup | grep sshd
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1075/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1075/sshd

用戶

[root@local-dev ~]# useradd ljq
[root@local-dev ~]# passwd ljq
Changing password for user ljq.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@local-dev ~]# su - ljq
[ljq@chuangxin ~]$ whoami
ljq
[ljq@chuangxin ~]$ su - root
Password:

一句話完成密碼設置,可是須要該用戶已存在
[root@local-dev ~]# echo "1234" | passwd --stdin ljq && history -c
Changing password for user ljq.
passwd: all authentication tokens updated successfully.

[ljq@chuangxin ~]$          普通用戶爲$美圓符號
[root@local-dev ~]#         root用戶爲#符號

[root@i-92x8m5i3 backend]# whoami     # 查看當前用戶
root
[root@i-92x8m5i3 backend]# hostname   # 查看當前主機名
i-92x8m5i3

[root@local-dev ~]# echo $PS1      #設置PS1變量
\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@local-dev \[\e[35;40m\]\W\[\e[0m\]]\$

安全設置

關閉SELinux

一、修改SELinux配置文件,使之永遠失效

[root@localhost ~]#  sed -i 's/SELINUX=enforcing/SELinux=disabled/' /etc/selinux/config
[root@localhost ~]# grep SELINUX=disabled /etc/selinux/config
SELINUX=disabled

二、結合手動關閉,可避免重啓

[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]# getenforce
Disabled

設定運行基本爲3(文本模式)

[root@localhost ~]# runlevel
N 3
[root@localhost ~]# grep 3:initdefault /etc/inittab
id:3:initdefault:

實現精簡開機

默認啓動只須要開啓以下5種服務便可

  • sshd

  • rsylog 系統的守護進程使用rsylog程序將各類信息寫到各個系統日誌文件中

  • network 激活或關閉各個網絡接口

  • crond

  • sysstat 檢測系統性能及運行效率的工具

設置開機自自動項

方式一,執行命令完成設置
執行ntsysv命令或執行setup命令,選擇system service選項
退出按Tab鍵進行選擇Exit退出

方式二,使用shell完成設置

注意:只查找3級別的服務項便可

一、先所有關閉,在開啓保留項

# 一、先查看level 3的服務開關情況
[root@localhost ~]# LANG=en
[root@localhost ~]# echo $LANG
en
[root@localhost ~]# chkconfig --list
auditd             0:off    1:off    2:on    3:on    4:on    5:on    6:off
blk-availability    0:off    1:on    2:on    3:on    4:on    5:on    6:off
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
ip6tables          0:off    1:off    2:on    3:on    4:on    5:on    6:off
iptables           0:off    1:off    2:on    3:on    4:on    5:on    6:off
lvm2-monitor       0:off    1:on    2:on    3:on    4:on    5:on    6:off
messagebus         0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
netfs              0:off    1:off    2:off    3:on    4:on    5:on    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
postfix            0:off    1:off    2:on    3:on    4:on    5:on    6:off
rdisc              0:off    1:off    2:off    3:off    4:off    5:off    6:off
restorecond        0:off    1:off    2:off    3:off    4:off    5:off    6:off
rsyslog            0:off    1:off    2:on    3:on    4:on    5:on    6:off
saslauthd          0:off    1:off    2:off    3:off    4:off    5:off    6:off
sshd               0:off    1:off    2:on    3:on    4:on    5:on    6:off
udev-post          0:off    1:on    2:on    3:on    4:on    5:on    6:off

#二、關閉後,查看關閉情況
[root@localhost ~]# for oldboy in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $oldboy off;done
[root@localhost ~]# chkconfig --list
auditd             0:off    1:off    2:on    3:off    4:on    5:on    6:off
blk-availability    0:off    1:on    2:on    3:off    4:on    5:on    6:off
crond              0:off    1:off    2:on    3:off    4:on    5:on    6:off
ip6tables          0:off    1:off    2:on    3:off    4:on    5:on    6:off
iptables           0:off    1:off    2:on    3:off    4:on    5:on    6:off
lvm2-monitor       0:off    1:on    2:on    3:off    4:on    5:on    6:off
messagebus         0:off    1:off    2:on    3:off    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
netfs              0:off    1:off    2:off    3:off    4:on    5:on    6:off
network            0:off    1:off    2:on    3:off    4:on    5:on    6:off
postfix            0:off    1:off    2:on    3:off    4:on    5:on    6:off
rdisc              0:off    1:off    2:off    3:off    4:off    5:off    6:off
restorecond        0:off    1:off    2:off    3:off    4:off    5:off    6:off
rsyslog            0:off    1:off    2:on    3:off    4:on    5:on    6:off
saslauthd          0:off    1:off    2:off    3:off    4:off    5:off    6:off
sshd               0:off    1:off    2:on    3:off    4:on    5:on    6:off
udev-post          0:off    1:on    2:on    3:off    4:on    5:on    6:off
# 三、開啓後,查看開啓情況
[root@localhost ~]# for oldboy in crond network rsyslog sshd sysstat;do chkconfig --level 3 $oldboy on;done
[root@localhost ~]# chkconfig --list
auditd             0:off    1:off    2:on    3:off    4:on    5:on    6:off
blk-availability    0:off    1:on    2:on    3:off    4:on    5:on    6:off
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
ip6tables          0:off    1:off    2:on    3:off    4:on    5:on    6:off
iptables           0:off    1:off    2:on    3:off    4:on    5:on    6:off
lvm2-monitor       0:off    1:on    2:on    3:off    4:on    5:on    6:off
messagebus         0:off    1:off    2:on    3:off    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
netfs              0:off    1:off    2:off    3:off    4:on    5:on    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
postfix            0:off    1:off    2:on    3:off    4:on    5:on    6:off
rdisc              0:off    1:off    2:off    3:off    4:off    5:off    6:off
restorecond        0:off    1:off    2:off    3:off    4:off    5:off    6:off
rsyslog            0:off    1:off    2:on    3:on    4:on    5:on    6:off
saslauthd          0:off    1:off    2:off    3:off    4:off    5:off    6:off
sshd               0:off    1:off    2:on    3:on    4:on    5:on    6:off
udev-post          0:off    1:on    2:on    3:off    4:on    5:on    6:off

二、一條命令shell搞定

默認狀況下,須要保留的服務,已經開啓了,只須要把不用的狀態關閉掉便可

[root@localhost ~]# for oldboy in `chkconfig --list | grep "3:on" | awk '{print $1}' | grep -vE "crond|network|sshd|rsyslog|sysstat"`;do chkconfig $oldboy off;done
[root@localhost ~]# chkconfig --list
auditd             0:off    1:off    2:on    3:off    4:on    5:on    6:off
blk-availability    0:off    1:on    2:on    3:off    4:on    5:on    6:off
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
ip6tables          0:off    1:off    2:on    3:off    4:on    5:on    6:off
iptables           0:off    1:off    2:on    3:off    4:on    5:on    6:off
lvm2-monitor       0:off    1:on    2:on    3:off    4:on    5:on    6:off
messagebus         0:off    1:off    2:on    3:off    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
netfs              0:off    1:off    2:off    3:off    4:on    5:on    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
postfix            0:off    1:off    2:on    3:off    4:on    5:on    6:off
rdisc              0:off    1:off    2:off    3:off    4:off    5:off    6:off
restorecond        0:off    1:off    2:off    3:off    4:off    5:off    6:off
rsyslog            0:off    1:off    2:off    3:on    4:off    5:off    6:off
saslauthd          0:off    1:off    2:off    3:off    4:off    5:off    6:off
sshd               0:off    1:off    2:on    3:on    4:on    5:on    6:off
sysstat            0:off    1:on    2:on    3:on    4:on    5:on    6:off
udev-post          0:off    1:on    2:on    3:off    4:on    5:on    6:off

三、循環語句搞定

原理如2,使用命令拼出處理的字符串,而後經過bash將其當作命令執行

[root@localhost ~]# chkconfig --list | grep -vE "crond|sshd|network|rsyslog|sysstat" | awk '{print "chkconfig " $1 " off"}' | bash

# 另一種寫法
[root@localhost ~]# chkconfig --list | grep 3:on | grep -vE "crond|sshd|network|rsyslog|sysstat" | awk '{print $1}' | sed -r 's#(.*)#chkconfig \1 off#g' | bash

上面的操做會把iptables防火牆也關閉掉,當前系統沒有關閉,須要執行

[root@localhost ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@localhost ~]# /etc/init.d/iptables stop  # 重複執行,確認關閉

修改ssh登陸端口

一、改配置文件方式

二、使用sed命令改

sudo命令控制用戶對系統命令的使用權限

TBD

Linux中文顯示設置

[root@localhost ~]# cat /etc/sysconfig/i18n 
LANG="zh_CN.UTF-8"
[root@localhost ~]# cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori
[root@localhost ~]# echo 'LANG="en_us.UTF-8"' > /etc/sysconfig/i18n
[root@localhost ~]# echo $LANG
en_us.UTF-8
[root@localhost ~]# source /etc/sysconfig/i18n # 立刻生效

設置Linux時間同步

TBD

歷史數據history文件和登陸超時設置

TBD

調整Linux文件描述符數量

TBD

Linux內核參數優化

TBD

定時清理郵件服務臨時目錄垃圾文件

TBD

隱藏Linux版本信息顯示

TBD

鎖定關鍵文件,防止篡改

TBD

清除多餘虛擬帳號

TBD

禁止系統被Ping

TBD

升級具備典型漏洞的軟件版本

TBD

基礎優化與安全

  • 不用root登陸,使用普通用戶,經過sudo受權

  • 更改默認ssh端口,禁止root遠程登陸,甚至修改ssh只監聽內網IP

  • 定時自動更新系統時間

  • 更新yum源

  • 關閉SELinux和iptables

  • 調整文件描述符數量。進程及文件的打開都會消耗文件描述符數量

  • 定時自動清零郵件臨時目錄,防止磁盤inode數量被小文件佔滿

  • 精簡開機任務(如只保留crond、sshd、network、rsyslog、systat)

  • linux內核優化/etc/sysctl.conf,執行sysctl -p生效

  • 更改系統字符集LANG=en_us.UTF-8或LANG=zh_CN.UTF-8

  • 鎖定系統關鍵文件,如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab,處理以上內容吧chattr、lsatr更名爲oldboy並轉移,這樣就安全多了。

  • 清除系統版本信息,清空或修改/etc/issue、/etc/issue.net,去除登陸後的系統信息顯示

  • 清除系統多餘的虛擬用戶帳號

相關文章
相關標籤/搜索