zoukankan      html  css  js  c++  java
  • Linux命令-设置免密码登录

    设置免密码登陆:
    [root@Redis01 test]# cd /install/
    [root@Redis01 install]# rm -rf test/
    [root@Redis01 install]# ls
    initNetwork.sh
    [root@Redis01 install]# ssh Redis02
    root@redis02's password:
    Last login: Thu Nov 30 23:28:06 2017 from 192.168.238.1
    [root@Redis02 ~]# ls /software/
    jdk1.7.0_79 mysql-5.5.32 redis-2.8.9
    [root@Redis02 ~]# exit
    logout
    Connection to Redis02 closed.
    [root@Redis01 install]# ssh Redis02 "ls /software"
    root@redis02's password:
    jdk1.7.0_79
    mysql-5.5.32
    redis-2.8.9

    在Redis01设置加密协议:(此处按了三次Enter)
    [root@Redis01 install]# ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    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:
    4f:a4:b0:91:3b:75:18:22:9b:f3:f9:11:e4:c6:8d:fb root@Redis01
    The key's randomart image is:
    +--[ RSA 2048]----+
    | . . o |
    | + * = |
    | + + O + |
    | o O * |
    | * S . |
    | o = |
    | . E |
    | |
    | |
    +-----------------+

    将加密协议复制到Redis02:
    [root@Redis01 install]# ssh-copy-id Redis02
    root@redis02's password:
    Now try logging into the machine, with "ssh 'Redis02'", and check in:

    .ssh/authorized_keys

    to make sure we haven't added extra keys that you weren't expecting.

    此时在Redis01中登陆Redis02将不需要密码:
    [root@Redis01 install]# ssh Redis02
    Last login: Thu Nov 30 23:30:44 2017 from redis01
    [root@Redis02 ~]# exit
    logout
    Connection to Redis02 closed.

    Redis01登陆自己时也需要密码,所以也需要将加密协议复制给自己:
    [root@Redis01 install]# ssh Redis01
    The authenticity of host 'redis01 (192.168.238.128)' can't be established.
    RSA key fingerprint is 82:72:60:05:6d:dc:3e:bf:f7:aa:2d:f5:08:c1:59:3a.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'redis01,192.168.238.128' (RSA) to the list of known hosts.
    root@redis01's password:
    Last login: Thu Nov 30 15:47:03 2017 from 192.168.238.1
    [root@Redis01 ~]# exit
    logout
    Connection to Redis01 closed.
    [root@Redis01 install]# ssh-copy-id Redis01
    root@redis01's password:
    Now try logging into the machine, with "ssh 'Redis01'", and check in:

    .ssh/authorized_keys

    to make sure we haven't added extra keys that you weren't expecting.

    此时在Redis01中登陆Redis01将不需要密码:
    [root@Redis01 install]# ssh Redis01
    Last login: Thu Nov 30 15:58:51 2017 from redis01
    [root@Redis01 ~]# exit
    logout
    Connection to Redis01 closed.

    此时在用ssh命令时可以直接输入对应的在主机中需要执行的命令:
    [root@Redis01 install]# ssh Redis02 "ls /software"
    jdk1.7.0_79
    mysql-5.5.32
    redis-2.8.9

    添加用户:
    md表示设置指定用户家目录:
    [root@Redis01 install]# useradd -md /home/ligang/ ligang
    不可修改的查看刚添加的用户目录:
    [root@Redis01 install]# less /etc/passwd
    查看sh所在位置:
    [root@Redis01 install]# type sh
    sh is /bin/sh
    [root@Redis01 install]# ls -ld /bin/s
    sed setfont setserial sh sleep sort stty su sync
    [root@Redis01 install]# ls -ld /bin/sh
    lrwxrwxrwx. 1 root root 4 7月 13 21:25 /bin/sh -> bash
    [root@Redis01 install]# type bash
    bash is /bin/bash
    [root@Redis01 install]# grep ligang /etc/passwd
    ligang:x:502:502::/home/ligang/:/bin/bash
    设置用户ligang的密码,必须设置密码才能登陆:
    [root@Redis01 install]# passwd ligang
    更改用户 ligang 的密码 。
    新的 密码:
    无效的密码: 它基于字典单词
    无效的密码: 过于简单
    重新输入新的 密码:
    passwd: 所有的身份验证令牌已经成功更新。

    不可修改的查看刚添加的用户的密码:
    [root@Redis01 install]# less /etc/shadow

    sh默认有权限;./没有权限;
    [root@Redis01 test]# ./a.sh
    -bash: ./a.sh: 权限不够
    [root@Redis01 test]# sh a.sh
    Hello World

    [root@Redis01 ~]# ls -a
    . anaconda-ks.cfg .bash_logout .bashrc install.log .mysql_history .ssh .viminfo
    .. .bash_history .bash_profile .cshrc install.log.syslog .rediscli_history .tcshrc

    .当前目录;..上一级目录;.开头的文件表示隐藏文件;

    [root@Redis01 ~]# ll -a
    总用量 96
    dr-xr-x---. 3 root root 4096 11月 29 22:20 .
    dr-xr-xr-x. 24 root root 4096 12月 1 10:35 ..
    -rw-------. 1 root root 1311 7月 13 21:33 anaconda-ks.cfg
    -rw-------. 1 root root 9570 12月 1 10:34 .bash_history
    -rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout
    -rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile
    -rw-r--r--. 1 root root 176 9月 23 2004 .bashrc
    -rw-r--r--. 1 root root 100 9月 23 2004 .cshrc
    -rw-r--r--. 1 root root 22618 7月 13 21:33 install.log
    -rw-r--r--. 1 root root 5890 7月 13 21:32 install.log.syslog
    -rw-------. 1 root root 452 11月 28 23:43 .mysql_history
    -rw-r--r--. 1 root root 197 11月 30 00:04 .rediscli_history
    drwx------. 2 root root 4096 11月 30 15:59 .ssh
    -rw-r--r--. 1 root root 129 12月 4 2004 .tcshrc
    -rw-------. 1 root root 686 11月 28 00:35 .viminfo


    .ssh中的authorized_keys和known_hosts负责保存加密协议;
    删除这两个文件后,再次通过ssh协议连接时需要输入密码;
    [root@Redis01 ~]# cd .ssh/
    [root@Redis01 .ssh]# ls
    authorized_keys id_rsa id_rsa.pub known_hosts

    清空的意思,相当于空覆盖原有内容:
    [root@Redis01 .ssh]# >known_hosts
    [root@Redis01 .ssh]# >authorized_keys


    [root@Redis01 .ssh]# ls -ld authorized_keys
    -rw-------. 1 root root 394 11月 30 15:59 authorized_keys


    [root@Redis01 ~]# ls -ld .ssh
    drwx------. 2 root root 4096 11月 30 15:59 .ssh

  • 相关阅读:
    mysql--------常用命令
    PHP--------微商城实现微信授权登录
    mysql--------命令来操作表
    PHP--------解决网址URL编码问题
    php-----utf8和gbk相互转换
    javascript遍历json对象数据的方法
    PHP-----------HTTP请求的第三方接口
    PHP------数组和对象相互转化,stdClass Object转array
    include与require的区别
    PHP中exit()与die()的区别
  • 原文地址:https://www.cnblogs.com/mmzs/p/8183962.html
Copyright © 2011-2022 走看看