[root@centos7 ~]# rpm -qa openssh-server
openssh-server-7.4p1-21.el7.x86_64
要想通过ssh链接远程服务器,对方服务器需要安装ssh服务器端程序,并开启对应服务。
CentOS中默认使用的是openssh-server,进程名称为sshd,默认监听的网络端口是22。
[root@centos7 ~]# rpm -ql openssh-server
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
/usr/lib/systemd/system/sshd-keygen.service
/usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/sshd.socket
/usr/lib/systemd/system/[email protected]
/usr/lib64/fipscheck/sshd.hmac
/usr/libexec/openssh/sftp-server
/usr/sbin/sshd
/usr/sbin/sshd-keygen
/usr/share/man/man5/moduli.5.gz
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
/var/empty/sshd
修改配置⽂件中的Port 配置参数
#配置文件路径
vim /etc/ssh/sshd_config
修改为:
······
#Port 9527
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
······
#重启sshd服务
[root@centos7 ~]# systemctl restart sshd
给node1主机在添加⼀个ip地址172.20.1.111
[root@centos6 data]# ip a a 172.20.3.111/16 dev eth0
[root@centos6 data]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1c:c0:2e brd ff:ff:ff:ff:ff:ff
inet 172.20.3.6/16 brd 172.20.255.255 scope global eth0
inet 172.20.3.111/16 scope global secondary eth0
inet6 fe80::20c:29ff:fe1c:c02e/64 scope link
valid_lft forever preferred_lft forever
修改ssh的配置⽂件,指定监听的ip地址为172.20.1.111(此处配置的是node1)
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
#Port 22
#AddressFamily any
ListenAddress 172.20.3.111
#ListenAddress ::
······
#重启sshd服务
[root@centos6 ~]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
#查看端口
[root@centos6 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::41310 :::*
LISTEN 0 128 *:53324 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 172.20.3.111:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
然后使⽤node2链接172.20.3.6 链接失败,链接172.20.3.111成功。
[root@centos7 ~]# ssh 172.20.3.6
ssh: connect to host 172.20.3.6 port 22: Connection refused
[root@centos7 ~]# ssh 172.20.3.111
[email protected]'s password:
Last login: Thu Nov 7 17:51:31 2019 from 172.20.1.11
[root@centos6 ~]#
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
#Port 22
#AddressFamily inet
ListenAddress 172.20.3.111
#ListenAddress ::
······
[root@centos6 ~]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@centos6 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::41310 :::*
LISTEN 0 128 *:53324 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 172.20.3.111:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
SyslogFacility AUTHPRIV
LogLevel INFO
······
[root@centos6 ~]# ll /var/log/secure
-rw------- 1 root root 18489 Nov 7 18:12 /var/log/secure
LoginGraceTime 指的是登录输入密码的最长时间,2m内容不输入密码,则结束本次链接,0代表不做限制。
PermitRootLogin 指的是是否允许root远程登录。yes代表允许, no代表不允许。
StrictModes 指的是在接受登录之前 是否检查文件权限和所有者信息和家目录信息,默认要检查。
MaxAuthTries 每次登录输入密码错误次数,默认是6次。
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
LoginGraceTime 2m
PermitRootLogin yes
StrictModes yes
MaxAuthTries 6
MaxSessions 10
······
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
PubkeyAuthentication yes
PasswordAuthentication yes
······
X11Forwarding 指定是否支持X11转发;
ClientAlivedInterval 非活动时间限制,0为不限制;
ClientAlivedCountMax 指定满足非活动时间次数,因此ClientAlivedInterval乘以ClientAlivedCountMax
的值为最长终非活动时间;
UseDNS 是否需要域名反解析,建议设置为no 这样可以提高链接速度。
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为:
······
X11Forwarding yes
ClientAlivedInterval 0
ClientAlivedCountMax 3
······
设置banner,后⾯跟上提⽰的⽂本信息
vim /etc/ssh/sshd_config
修改为:
······
Banner /root/sshbanner.txt
······
重启sshd服务,重新连接172.20.3.111可看到登录信息
[root@centos7 ~]# ssh 172.20.3.111
Hello Nanjing_Bokebi!!!
[email protected]'s password:
Last login: Thu Nov 7 19:30:33 2019 from 172.20.3.7
[root@centos6 ~]#
AllowUsers 允许登录的用户;
DenyUsers 不允许登录的用户;
AllowGroups 允许登录的组;
DenyGroups 不允许登录的组
在node1上创建3个⽤户,并配置对应的密码:
[root@centos6 ~]# useradd gordon
[root@centos6 ~]# passwd gordon
Changing password for user gordon.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@centos6 ~]# useradd tom
[root@centos6 ~]# passwd tom
Changing password for user tom.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@centos6 ~]# useradd alice
[root@centos6 ~]# passwd alice
Changing password for user alice.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
sshd配置⽂件中添加相关的访问控制选项
[root@centos6 ~]# vim /etc/ssh/sshd_config
修改为
······
AllowUsers gordon tom
DenyUsers tom
AllowGroups alice gordon
DenyGroups alice
······
实际登录测试时,只有gordon可以登录成功,tom和alice再输⼊密码后被拒绝,要求再次输⼊密码;因此,有拒绝的⽤户
和组,肯定会被拒绝。