02-SSH排错和基础优化


SSH排错和基础优化

作者:Ago

归档:学习笔记

2016/12/30

 

目 录

第1章 解释IP 端口 协议... 1

1.1 ip. 1

1.2 端口... 1

1.3 协议... 1

第2章 SSH连接排错... 1

2.1 连接示意图... 1

2.2 环境的搭建的排错 (物理 检查网线与路由)... 2

2.3 环境搭检查好后,用网络排查手段... 2

2.3.1 检查ifconfig,查看网卡配置是否正确。... 2

2.3.2 在Windows ping,检查网络是否畅通... 3

2.3.3 检查服务对应的端口是否打开(在Windows上检查)... 3

2.3.4 检查linux 的防火墙(iptables)是否关闭... 3

2.3.5 检查linux是否关闭... 4

第3章 基础优化... 4

3.1. 4

3.2. 4

3.3 添加普通用户账号... 4

3.4 使用阿里云镜像做yum源... 5

3.5 关闭SELinux功能... 6

3.6 设定运行级别为3(文本模式,完全多用户模式)... 6

3.7 关闭防火墙(iptables)。... 7

3.8 字符集的设置... 7

3.9 linux特殊变量====调整历史记录以及终端超时... 8

3.10 隐藏linux版本信息显示... 8

第1章 解释IP 端口 协议

1.1 ip

网协是怎样实现的?网络互连设备,如以太网、分组交换网等,它们相互之间不能互通,不能互通的主要原因是因为它们所传送数据的基本单元(技术上称之为“帧”)的格式不同。IP协议实际上是一套由软件、程序组成的协议软件,它把各种不同“帧”统一转换成“网协数据包”格式,这种转换是因特网的一个最重要的特点,使所有各种计算机都能在因特网上实现互通,即具有“开放性”的特点。

1.2 端口

服务器一般都是通过知名端口号来识别的。例如,对于每个 T C P / I P实现来说, F T P服务

器的T C P端口号都是2 1,每个Te l n e t服务器的T C P端口号都是2 3,每个T F T P (简单文件传送协议)服务器的U D P端口号都是 6 9。任何T C P / I P实现所提供的服务都用知名的 1~1 0 2 3之间的端口号。这些知名端口号由 I n t e r n e t号分配机构( Internet Assigned Numbers Authority, IANA)

来管理

1.3 协议

不同的服务提供了不同的端口,对应了不同的功能,通信方式可能也是不同的,这种通信的方式我们就可以理解位协议。

一台主机上的不同服务功能,就是通过端口,不同的服务端口,客户端和服务器之间通信,就可能使用。

第2章 SSH连接排错

2.1 连接示意图

clip_image001

2.2 环境的搭建的排错 (物理 检查网线与路由)

虚拟机的服务是否全部开启,

clip_image002

NAT的搭建,网关与linux配置的IP是否处于同一网段。DHCP服务的开启,以及网卡的使用

clip_image003

2.3 环境搭检查好后,用网络排查手段

2.3.1 检查ifconfig,查看网卡配置是否正确。
  1 [root@AIGE ~]# ifconfig
  2 
  3 eth0 Link encap:Ethernet HWaddr 00:0C:29:2E:26:3C
  4 
  5 inet addr:192.168.101.129 Bcast:192.168.101.255 Mask:255.255.255.0
  6 
  7 inet6 addr: fe80::20c:29ff:fe2e:263c/64 Scope:Link
  8 
  9 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 10 
 11 RX packets:110896 errors:0 dropped:0 overruns:0 frame:0
 12 
 13 TX packets:28523 errors:0 dropped:0 overruns:0 carrier:0
 14 
 15 collisions:0 txqueuelen:1000
 16 
 17 RX bytes:72141056 (68.7 MiB) TX bytes:2906560 (2.7 MiB)
 18 
2.3.2 在Windows ping,检查网络是否畅通
  1 ping 192.168.101.129
  2 
  3 正在 Ping 192.168.101.129 具有 32 字节的数据:
  4 
  5 来自 192.168.101.129 的回复: 字节=32 时间<1ms TTL=64
  6 
  7 来自 192.168.101.129 的回复: 字节=32 时间<1ms TTL=64
  8 
  9 来自 192.168.101.129 的回复: 字节=32 时间<1ms TTL=64
 10 
 11 来自 192.168.101.129 的回复: 字节=32 时间<1ms TTL=64
 12 
 13 192.168.101.129 的 Ping 统计信息:
 14 
 15 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
 16 
 17 往返行程的估计时间(以毫秒为单位):
 18 
 19 最短 = 0ms,最长 = 0ms,平均 = 0ms
 20 
