SSH(Secure Shell)是一种加密的网络协议,用于安全地访问远程计算机。在Linux系统中,SSH通常用于远程登录、执行命令、传输文件等

ssh

ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在于,前者是针对客户端的配置文件,后者则是针对服务端的配置文件

ssh命令

1.连接到远程服务器
ssh user@hostname
其中user是远程服务器上的用户名,hostname是远程服务器的IP地址或主机名,使用默认端口22

2.使用特定端口:
ssh -p port user@hostname

3.使用SSH密钥:
ssh -i /path/to/private_key user@hostname

4.执行远程命令:
ssh user@hostname 'command'

5.端口转发(也称为SSH隧道):
ssh -L local_port:localhost:remote_port user@hostname

6.复制文件到远程服务器(使用scp):
scp /path/to/local_file user@hostname:/path/to/remote_directory

6.复制文件从远程服务器(使用scp):
scp user@hostname:/path/to/remote_file /path/to/local_directory

7.查看远程服务器的SSH密钥指纹:
ssh-keyscan hostname

8.生成SSH密钥对:
ssh-keygen -t rsa -b 4096

9.查看SSH配置文件:
cat ~/.ssh/config

#ssh连接配置

SSH配置文件通常位于用户的家目录下的.ssh/config文件中。这个文件可以用来指定默认的用户名、端口、密钥文件等,以简化SSH命令

SSH配置文件可能看起来像这样:
Host myserver
  HostName example.com
  User myusername
  Port 2222
  IdentityFile ~/.ssh/mykey

ssh_config(client)

/etc/ssh/ssh_config
# Site-wide defaults for various options   //定义默认host行为
   Host *
        ForwardAgent no
        ForwardX11 no
        RhostsAuthentication no
        RhostsRSAAuthentication no
        RSAAuthentication yes        #密钥认证
        PasswordAuthentication yes   #密码认证 
        FallBackToRsh no
        UseRsh no
        BatchMode no
        CheckHostIP yes
        StrictHostKeyChecking no
        IdentityFile ~/.ssh/identity
        Port 22
        Cipher blowfish
        EscapeChar

sshd_config(server)

/etc/ssh/sshd_config

# This is ssh server systemwide configuration file.
          Port 22    #监听端口,支持多行填写以此监听多个端口
          ListenAddress 192.168.1.1
          HostKey /etc/ssh/ssh_host_key
          ServerKeyBits 1024
          LoginGraceTime 600
          KeyRegenerationInterval 3600
          PermitRootLogin no  #是否允许root远程登陆
          IgnoreRhosts yes
          IgnoreUserKnownHosts yes
          StrictModes yes
          X11Forwarding no  #X11转发
          PrintMotd yes
          SyslogFacility AUTH
          LogLevel INFO
          RhostsAuthentication no
          RhostsRSAAuthentication no
          RSAAuthentication yes
          PasswordAuthentication yes
          PermitEmptyPasswords no
          AllowUsers admin   #限定登陆用户
          UseDNS no     #禁止解析hostip,加快登陆速度

restart/relaod sshd

# centos6-
/etc/init.d/sshs reload|restart
serivce restart|reload sshd

# centos7+
systemctl sshd restart|reload