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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM