DAY 11 操作系统基础优化

备份服务 运维 /sa (systemadmin) 网络 /network 开发/dev 数据/dba(database admin)
系统用户优化 (创建用户)
利用如何创建多个用户
~~~
[root@oldboy63 ~]# vi useradd.sh
#!/bin/bash ---标准格式
for i in {1..10}
do
useradd stui
done
"useradd.sh" [New] 5L, 86C written
[root@oldboy63 ~]# sh useradd.sh
Changing password for user stu1.
passwd: all authentication tokens updated successfully.
Changing password for user stu2.
passwd: all authentication tokens updated successfully.
Changing password for user stu10.
passwd: all authentication tokens updated successfully.
[root@oldboy63 ~]# #!/bin/bash
[root@oldboy63 ~]# for i in {1..10}
> do
> useradd stui
> done
us
[root@oldboy63 ~]#
系统用户优化 添加用户 设置密码 切换用户
su - 和 su切换用户有什么区别??
su - : 彻底切换环境变量信息 用户相关环境变量
su : 切换环境变量信息不彻底 用户相关环境变量
第一步: 如何查看系统环境变量
env
第二步: 切换后查看环境变量信息
su -切换
[oldboy@oldboyedu ~]$ env|grep oldboy
HOSTNAME=oldboyedu
USER=oldboy
MAIL=/var/spool/mail/oldboy
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/oldboy:/oldgirl:/home/oldboy/.local/bin:/home/oldboy/bin
PWD=/home/oldboy
HOME=/home/oldboy
LOGNAME=oldboy

   su 切换 
   [root@oldboyedu ~]# su oldboy
   [oldboy@oldboyedu root]$ env|grep root
   PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/oldboy:/oldgirl:/root/bin
   MAIL=/var/spool/mail/root
   PWD=/root

命令提示符优化
命令提示符信息组成: PS1
设置提示符颜色
PS1='[\e[32;1m][\u@\h \W]\ [\e[0m]
信息添加颜色开始 添加颜色信息 信息添加颜色结束
添加颜色开始: 设置什么颜色 对设置信息有什么特殊效果
如何让命令提示符,显示多个颜色
[root@oldboy63 ~]# export PS1='[\e[31;1m][\u@\h \W]$[\e[0m]'
[root@oldboy63 ~]#export PS1='[\e[32;1m][\u@\h \W]$[\e[0m]'
[root@oldboy63 ~]#export PS1='[\e[33;1m][\u@\h \W]$[\e[0m]'
[root@oldboy63 ~]#export PS1='[\e[34;1m][\u@\h \W]$[\e[0m]'
系统yum 源优化
yum仓库:汇总保存多个软件包的服务
yum源:/etc/yum.repos.d配置好yum源文件,便于找到指定的yum仓库
yum可以解决软件的依赖
优化基础yum(base)
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
优化扩展yum(epel -- Extra Packages for Enterprise Linux)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum install -y vim tree wget dos2unix nc nmap net-tools sl cowsay bash-completion
vim --- 编辑文本 vi升级版
tree ---目录树形结构显示
wget
nc nmap net-tools --- 和网络有关的命令
sl cowsay --- 搞笑软件包
bash-completion --- 对命令参数进行补全
软件无法正常yum下载
问题一: 网络配置不正确 ping www.baidu.com
问题二: yum源是否更新 是否安装wget
问题三: yum缓存需要清除 yum clean all
下载索引清单信息
查看软件是否安装:
rpm -qa cowsay
查看软件都安装哪些信息:
rpm -ql cowsay

