ssh基本管理和配置文件的使用

本节所讲内容:

学习一个服务的过程:

1、了解服务的作用:名字,功能,特点

2、安装

3、配置文件位置,端口

4、服务启动关闭的脚本

5、此服务的使用方法

6、修改配置文件,实战举例

7、排错(从下到上,从内到外)。

 

服务端:xuegod63.cn   IP:192.168.1.63

客户端:xuegod64.cn   IP:192.168.1.64

 

本节所讲内容:

•      SSHD服务介绍

•      SSHD服务安装配置

•      两Linux服务器之间数据拷贝

•      xinetd服务配置和管理

•      telnet服务配置和管理

 

SSHD服务

SSH 协议:安全外壳协议。为 Secure Shell 的缩写。SSH 为建立在应用层和传输层基础上的安全协议。

sshd服务使用SSH协议可以用来进行远程控制,或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)都是极为不安全的,并且会使用明文传送密码。

OpenSSH软件包,提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。

安装包:

OpenSSH 服务需要4个软件包

[root@xuegod63 Packages]#ls openssh*

openssh-5.3p1-94.el6.x86_64.rpm         openssh-clients-5.3p1-94.el6.x86_64.rpm

openssh-askpass-5.3p1-94.el6.x86_64.rpm  openssh-server-5.3p1-94.el6.x86_64.rpm

 

每个软件包的作用:

openssh-5.3p1-94.el6.x86_64.rpm:包含OpenSSH服务器及客户端需要的核心文件

openssh-clients-5.3p1-94.el6.x86_64.rpm:OpenSSH客户端软件包

openssh-server-5.3p1-94.el6.x86_64.rpm:OpenSSH服务器软件包

 

[root@xuegod63 Packages]#rpm -qpi openssh-5.3p1-94.el6.x86_64.rpm

 

[root@xuegod63 ~]# rpm-qa | grep openssh

openssh-server-5.3p1-94.el6.x86_64

openssh-5.3p1-94.el6.x86_64

openssh-clients-5.3p1-94.el6.x86_64

openssh-askpass-5.3p1-94.el6.x86_64

 

OpenSSH 配置文件

OpenSSH 常用配置文件有两个/etc/ssh/ssh_config 和 /etc/ssh/sshd_config

ssh_config 为客户端配置文件

sshd_config 为服务器端配置文件

 

服务启动和关闭

方法1:

[root@xuegod63 ~]#service sshd restart | start | stop | status | reload

方法2:

[root@xuegod63 ~]#/etc/init.d/sshd start | stop | restart | status | reload

 

开机自动启动服务:

[root@xuegod63 ~]#chkconfig sshd on

[root@xuegod63 ~]#chkconfig --list sshd

sshd            0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

[root@xuegod63 ~]#chkconfig sshd off

[root@xuegod63 ~]#chkconfig --list sshd

sshd            0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭

 

SSH运程登录:身份验证

(1)口令验证

ssh [远程主机用户名] @[远程服务器主机名或IP地址]

如果用root进程登录远程主机可以省略用户名:

[root@xuegod63 ~]# ssh192.168.1.64

The authenticity of host'192.168.1.64 (192.168.1.64)' can't be established.

RSA key fingerprint is4c:72:24:25:ca:13:69:06:a8:77:38:6d:cf:2c:66:76.

Are you sure you want tocontinue connecting (yes/no)? yes

Warning: Permanentlyadded '192.168.1.64' (RSA) to the list of known hosts.

root@192.168.1.64'spassword:

 

普通用户

[root@xuegod64 ~]#useradd rm

[root@xuegod64 ~]# echo123456 | passwd --stdin rm

更改用户 rm 的密码。

passwd: 所有的身份验证令牌已经成功更新。

 

[root@xuegod63 ~]# sshrm@192.168.1.64

第一次登录服务器时系统没有保存远程主机的信息,为了确认该主机身份会提示用户是否继续连

接,输入yes 后登录,这时系统会将远程服务器信息写入用户主目录下的$HOME/.ssh/known_hosts 文件中,下次再进行登录时因为保存有该主机信息就不会再提示了

[root@xuegod63 ~]#cat  ./.ssh/known_hosts

192.168.1.64 ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAzTNFSEIzwAVbEVyH147K1Na+jyYXtOr0JqPRzLn8wvnieFuKW0e7Ad2RmfZIVqwzA+NmBEMsAeiJAzlX+4hDB7Gt7f/04QR8pVV0CHt6lacaLhECHHDGkdE6QUfFYI1LhUPG3y7kOq+dXBnn2YxNCf+jMhJ5oyaqLwyXOZf7wk4QhdnHq0fRugUB6Oae6iNbN5hV+i5Ph4GlqCcTveSsn9oJE7BiafPhL0IIHyYcpU29Ut38V4oxcB4M8JUEzOfZyJkmcUHnJGzFGGTISzhWogWeTbXLh1/7GmlcWS0PJG/vx9ZqbtuOGqHMrWS6EDxgTYeP7jWXcF1sCK6JAvuoFQ==

 

或:

ssh -l [远程主机用户名] [远程服务器主机名或IP 地址]

例:[root@xuegod63~]# ssh -l rm 192.168.1.64

-l login_name

 

先整体看一下服务器端的配置文件

注:参数前面有#,表示是默认值。 当然#号也表示注释。

/etc/ssh/sshd_config配置文件

 

Port 22

# SSH 预设使用 22 这个port,也可以使用多个port,即重复使用 port 这个设定项目!

