In the past two days, we need to use ssh to configure Linux servers in the company, and record the configuration process. Environment OS: Ubuntu12.041. install the server: sudoapt-getinstallsshopenssh-serverssh-import-id client: www.linuxidc.com @ linux :~ $ Sudoapt-getinstallopenssh-client2. Configure 2.
In the past two days, we need to use ssh to configure Linux servers in the company, and record the configuration process.
Environment
OS: Ubuntu 12.04
1. Install
Server:
Sudo apt-get install ssh openssh-server ssh-import-id
Client:
Www.linuxidc.com @ linux :~ $ Sudo apt-get install openssh-client
2. Configuration
2. 1. Modify the ssh port
The default ssh port is 22, which is too eye-catching. We recommend that you change it to another port, such as 3301.
Modify the/etc/ssh/sshd_config file
Replace
# Port 22
Change
Port 3301
Restart the ssh service after modification.
Www.linuxidc.com @ linux :~ $ Sudo/etc/init. d/ssh restart
3. log on
There are two methods to log on to the ssh server: password and key file.
3. 1. log on with a password
Www.linuxidc.com @ linux :~ $ Ssh testuser@192.168.56.101-p3301
Testuser@192.168.56.101's password:
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
System information as of Thu May 17 00:31:21 CST 2012
System load: 0.0 Processes: 73
Usage of/: 9.1% of 19.18 GB Users logged in: 1
Memory usage: 14% IP address for eth0: 192.168.56.101
Swap usage: 0%
Graph this data and manage this system at https://landscape.canonical.com/
Last login: Thu May 17 00:22:04 2012 from 192.168.56.1
If you see such information, the logon is successful.
This method is relatively simple, but the security is slightly poor.
3. 2. Public Key-Private Key Login
First, run
Www.linuxidc.com @ linux :~ $ Ssh-keygen-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jim/. ssh/id_rsa ):
Enter passphrase (empty for no passphrase): // Enter your passphrase password. This password will be used later.
Enter same passphrase again:
Your identification has been saved in/home/jim/. ssh/id_rsa.
Your public key has been saved in/home/jim/. ssh/id_rsa.pub.
The key fingerprint is:
B9: 8f: 4b: e4: a8: 39: 9d: 57: df: cb: 4e: 6c: 79: 76: db: 7f www.linuxidc.com @ linux
The key's randomart image is:
+ -- [RSA 2048] ---- +
|
|
|
|. |
| S |
| +... |
|... +... = + |
|. Oo.. o. +. oE |
| O. o... + o = |
+ ----------------- +
Www.linuxidc.com @ linux :~ $
Upload Public Key
Www.linuxidc.com @ linux :~ $ Ssh-copy-id "-p 3301-I ~ /. Ssh/id_rsa.pub testuser@192.168.56.101"
Warning: Identity file ~ /. Ssh/id_rsa.pub not accessible: No such file or directory.
Testuser@192.168.56.101's password:
Now try logging into the machine, with "ssh '-p 3301-I ~ /. Ssh/id_rsa.pub testuser@192.168.56.101 '", and check in:
~ /. Ssh/authorized_keys
To make sure we haven't added extra keys that you weren't expecting.
Www.linuxidc.com @ linux :~ $
The program prompts you to connect and try again. Just do it.
Www.linuxidc.com @ linux :~ $ Ssh-p 3301-I ~ /. Ssh/id_rsa.pub testuser@192.168.56.101
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
System information as of Thu May 17 00:48:35 CST 2012
System load: 0.01 Processes: 73
Usage of/: 9.1% of 19.18 GB Users logged in: 1
Memory usage: 15% IP address for eth0: 192.168.56.101
Swap usage: 0%
Graph this data and manage this system at https://landscape.canonical.com/
Last login: Thu May 17 00:45:27 2012 from 192.168.56.1
Testuser @ demo-server :~ $ Exit
There is a prompt in the middle, asking you to enter the previous passphrase password, you only need to enter it once, and then no longer need to enter it.
OK. Log On again and try again. This time, the public key is not imported.
Www.linuxidc.com @ linux :~ $ Ssh testuser@192.168.56.101-p 2201
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
* Documentation: https://help.ubuntu.com/
System information as of Thu May 17 00:49:08 CST 2012
System load: 0.06 Processes: 73
Usage of/: 9.1% of 19.18 GB Users logged in: 1
Memory usage: 15% IP address for eth0: 192.168.56.101
Swap usage: 0%
Graph this data and manage this system at https://landscape.canonical.com/
Last login: Thu May 17 00:48:52 2012 from 192.168.56.1
Administrator @ demo-server :~ $ Exit
Logout
Connection to 192.168.56.101 closed.
Www.linuxidc.com @ linux :~ $
Log on to the ssh server without entering the password.
Check that there are several files in the local. ssh directory.
Www.linuxidc.com @ linux :~ $ Ls. ssh-l
Total usage 36
-Rw ------- 1 jim 1766 May 17 00:46 id_rsa // This is the private key
-Rw-r -- 1 jim 396 May 17 00:46 id_rsa.pub // This is the Public Key
-Rw-r -- 1 jim 222 May 17 00:45 known_hosts // This is the public key information of all the ssh servers you have accessed. If this file does not exist, you will be prompted next time you log on.