美文网首页程序员
ssh-简单配置

ssh-简单配置

作者: Mr_Normal | 来源:发表于2018-06-04 10:50 被阅读0次

    一、配置文件

    1.1 全局配置

    • /etc/ssh/sshd_config ssh服务器设置
    • /etc/ssh/ssh_config ssh客户端设置

    Note: 建议在修改配置之前先把配置文件备份一下。

    如果是直接修改的配置文件,把文件改乱了,可以使用下面的命令获取原始的配置文件。注意先要把已修改的文件删除或重命名才能恢复。

    CentOS

    $ yum reinstall openssh-server
    

    1.2 用户配置

    • ~/.ssh/config

    二、server端设置

    1. 公钥验证

    默认开启

    sshd_config 配置

    PubkeyAuthentication yes 
    AuthorizedKeysFile .ssh/authorized_keys  #公钥文件
    

    Note:如果需要禁用公钥验证,必须显示设置PubkeyAuthentication no,因为公钥验证是默认开启的。

    公钥默认存放在文件~/.ssh/authorized_keys

    如果设置之后无法使用公钥连接,检查authorized_keys的权限,确保为0644
    检查~/.ssh目录,确认为0744,虽然下面参考链接中说的是0700,我0744也能用啊哈哈。
    SuperUser参考链接

    2. 密码验证

    默认开启

    sshd_config配置

    PasswordAuthentication yes
    

    3. 设置验证方式

    可以在sshd_config文件中设置验证方式,比如可以设置公钥和密码双重验证,用到的设置为AuthenticationMethods

    AuthenticationMethods password,publickey hostbased
    

    上面的设置意为接受(密码,公钥)或(hostbased)验证,所有的验证方式名可以在ssh_config的手册中的PreferredAuthentications一项中找到

    4. Match-条件化设置

    功能:当Match这一行的所有语句满足时,在其后面的设置语句将覆盖全局设置。

    例如现在想要设置pi用户从局域网(即192.168.1.*)访问只需提供公钥,而其它情况需要同时提供公钥和密码,sshd_config如下

    AuthenticationMethods password,publickey
    Match address 192.168.1.0/24 user pi
        AuthenticationMethods publickey
    # end of sshd_config
    

    注意上面的ip地址格式为CIDR 格式。

    当然还有更多的Match功能比如User、group,请使用man sshd_config查看

    三、client端设置

    1. 公钥验证

    私钥文件~/.ssh/id_rsa

    如果没有公钥私钥对,可以使用ssh-keygen -t rsa 生成密钥对

    当然还可以用参数指定私钥文件

    $ ssh -i ~/.id_rsa someuser@<ip.here>
    

    2. 设置验证方式

    可以在/etc/ssh/ssh_config~/.ssh/config或使用命令行参数-o

    PasswordAuthentication

    是否使用密码验证,yes/no

    $ ssh user@host -o PasswordAuthentication=yes
    

    3. Host设置

    刚开始使用ssh时,我连接主机都是采用类似下列格式

    $ ssh user@host -p 2222 #可能还有一堆的参数
    

    有没有觉得每次连接都要重复输入一串参数很繁琐呢?

    Host设置可以对连接设置别名,方便访问

    例如在~/.ssh/config 的最后有如下设置

    Host pi
        HostName 192.168.1.106
        User pi
        Port 2222
        IdentityFile ~/somedir/id_rsa
        PreferredAuthentications publickey
    

    那么当你需要连接到pi的时候只需要输入ssh pi 即可,不用记住这些配置信息。

    note: 设置Host之后用scp传输文件也变得方便,scp ./a.file pi:~/a.file

    相关文章

      网友评论

        本文标题:ssh-简单配置

        本文链接:https://www.haomeiwen.com/subject/ppyesftx.html