# 例如想要开放 sshd 端口为 22和 222 ,则多加一行内容为:Port 222  即可

# 然后重新启动 sshd 这样就好了。 建议大家修改 port number 为其它端口。防止别人暴力破解。

 

例:修改sshd服务默认监听的端口为22和222

[root@xuegod63 ~]# vim/etc/ssh/sshd_config

改:

Port 22

为:

Port 22

Port 222

[root@xuegod63 ~]#service sshd restart

测试

[root@xuegod63 ~]#netstat -anptu | grep ssh

 

 

修改完端口默认端口后,登录方法:

[root@xuegod64 ~]# ssh -p222 192.168.1.63

ListenAddress 0.0.0.0

设置sshd 服务器绑定的IP 地址,0.0.0.0 表示侦听所有地址

例:虚拟机,添加一块网卡。让sshd服务器只在192.168.2.63 网络接口上监听

 

关机Linux机器,添加一块网卡。

通过setup命令生成配置eth1配置文件。

 

 

重启网络

service network restart

ifup eth1

 

[root@xuegod163 ~]# vim/etc/ssh/sshd_config

改:

15 #ListenAddress 0.0.0.0

为:

15 ListenAddress  192.168.2.63

 

[root@localhost network-scripts]# servicesshd restart

测试:

[root@xuegod163 ~]# netstat -anptu | grep22

tcp       0      0 192.168.2.163:22            0.0.0.0:*                   LISTEN      3024/sshd

tcp       0     64 192.168.1.163:22            192.168.1.3:52978           ESTABLISHED 2459/sshd

 

[root@xuegod64 ~]# ssh -p222 192.168.1.63

ssh: connect to host192.168.1.63 port 222: Connection refused

 

Protocol 2

# 选择的 SSH 协议版本,可以是 1 也可以是 2 ,CentOS 5.x 预设是仅支援 V2。

# 如果想要支持旧版 V1 ,

改:

Protocol 2

为:

Protocol 2,1 

 

 

SyslogFacility AUTHPRIV

# 当有人使用 SSH 登入系统的时候,SSH 会记录信息,这个信息要记录的类型为AUTHPRIV。

 

互动:默认日志存放在哪?

sshd服务日志存放在: /var/log/secure。

 

例: 为什么sshd配置文件中没有指定日志,但日志却存放在了: /var/log/secure?

[root@xuegod ssh]# vim/etc/rsyslog.conf   查看:

 

 

#LogLevel INFO

# 登录记录的等级!INFO级别以上。

 

#ServerKeyBits 1024

定义服务器密匙长度

 

安全设定项

# PermitRootLogin yes

 

#是否允许 root 登入!预设是允许的,但是建议设定成 no !

#PermitEmptyPasswords no

# 若上面那一项如果设定为 yes 的话,这一项就最好设定为 no ,

# 这个项目在是否允许以空的密码登入!当然不许!

#PasswordAuthenticationyes

#是否允许使用密码验证,默认为允许

 

#LoginGraceTime 2m      #grace优雅

# 当使用者连上 SSHserver 之后,会出现输入密码的画面,在该画面中,

# 在多久时间内没有成功连上 SSH server 就强迫断线!若无单位则默认时间为秒!

 

LoginGraceTime 5

改:LoginGraceTime 2m  为:LoginGraceTime 5  ,重启后

测试:

[root@xuegod64 ~]# ssh 192.168.1.63

停留5秒后,再次输入,结果断开

root@192.168.1.63's password:

Connection closed by192.168.1.63

 

# PrintMotd yes

# 打印出 /etc/motd 这个文档的内容。

 

[root@xuegod ~]# cat/etc/motd

[root@xuegod ~]# echo "警告!从现在开始,你所有的操作已经被记录!" > /etc/motd

测试:

ssh 192.168.1.63

Last login: Tue Nov  4 19:57:31 2014 from 192.168.1.107

警告!从现在开始,你所有的操作已经被记录!

 

# PrintLastLog yes

# 显示上次登入的信息!预设也是 yes  !

Last login: Wed Mar 2322:12:58 2016 from 192.168.1.100

 

改:

PrintLastLog yes

为:

PrintLastLog no

就不显示这个信息

 

UsePrivilegeSeparation yes

# 是否权限较低的程序来提供用户操作。我们知道 sshd 启动在 port 22 ,

# 因此启动的程序是属于 root 的身份。那么当 student 登入后,这个设定值

 

# 会让 sshd 产生一个属于 sutdent 的 sshd 程序来使用,对系统较安全

[root@xuegod64 ~]# sshrm@192.168.1.63

这样,当这个进程sshd被缓存区溢出等手段,得到登录的权限后,还是rm普通用户。不会得到root身份。

 

# UseDNS yes

 

#一般来说,为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名 # 不过如果是在内网互连,这项目设定为 no 会让联机速度比较快。

改:UseDNS yes

为:UseDNS no

改:GSSAPIAuthentication yes

为:GSSAPIAuthentication no

 

# PidFile /var/run/sshd.pid

# 可以放置 SSHD 这个 PID 的文档!上述为默认值

 

[root@xuegod63 ~]# cat/var/run/sshd.pid

3874

[root@xuegod63 ~]# ps -aux |grep 3874

Warning: bad syntax, perhaps abogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root       3874 0.0  0.0  66604 1232 ?        Ss   22:28  0:00 /usr/sbin/sshd

root      3926  0.0  0.0 103256  852 pts/2    S+   22:30  0:00 grep 3874
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值