- 方法2
ssh -l [远程主机用户名] [远程服务器主机名或IP 地址] -p port
[root@xuegod137 ~]# ssh -l kill 192.168.95.137
-l : -l选项,指定登陆名称
-p : -p 选项,指定登陆端口(当服务端的端口非默认时,需要使用-p指定端口进行登陆) - LogLevel INFO
定义登录记录的等级 - 给sshd服务添加一些警告信息
[root@xuegod137 ssh]# echo ‘Warning ! From now on, all of your operation has been record!’> /etc/motd
#测试:
[root@xuegod137 ssh]# ssh -p 2222 192.168.95.137
root@192.168.95.137’s password:
Last login: Mon Jun 3 06:28:06 2019 from xiaomao1
Warning ! From now on, all of your operation has been record!
# PrintLastLog yes
显示上次登入的信息!默认为 yes
UseDNS yes
一般来说,为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名,但通常在内网互连时,该基设置为no,因此使联机速度会快些
清空防火墙
[root@xuegod136 ~]# iptables -F #清空防火墙规则
[root@xuegod136 ~]# systemctl stop firewalld #关闭防火墙
[root@xuegod136 ~]# systemctl disable firewalld #设置开机不启动防火墙
关闭SElinux
[root@xuegod136 ~]# getenforce #查看SElinux状态
Disabled
# 临时关闭(机器重启则会失效):
[root@xuegod136 ~]# setenforce 0 #临时关闭SElinux,0表示关闭
# 永久关闭SELinux
[root@xuegod136 ~]# cat /etc/selinux/config
SELINUX=disabled
注意:永久关闭Selinux需要重启主机则生效,如果当前主机是生产环境不能重启主机的条件下,但又要实现永久关闭SELinux的情况下,需要先做临时关闭,在修改其配置文件实现永久关闭,但是不需要重启主机
配置静态ip
[root@xuegod136 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=”Ethernet”
PROXY_METHOD=”none”
BROWSER_ONLY=”no”
BOOTPROTO=”static” #设置静态
DEFROUTE=”yes”
IPV4_FAILURE_FATAL=”no”
IPV6INIT=”yes”
IPV6_AUTOCONF=”yes”
IPV6_DEFROUTE=”yes”
IPV6_FAILURE_FATAL=”no”
IPV6_ADDR_GEN_MODE=”stable-privacy”
NAME=”ens33”
DEVICE=”ens33”
ONBOOT=”yes”
IPADDR=”192.168.95.136” #ip地址
PREFIX=”24” #子网掩码
GATEWAY=”192.168.95.2” #网关
DNS1=”8.8.8.8”
DNS2=”114.114.114.114”
IPV6_PRIVACY=”no”
配置主机和IP映射关系
[root@xuegod136 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.95.136 xuegod136.cn xuegod136
192.168.95.137 xuegod137.cn xuegod137
修改主机名
[root@xuegod136 ~]# cat /etc/hostname
xuegod136 #修改主机名配置文件(永久生效,需要重启)
[root@xuegod136 ~]# hostname xuegod136 #临时设置,立即生效(需要重启当前终端)
sshd服务安装-ssh命令使用方法
介绍:SSH协议:安全外壳协议,为Secure shell的缩写。SSH为建立在应用层和传输层基础上的安全协议
作用:SSHD服务使用SSH协议可以用来进行远程控制或在计算机之间传送文件。想比较之前Telnet方式来传输文件要安全很多,因为Telnet使用明文传输,SSH是加密传输
服务安装
[root@xuegod136 ~]# yum -y install openssh openssh-clients openssh-server
确认软件包是否已经安装
[root@xuegod136 ~]# rpm -qa | grep openssh
openssh-clients-7.4p1-16.el7.x86_64
openssh-server-7.4p1-16.el7.x86_64
openssh-7.4p1-16.el7.x86_64
查看软件安装生产的文件
[root@xuegod136 ~]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ctr-cavstest
/usr/libexec/openssh/ssh-keysign
/usr/share/doc/openssh-7.4p1
/usr/share/doc/openssh-7.4p1/CREDITS
/usr/share/doc/openssh-7.4p1/ChangeLog
/usr/share/doc/openssh-7.4p1/INSTALL
/usr/share/doc/openssh-7.4p1/OVERVIEW
/usr/share/doc/openssh-7.4p1/PROTOCOL
/usr/share/doc/openssh-7.4p1/PROTOCOL.agent
/usr/share/doc/openssh-7.4p1/PROTOCOL.certkeys
/usr/share/doc/openssh-7.4p1/PROTOCOL.chacha20poly1305
/usr/share/doc/openssh-7.4p1/PROTOCOL.key
/usr/share/doc/openssh-7.4p1/PROTOCOL.krl
/usr/share/doc/openssh-7.4p1/PROTOCOL.mux
/usr/share/doc/openssh-7.4p1/README
/usr/share/doc/openssh-7.4p1/README.dns
/usr/share/doc/openssh-7.4p1/README.platform
/usr/share/doc/openssh-7.4p1/README.privsep
/usr/share/doc/openssh-7.4p1/README.tun
/usr/share/doc/openssh-7.4p1/TODO
/usr/share/licenses/openssh-7.4p1
/usr/share/licenses/openssh-7.4p1/LICENCE
/usr/share/man/man1/ssh-keygen.1.gz
/usr/share/man/man8/ssh-keysign.8.gz
启动服务
[root@xuegod136 ~]# systemctl restart sshd #重启sshd服务
[root@xuegod136 ~]# systemctl start sshd #开启sshd服务
[root@xuegod136 ~]# systemctl enable sshd #设置开机自启动
如何使用SSH来远程连接主机
# ssh [远程主机用户名]@[远程服务器主机或IP地址] -p port
# 当在Linux主机上远程连接另一台Linux主机时,如当前所登陆的用户是root的话,当连接另一台主机时也是用root用户
登陆时,可以直接用ssh IP,端口默认即可,如果端口不是默认的情况下,需要使用-p 指定端口
[root@xuegod136 ~]# ssh 192.168.95.137
The authenticity of host ‘192.168.95.137 (192.168.95.137)’ can’t be established.
ECDSA key fingerprint is SHA256:qVBJa2YNbjjjOh5BHrFGFvlfAZLB62sZYYIwe8zZyOo.
ECDSA key fingerprint is MD5:64:6a:73:86:c0:ac:94:ef:9b:ed:4c:3a:dc:b2:36:9c.
Are you sure you want to continue connecting (yes/no)?
# 第一次登陆服务器时系统没有保存远程主机的信息,为了确认该主机身份会提示是否继续连接,输入yes后登录,这时
系统会将远程服务器器信息写入用户主目录下的$HOME/.ssh/known_hosts文件中,下次再进行登陆时因为保存有该主机
信息就不会在提示了,如图所示
使用普通用户登陆
[root@xuegod136 ~]# ssh 192.168.95.137
[root@xuegod137 ~]#useradd kill && echo root | passwd —stdin kill
[root@xuegod137 ~]# ssh kill@192.168.59.137
方法2
ssh -l [远程主机用户名] [远程服务器主机名或IP 地址] -p port
[root@xuegod137 ~]# ssh -l kill 192.168.95.137
-l : -l选项,指定登陆名称
-p : -p 选项,指定登陆端口(当服务端的端口非默认时,需要使用-p指定端口进行登陆)
SSHD服务配置和管理
配置文件详解
注:在配置文件中参数前面有#号,表示是默认值,当然#号也表示注释
/etc/ssh/sshd_config 配置文件内容详解
设置SSHD监听端口号:
1、SSH 预设使用22这个port,也可以使用多个port,即重复使用port这个设定项
2、例如想要开放SSHD端口为22和222,则多加一行内容为:Port 222即可
3、然后重新启动SSHD这样就好了,建议大家修改port number为其他端口,防止别人暴力破解
# 例1:修改SSHD服务默认监听的端口为2222
[root@xuegod137 ~]# cd /etc/ssh/
[root@xuegod137 ~]# cp sshd_config sshd_config.bak
改:
#Port 22
为:
Port 2222
[root@xuegod137 ~]# systemctl restart sshd
[root@xuegod137 ~]# netstat -tlunp | grep sshd
tcp 0 0 0.0.0.0:2222 0.0.0.0: LISTEN 36766/sshd
tcp6 0 0 :::2222 ::: LISTEN 36766/sshd
# 修改完默认端口后,登陆方法:
[root@xuegod137 ~]#ssh -p 2222 192.168.10.129
- ListenAddress 0.0.0.0
- 设置SSHD服务器绑定的IP地址,0.0.0.0 表示侦听所有地址
- 安全建议:如果主机不需要从公网ssh访问,可以把监听地址改为内网地址
- 这个值可以写成本地IP地址,也可以写成所有地址,即0.0.0.0 表示所有IP
Protocol 2
- 选择的 SSH 协议版本,可以是 1 也可以是 2,CentOS 5.x 预设是仅支援V2版本,出于安全考虑,设置为最新的协议版本
3.HostKey /etc/ssh/ssh_host_key
设置包含计算机私人密钥的文件SyslogFacility AUTHPRIV
当有人使用 SSH 登入系统的时候,SSH 会记录信息,这个信息要记录的类型为AUTHPRIV,sshd服务日志存放在:/var/log/secure。
例:为什么sshd配置文件中没有指定日志,但日志却存放在了/var/log/secure?
LogLevel INFO
定义登录记录的等级
安全调优的重点
- LoginGraceTime 2m
- grace 意思是系统给与多少秒来进行登陆
- 当使用者连上SSH server之后,会出现输入密码的画面,在该画面中
- 在多久时间内没有成功连接上的SSH server 就强迫断线!若无单位则默认时间为秒。
可以根据实际情况来修改实际
2. PasswordAuthentication yes
密码验证当然是需要的!所以这里写 yes,也可以设置为no,在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆。
3.PermitRootLogin yes
是否允许 root 登入,默认是允许的,但是建议设定成 no,真实的生产环境服务器,是不允许root账号直接登陆的,仅允许普通用户登录,需要用到root用户再切换到root用户。
4.PermitEmptyPasswords no
是否允许空密码的用户登录,默认为no,不允许空密码登录。
给sshd服务添加一些警告信息
[root@xuegod137 ssh]# echo ‘Warning ! From now on, all of your operation has been record!’> /etc/motd
#测试:
[root@xuegod137 ssh]# ssh -p 2222 192.168.95.137
root@192.168.95.137’s password:
Last login: Mon Jun 3 06:28:06 2019 from xiaomao1
Warning ! From now on, all of your operation has been record!
# PrintLastLog yes
显示上次登入的信息!默认为 yes
UseDNS yes
一般来说,为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名,但通常在内网互连时,该基设置为no,因此使联机速度会快些
SSHD服务防止暴力破解
(1) 密码足够的复杂,密码的长度要大于8位最好大于20位。密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成。
(2) 修改默认端口号。
(3) 不允许root账号直接登陆到系统,添加普通账号,使用普通账号登录系统,授予root的权限,必要时再从普通用户切换到root用户。
(4) 不允许密码登陆,只能通过认证的密钥来登陆系统。
通过密钥认证实现SSHD认证
实验环境:
服务端:xuegod136 IP:192.168.95.136
客户端:xuegod137 IP:192.168.95.137
客户端生成密钥对,然后把公钥传输到服务端
[root@xuegod137 ssh]# echo ‘Warning ! From now on, all of your operation has been record!’> /etc/motd
[root@xuegod137 ssh]# ssh 192.168.95.136
Last login: Sun Jul 21 11:37:49 2019 from xuegod137.cn
[root@xuegod136 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #提示输入密钥文件的保存路径,选择默认,回车继续
Enter passphrase (empty for no passphrase): #下面要求输入密码,这里的passphrase 密码是对生成的私钥文件(/root/.ssh/id_dsa)的保护口令,如果不设置可以回车。
Enter same passphrase again: #直接回车。
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+LSWYq72YAWaQJnvuFXS89OL2DwM/FZHpkMr9vcgWtg root@xuegod136
The key’s randomart image is:
+—-[RSA 2048]——+
| .o |
|.o |
|. . o |
| . = = . . o |
| = + = S = |
| . o + BoO . |
| o o @.@E+. |
| . ..= @oo… |
| ..o+.. . .. |
+——[SHA256]——-+
#查看生成的密钥文件
[root@xuegod136 ~]# cd /root/.ssh/
[root@xuegod136 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
发布公钥到服务端
使用ssh-copy-id 命令将客户端生成的公钥发布到远程服务端:xuegod136 IP:192.168.95.136
[root@xuegod136 ~]# ssh-copy-id -i 192.168.95.136
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
The authenticity of host ‘192.168.10.130 (192.168.10.130)’ can’t be established.
ECDSA key fingerprint is SHA256:IF3CKkzrjpH/sc0C3pEtoWR4aLf1dyJcCXkK2LQkeik.
ECDSA key fingerprint is MD5:c7:00:21:82:94:36:b9:b9:fc:03:57:22:0c:c8:67:a6.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
root@192.168.10.130’s password: #输入密码
#这时候可以通过ssh无密钥登陆主机
注意:如果服务器不是监听22端口,则需要指定端口来传输密钥
[root@xiaomao1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 222 root@192.168.10.130