Linux tips: SSH password-free login and configure environment variables

“This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. Exempt login

Working principle of non-secret login

Note: The secret key pair is in the user’s ~/. SSH directory, which means that the user of this machine can log in secret-free. If the same machine switches to a user that is not configured with secret-free login, the following configuration is required again.

Generate public and private key pairs

ssh-keygen -t rsa

You can then press enter (three times) to generate two files id_rsa (private key), id_rsa.pub (public key)

[root@k8s1 .ssh]# cd ~/.ssh/
[root@k8s1 .ssh]# pwd
/root/.ssh
[root@k8s1 .ssh]# ll
total 4
-rw-r--r--. 1 root root 869 Nov 26 02:14 known_hosts
[root@k8s1 .ssh]# 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:
SHA256:Wj90GVrbS+O/WOdEDUJj8uJD1mcsO+RmaddyZxBoRwI root@k8s1
The key's randomart image is: +---[RSA 2048]----+ | E.=oo | | *o+.. | | +.O.* | | + * % +.| | S = % * B| | o o * = B.| | . o o.o| | . o+.| | . .+| +----[SHA256]-----+ [root@k8s1 .ssh]# ll total 12 -rw-------. 1 root root 1679 Nov 26 03:17 id_rsa -rw-r--r--. 1 root root 391 Nov 26 03:17 id_rsa.pub -rw-r--r--. 1 root root 869 Nov 26 02:14 known_hostsCopy the code

Copy the public key to the target machine where you want to avoid secret login

Each machine runs these three commands

ssh-copy-id k8s1 
ssh-copy-id k8s2
ssh-copy-id k8s3
Copy the code

The ssh-copy-id command is used to copy the public key to the ~/. SSH /authorized_keys file of the server to be logged in to without secret.

Manual copying also works.

test

SSH Machine host name

Matters needing attention

  1. The password – free configuration corresponds to the account. You need to reconfigure different accounts.
  2. The related operation requires the machine network to communicate.

2. Configure environment variables

Create environment variable files

A separate environment variable file should be created for each service for easy management

vim /etc/profile.d/xxx.sh

Export JAVA_HOME = / app/JDK/jdk1.8.0 _301 export PATH = $PATH: $JAVA_HOME/binCopy the code

Example Refresh profile information

source /etc/profile

View the global environment variable paths

Scripts under these paths can also be run globally

echo $PATH