2.3.3 检查服务对应的端口是否打开(在Windows上检查)
  1 telnet 192.168.101.129 22
  2 
  3 Connecting to 192.168.101.129:22...
  4 
  5 Connection established.
  6 
  7 To escape to local shell, press 'Ctrl+Alt+]'.
  8 
  9 SSH-2.0-OpenSSH_5.3
 10 
 11 Protocol mismatch.
 12 
 13 Connection closed by foreign host.
 14 
 15 Disconnected from remote host(192.168.101.129:22) at 07:46:14.
 16 
 17 Type `help' to learn how to use Xshell prompt.
 18 
2.3.4 检查linux 的防火墙(iptables)是否关闭
  1 [root@AIGE ~]# /etc/init.d/iptables status
  2 
  3 iptables: Firewall is not running.
  4 
  5 [root@AIGE ~]#
  6 
  7 没有关闭的话,用该命令关闭iptables
  8 
  9 [root@AIGE ~]# /etc/init.d/iptables stop
 10 
 11 iptables: Setting chains to policy ACCEPT: filter [ OK ]
 12 
 13 iptables: Flushing firewall rules: [ OK ]
 14 
 15 iptables: Unloading modules: [ OK ]
 16 
 17 [root@AIGE ~]#
 18 
2.3.5 检查linux是否关闭
  1 [root@AIGE ~]# getenforce 
  2 
  3 Enforcing
  4 
  5 [root@AIGE ~]#
  6 
  7 没有关闭的话,用修改配置文件(永久生效)和命令(临时生效)的方式使之全部关闭
  8 
  9 [root@AIGE ~]# cat /etc/selinux/config 
 10 
 11 # This file controls the state of SELinux on the system.
 12 
 13 # SELINUX= can take one of these three values:
 14 
 15 # enforcing - SELinux security policy is enforced.
 16 
 17 # permissive - SELinux prints warnings instead of enforcing.
 18 
 19 # disabled - No SELinux policy is loaded.
 20 
 21 SELINUX=enforcing ==>改成disabled
 22 
 23 # SELINUXTYPE= can take one of these two values:
 24 
 25 # targeted - Targeted processes are protected,
 26 
 27 # mls - Multi Level Security protection.
 28 
 29 SELINUXTYPE=targeted
 30 
 31 使用sed在命令行直接改文件内容,也可以使用vi编辑器,改变之前需要做好备份。
 32 
 33 sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
 34 
 35 命令行:
 36 
 37 [root@AIge ~]# setenforce 
 38 
 39 usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
 40 
 41 [root@AIge ~]# setenforce 0
 42 
 43 [root@AIge ~]# getenforce 
 44 
 45 Permissive
 46 
 47 [root@AIge ~]#
 48 

第3章 基础优化

3.1 添加普通用户账号

linux/unix是一个多用户,多任务的操作系统。

超级管理员(root),root默认在unix/linux操作系统中拥有最高的管理权限。

普通用户:管理员或者具备管理员权限的用户创建的。权限:系统管理员仅可以读,看,不能增删改。

权限越大,责任越大。

创建用户用useradd命令(adduser)。

修改(设置)用户密码用passwd来设置

  1 [root@AIge ~]# useradd aige
  2 
  3 [root@AIge ~]# passwd aige (交互式)
  4 
  5 Changing password for user aige.
  6 
  7 New password:
  8 
  9 BAD PASSWORD: it is too simplistic/systematic
 10 
 11 BAD PASSWORD: is too simple
 12 
 13 Retype new password:
 14 
 15 passwd: all authentication tokens updated successfully.
 16 
 17 [root@AIge ~]# echo 123456|passwd --stdin aige (非交互式)
 18 
 19 Changing password for user aige.
 20 
 21 passwd: all authentication tokens updated successfully.
 22 
 23 [root@AIge ~]#
 24 
 25 切换用户:
 26 
 27 [root@AIge ~]# su - aige (超级用户去普通用户无需密码)
 28 
 29 [aige@AIge ~]$ su - root (普通用户去超级用户需要超级用户的密码)
 30 
 31 Password:
 32 
 33 [root@AIge ~]#
 34 

说明:

1. 超级用户root切换到普通用户下面,无需输入对应用户密码,

2. 普通用户切换到root或其他普通用户下,需要输入切换的对应用户密码

3. 普通用户的权限比较小,只能进行基本的系统信息查看等操作,无法更改系统配置和管理服务

4. $符号是普通用户的命令行提示符,#是超级用户命令行的提示符

  1 提示符:
  2 
  3 [root@AIge ~]# echo $PS1
  4 
  5 [\u@\h \W]\$
  6 
  7 [root@AIge ~]#
  8 

3.2 使用阿里云镜像做yum源

http://mirrors.aliyun.com/(阿里云镜像网址)

做好备份,换掉yum的源(文件) 生成缓存,即可。

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

yum makecache

解释:网络太远,会导致网速的变慢,例如北京和福建的网速,是不同的地方提供服务的

3.3 关闭SELinux功能

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。

  1 [root@AIGE ~]# getenforce 
  2 
  3 Enforcing
  4 
  5 [root@AIGE ~]#
  6 

没有关闭的话,用修改配置文件(永久生效)和命令(临时生效)的方式使之全部关闭

  1 [root@AIGE ~]# cat /etc/selinux/config 
  2 
  3 # This file controls the state of SELinux on the system.
  4 
  5 # SELINUX= can take one of these three values:
  6 
  7 # enforcing - SELinux security policy is enforced.
  8 
  9 # permissive - SELinux prints warnings instead of enforcing.
 10 
 11 # disabled - No SELinux policy is loaded.
 12 
 13 SELINUX=enforcing ==>改成disabled
 14 
 15 # SELINUXTYPE= can take one of these two values:
 16 
 17 # targeted - Targeted processes are protected,
 18 
 19 # mls - Multi Level Security protection.
 20 
 21 SELINUXTYPE=targeted
 22 

使用sed在命令行直接改文件内容,也可以使用vi编辑器,改变之前需要做好备份。

  1 sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
  2 
  3 命令行:
  4 
  5 [root@AIge ~]# setenforce 
  6 
  7 usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
  8 
  9 [root@AIge ~]# setenforce 0
 10 
 11 [root@AIge ~]# getenforce 
 12 
 13 Permissive
 14 
 15 [root@AIge ~]#
 16 

3.4 设定运行级别为3(文本模式,完全多用户模式)

3.5 关闭防火墙(iptables)。

在企业环境中,一般只有配置外网的IP的linux系统才需要配置防火墙规则,并且开启。内网一般不会受到攻击,因为防火墙是一层层根据规则过滤的,所以高并发的业务服务仍然是不可以开的。应该高并发的效率。用户体验不好。

关闭linux防火墙的具体操作

  1 命令行临时生效:
  2 
  3 [root@AIGE ~]# /etc/init.d/iptables status
  4 
  5 iptables: Firewall is not running.
  6 
  7 [root@AIGE ~]#
  8 
  9 没有关闭的话,用该命令关闭iptables
 10 
 11 [root@AIGE ~]# /etc/init.d/iptables stop
 12 
 13 iptables: Setting chains to policy ACCEPT: filter [ OK ]
 14 
 15 iptables: Flushing firewall rules: [ OK ]
 16 
 17 iptables: Unloading modules: [ OK ]
 18 
 19 开机不启动:(永久生效)用chkconfig命令。
 20 
 21 [root@AIge ~]# chkconfig |grep iptables
 22 
 23 iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 24 
 25 [root@AIge ~]# chkconfig iptables off
 26 
 27 [root@AIge ~]# chkconfig |grep iptables
 28 
 29 iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
 30 
 31 [root@AIge ~]#
 32 

3.6 字符集的设置

查看字符集:

  1 [root@AIge ~]# cat /etc/sysconfig/i18n 
  2 
  3 LANG="en_US.UTF-8"
  4 
  5 SYSFONT="latarcyrheb-sun16"
  6 
  7 [root@AIge ~]# echo $LANG
  8 
  9 en_US.UTF-8
 10 

默认是UTF8的字符集。可以不修改。

解决乱码:

1.系统字符集(utf8)

2.xshell软件的字符集保持一致(utf8)

3.软件(文件)的编码也得是(utf8)

修改字符集:

命令行: (临时)

  1 [root@AIge ~]# LANG=zh_CN.utf8
  2 
  3 [root@AIge ~]# echo $LANG
  4 
  5 zh_CN.utf8
  6 

配置文件修改: (永久)

  1 [root@AIge ~]# echo "LANG="zh_CN.utf8"" > /etc/sysconfig/i18n 
  2 
  3 [root@AIge ~]# cat /etc/sysconfig/i18n
  4 
  5 LANG="zh_CN.utf8"
  6 
  7 [root@AIge ~]#
  8 
  9 [root@AIge ~]# echo "LANG="en_US.UTF-8"" > /etc/sysconfig/i18n 
 10 
 11 [root@AIge ~]# source /etc/sysconfig/i18n (source加载一下也可以直接生效)
 12 
 13 [root@AIge ~]#
 14 

LANG是环境变量,所以经常用

export是其变成全局变量,让所有bash都用此变量

locale -a 查看所有LANG的环境变量取值

3.7 linux特殊变量====调整历史记录以及终端超时

命令行:(临时)

export TMOUT=3 #3秒之后,没有任何输入,则自动退出。timed out waiting for input :auto-logout

export HISTSIZE=5 #historu size 命令对应的历史记录文件,文件命令记录数 history命令显示的条数

export HISTFILESIZE=5 #命令行命令对应的历史,文件中命令记录数“~/.bash_history”

3.8 隐藏linux版本信息显示

在虚拟机登录的提示符

clip_image004

配置文件/etc/issue本地虚拟机登录时候的提示符

清空,或者填写自己想要的内容

[root@AIge ~]# >/etc/issue (清空即可)

posted @ 2017-01-28 12:38  汉克书  阅读(588)  评论(1编辑  收藏  举报