1. Generate SSH keys
ssh-keygen -t rsa -C "[email protected]"
rsa and rsa.pub will be generated and saved in the ~/.ssh/ directory by default. rsa is the private key and rsa.pub is the public key.
2. Server side settings
Edit etc/ssh/sshd_config
Uncomment the following lines to enable public key authentication login.
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys AuthorizedKeysCommand none AuthorizedKeysCommandRunAs nobody
If you need to prohibit the use of password login, you can set
PasswordAuthentication no
restart ssh
service sshd restart
3. Create a config file in the .ssh directory (set the server port, when connecting to the git server)
host 192.168.1.101 user git PreferredAuthentications publickey identityfile C:/Users/Administrator/.ssh/admin hostname 192.168.1.101 port 22
Specifies to use the admin private key for authentication when connecting to the server at 192.168.1.101.
4. Upload the public key to the server
Execute the following commands in git bash:
scp ~/.ssh/admin.pub [email protected]: ~/
5. Write the public key to the server verification file
Log in to the server as root and switch to the git user.
Create a .ssh/authorized_keys file in the git user directory. Both directories and files must be created.
Then execute the add public key command
cat id_rsa.pub >> ~/.ssh/authorized_keys
6. Set file and directory permissions
SSH has very strict requirements on the permissions and ownership of public keys and private keys, which are summarized as follows:
1. Set the permissions of the .ssh directory
$ chmod 700 -R .ssh
2. Set authorized_keys permissions
$ chmod 600 authorized_keys
To ensure that both .ssh and authorized_keys have write permissions only for the user. Otherwise validation is invalid.
7. ssh public key authentication login
Enter the ssh connection server command
ssh [email protected]
If the following information is displayed, the key login is successful.
$ ssh [email protected]
Last login: Sun Jul 6 22:04:32 2014 from 192.168.1.100
[git@localhost ~]$