[root@oldboy63 yum.repos.d]# cat epel.repo
cat: epel.repo: No such file or directory
[root@oldboy63 yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo/epel-7.repo
--2019-07-15 17:15:26--  http://mirrors.aliyun.com/repo/epel-
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.7.17102, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.7.1
[root@oldboy63 yum.repos.d]# yum install -y cowsay                               
Resolving Dependencies
--> Running transaction check
---> Package cowsay.noarch 0:3.04-4.el7 will be installed
--> Finished Dependency Resolution
Running transaction test
Transaction test succeeded
Running transaction
  Installing : cowsay-3.04-4.el7.noarch                      
  Verifying  : cowsay-3.04-4.el7.noarch                      

Installed:
  cowsay.noarch 0:3.04-4.el7                                 
Complete!
[root@oldboy63 yum.repos.d]# rpm -qa cowsay
cowsay-3.04-4.el7.noarch

系统安全有关的优化
防护墙优化: 关闭
确认一个主机里面有哪些服务
netstat -lntup --- 查看网络服务端口号码信息

centos6
临时关闭:
/etc/init.d/iptables stop
/etc/init.d/iptables status
永久关闭:
chkconfig iptables off chkconfig iptables on
chkconfig --list|grep iptables
chkconfig --list iptables

 [root@oldboy63 ~]# 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:22              0.0.0.0:*               LISTEN      2003/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2138/master         
tcp6       0      0 :::22                   :::*                    LISTEN      2003/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2138/master         
[root@oldboy63 ~]#

centos 7
临时:
[root@oldboy63 ~]# systemctl start firewalld.service 临时开启
[root@oldboy63 ~]# systemctl stop firewalld.service 临时关闭
[root@oldboy63 ~]# systemctl is-active firewalld.service 检查服务是否临时关闭或开启
active
永久:
[root@oldboy63 ~]# systemctl disable firewalld.service 永久关闭
[root@oldboy63 ~]# systemctl is-enabled firewalld.service 检查服务是否永久关闭或开启
disabled
selinux 优化:
selinux:企业中都会关闭(安全程序和root权限有关)
centos7:
临时:
Enforcing /1 selinux
永久:

[root@oldboy63 ~]# cat  /etc/selinux/config
#     enforcing - SELinux security policy is enforced.
   selinux:安全策略是开启状态
#     ermissive - SELinux prints warnings instead of enforcing.
     selinux: 显示警告信息代替开启状态==临时关闭
#     disabled - No SELinux policy is loaded.
   禁止selinux 策略加载
SELINUX=disabled
[root@oldboy63 ~]# getenforce -- 查看selinux状态 
Enforcing
       Enforcing  /1    --- selinux处于开启状态
       Permissive /0    --- selinux处于临时关闭
[root@oldboy63 ~]# setenforce
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@oldboy63 ~]# setenforce 0
[root@oldboy63 ~]# getenforce
Permissive

修改文件信息:
1.vi 进行编辑
2,替换: :7s#enforcing#disabled#g
3, sed sed -i '7s#enforcing#disabled#g'
[root@oldboy63 ~]# cat -n /etc/selinux/config ---cat -n :显示行数
[root@oldboy63 ~]# sed -i '7s#enforcing#disabled#g' /etc/selinux/config ---sed -i:不进入文件直接编辑

[root@oldboy63 ~]# cat  /etc/selinux/config
      # This file controls the state of SELinux on the system.
          # SELINUX= can take one of these three values:
          #     enforcing - SELinux security policy is enforced.
       #     permissive - SELinux prints warnings instead of enforcing.
        #     disabled - No SELinux policy is loaded.
SELINUX=disabled
         # SELINUXTYPE= can take one of three two values:
           #     targeted - Targeted processes are protected,
         #     minimum - Modification of targeted policy. Only selected processes are protected. 
        #     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

系统字符编码优化: 系统字符编码优化
作用:
1. 避免中文出现乱码
2. 部分信息显示中文
字符编码是什么:
王永民 --- 发明了五笔
UTF-8
gbk

查看系统字符编码:
# echo $LANG
en_US.UTF-8
修改字符编码:
centos6
临时调整:
export LANG="en_US.UTF-8"
永久调整:
vim /etc/sysconfig/i18n
LANG="en_US.utf8
centos7:
临时调整:
export LANG="en_US.UTF-8"
永久调整:
vim /etc/locale.conf
LANG="en_US.utf8
source /etc/locale.conf
localectl set-locale LANG="en_US.UTF-8"
设置系统提示信息为中文:
localectl set-locale LANG="zh_CN.UTF-8"
UTF-8 GBK (这个GBK重启虚拟机后有错误提示,可能不存在此语言?)
系统时间和时区优化
[root@oldboy63 ~]# date
Mon Jul 15 18:47:36 CST 2019
[root@oldboy63 ~]# timedatectl查看时间和时区信息
Local time: Mon 2019-07-15 18:48:04 CST
Universal time: Mon 2019-07-15 10:48:04 UTC
RTC time: Mon 2019-07-15 10:48:04
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@oldboy63 ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
timedatectl set-time 18:49 --- 设置时间信息
timedatectl set-timezone Asia/Shanghai --- 设置时区信息 ******
timedatectl list-timezones --- 显示时区信息
set-local-rtc BOOL ---设置RTC功能是否开启 BOOL(数据布尔型--0/1 false/true)RTC是否修改硬件主板时间
set-ntp BOOL --- 设置NTP功能是否开启,会通过网络自动同步时间
Commands:
status Show current time settings
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Control whether NTP is enabled
[root@oldboy63 ~]# timedatectl set-time 18:49
[root@oldboy63 ~]# timedatectl list-timezones
...skipping...
Africa/Dar_es_Salaam
Asia/Yakutsk
[root@oldboy63 ~]# timedatectl set-timezone Asia/Shanghai
[root@oldboy63 ~]#
手动同步时间方法:
yum install -y ntpdate
ntpdate "ntp1.aliyun.com"

    timedatectl命令操作不了:
    第一步 安装时间同步软件
    yum install -y chrony     
    systemctl start chronyd
    
    第二个步: 修改同步方式
    timedatectl set-ntp 1

手动同步时间:

安装ntpdate:[root@oldboy63 ~]# yum install -y ntpdate
[root@oldboy63 ~]# 
[root@oldboy63 ~]# ntpdate "ntp1.aliyun.com"
15 Jul 19:03:44 ntpdate[37234]: step time server 120.25.115.20 offset 11.227356 sec
[root@oldboy63 ~]# date
Mon Jul 15 19:04:24 CST 2019
 [root@oldboy63 ~]# yum install -y chrony 
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed:
  chrony.x86_64 0:3.2-2.el7                                 

Dependency Installed:
  libseccomp.x86_64 0:2.3.1-3.el7                           
Complete!
[root@oldboy63 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
[root@oldboy63 ~]# systemctl enable  chronyd
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:15:13 CST
  Universal time: Mon 2019-07-15 11:15:13 UTC
        RTC time: Mon 2019-07-15 11:15:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]# timedatectl set-ntp 1
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:15:37 CST
  Universal time: Mon 2019-07-15 11:15:37 UTC
        RTC time: Mon 2019-07-15 11:15:26
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]# timedatectl set-ntp 1
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:16:01 CST
  Universal time: Mon 2019-07-15 11:16:01 UTC
        RTC time: Mon 2019-07-15 11:15:50
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]# timedatectl set-ntp 0
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:16:10 CST
  Universal time: Mon 2019-07-15 11:16:10 UTC
        RTC time: Mon 2019-07-15 11:15:59
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]# timedatectl set-ntp 1
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:16:19 CST
  Universal time: Mon 2019-07-15 11:16:19 UTC
        RTC time: Mon 2019-07-15 11:16:08
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-07-15 19:16:15 CST; 26s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 37984 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 37980 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 37982 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─37982 /usr/sbin/chronyd

Jul 15 19:16:15 oldboy63 systemd[1]: Starting NTP client/...
Jul 15 19:16:15 oldboy63 chronyd[37982]: chronyd version ...
Jul 15 19:16:15 oldboy63 systemd[1]: Started NTP client/s...
Jul 15 19:16:21 oldboy63 chronyd[37982]: Selected source ...
Hint: Some lines were ellipsized, use -l to show in full.
[root@oldboy63 ~]# timedatectl 
      Local time: Mon 2019-07-15 19:16:48 CST
  Universal time: Mon 2019-07-15 11:16:48 UTC
        RTC time: Mon 2019-07-15 11:16:48
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@oldboy63 ~]#

你可能感兴趣的:(DAY 11 操作系统基础优化)