SSH Server

来源:互联网 发布:手机应用分身软件 编辑:程序博客网 时间:2024/05/21 14:46

远程监控方案  SSH    HADOOP 集群模式也要用到

 

 

安装 ssh server 在Windows 中使用 putty 访问  ubuntu server

 

启用 root 帐户

 

sudo passwd root

输入当前用户密码

 

输入 root  用户密码  ,重复输入一次。

 

切换 root 用户

su root

 

apt-get install openssh-server

 

使用 ifconfig

查看 ip 地址 ,使用 putty ,ssh 方式远程登录服务器操作。

 

四台服务器相互不用密码登录的设置。

 

ServerU1           192.168.1.110

 

ServerU2           192.168.1.111

 

ServerU3           192.168.1.112

 

ServerU4           192.168.1.113

 

使用命令

 

ssh localhost

 

输入密码: 登录成功

 

exit  退出。

 

注意防火墙和22端口。

 

不使用密码,使4台机器可以相互登录。

 

root@ServerU2:~# ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is 4f:0b:29:5e:ac:f0:a7:21:02:ff:59:52:24:71:80:01.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:

 

步骤:

 

1、生成密钥对

 

root@ServerU1:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

 

一路回车即可,

 

root@ServerU1:~# cd /root
root@ServerU1:~# ls
root@ServerU1:~# ls -la
总计 36
drwx------  6 root root 4096 2010-06-30 09:36 .
drwxr-xr-x 22 root root 4096 2010-06-29 22:31 ..
drwx------  2 root root 4096 2010-06-29 22:42 .aptitude
-rw-------  1 root root   81 2010-06-30 09:37 .bash_history
-rw-r--r--  1 root root 3106 2010-04-23 17:45 .bashrc
drwxr-xr-x  2 root root 4096 2010-06-29 23:05 .cache
drwxr-xr-x  2 root root 4096 2010-06-29 22:42 .debtags
-rw-r--r--  1 root root  140 2010-04-23 17:45 .profile
drwx------  2 root root 4096 2010-06-30 09:41 .ssh
root@ServerU1:~#

 

注意隐藏目录  .ssh

 

root@ServerU1:~# cd .ssh
root@ServerU1:~/.ssh# ls
id_rsa ----私钥     id_rsa.pub   ------  公钥      known_hosts
root@ServerU1:~/.ssh#

复制公钥到服务器,

命令:

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.110

The authenticity of host '192.168.1.110 (192.168.1.110)' can't be established.
RSA key fingerprint is 39:c1:4e:98:60:69:b2:a8:38:c5:8e:93:3e:1b:f9:15.
Are you sure you want to continue connecting (yes/no)? yes

 

Warning: Permanently added '192.168.1.110' (RSA) to the list of known hosts.
root@192.168.1.110's password:
Now try logging into the machine, with "ssh 'root@192.168.1.110'", and check in:

  .ssh/authorized_keys

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

 

服务器为自动生成   ~/.ssh/authorized_keys

 

root@ServerU1:~/.ssh# ls
authorized_keys   ---公钥文件   id_rsa  id_rsa.pub  known_hosts
root@ServerU1:~/.ssh#

 

命令:

cat authorized_keys

 

文件内容如下:


ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxodEx2c0xrpt078sf3GA

YvmzoVIH9yxsGYQ1wqJBm5cZu02463n/7OetQ7G4G9FJYX4/xTul3m

gj9SR95iRBAO4o7Npftfth5JrLtWvPVLE8Lz2v5wzYxonLyAkosDgf8K91

Z79A3zB6ZqVtuzdIUSH8yTuzmdWYRVs+NrPyr1NZgr81hUuYw6hY1lcE

i0IZsPwaSy2nhGduznLx4GJ+kdzpKducpyuQRWeN1qxxl7IE70Ug8u8pq

AM1cNGuD2mcdUEQvAS2gia5YSx6M6UhGV1jcyvWnkGJJH124Zvep403kKU1

rBO5PGw8c0aPxbLGzBbdfeb10tAc24xNUhsFGQ== root@ServerU1   (可以看到是  SwerverU1)

 

 

再次使用 ssh localhost  不需要使用密码就可以进入,

 

按照这个方法。

 

分别在  ServerU2、ServerU3 、ServerU4、上生成密钥对,并复制公钥至 ServerU1,  这样 在 ServerU1上访问别外3台机器,就不需要密码了。

 

然后再把 ServerU1 上的  ~/.ssh/authorized_keys 复制到 另外3台机器中的 ~/.ssh/ 文件夹中,即可实现,互访且不需要密码。

 

在 ServerU2 中执行:

 

root@ServerU2:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):    //生成密钥对

root@ServerU2:~# ssh-copy-id -i /root/.ssh/id_rsa.pub    root@192.168.1.110     //复制文件到 ServerU1

 

The authenticity of host '192.168.1.110 (192.168.1.110)' can't be established.
RSA key fingerprint is 39:c1:4e:98:60:69:b2:a8:38:c5:8e:93:3e:1b:f9:15.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.110' (RSA) to the list of known hosts.
root@192.168.1.110's password:


root@ServerU1:~/.ssh# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxodEx2c0x

rpt078sf3GAYvmzoVIH9yxsGYQ1wqJBm5cZu02463n/7

OetQ7G4G9FJYX4/xTul3mgj9SR95iRBAO4o7Npftfth5JrLtWvP

VLE8Lz2v5wzYxonLyAkosDgf8K91Z79A3zB6ZqVtuzdIUSH8yT

uzmdWYRVs+NrPyr1NZgr81hUuYw6hY1lcEi0IZsPwaSy2nhGdu

znLx4GJ+kdzpKducpyuQRWeN1qxxl7IE70Ug8u8pqAM1cNGuD2

mcdUEQvAS2gia5YSx6M6UhGV1jcyvWnkGJJH124Zvep403kKU1

rBO5PGw8c0aPxbLGzBbdfeb10tAc24xNUhsFGQ== root@ServerU1
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAulzOY7UoSoKew6darRsY0z3IK

gsmH13PIfWGflTYOli46DvFEtZtODz5mCUoWi1z7RA/4iq9PoZnL45ou

msEENq1DZsLBKDjw7WtDzkWbpF0TfkyQGw9a9SQ99DK5PA7Idaa5yrIflD

cKrw6z9I9EcMfD/GMRt9Wrkb2zE8VJuNtfuJhvOCAE8hEbv3znrJdRsmRL5BZxZZ

Bk3AFJ4QqJdAmvg57xm3Vn9VuM7/EpV+h4u/CeAnYZeGmCRPKv8eL9oAwk6l9jAt

NyrtZzSoigd/eQNLSCHHXOFTqgE5fXcQgCtJtuiaVNn9SP0HWKQjVGsW03C5ObvvBiZT5k6vw6Q== root@ServerU2

//文件中多出了 ServerU2 的信息。

 

root@ServerU1:~/.ssh#

 

同上,添加 ServerU3/ServerU4信息到 ServerU1中。

 

cat authorized_keys    //可以看到文件每段的结束处都有  

root@ServerU1

root@ServerU2

root@ServerU3

root@ServerU4

 

 

注意:这时是 。 U2、U3、U4  可以无密码访问U1, 

 

使用 SSH的远程复制命令;

scp authorized_keys root@192.168.1.111:/root/.ssh      //复制文件到相应目录。
root@192.168.1.111's password:
authorized_keys                              100% 1975     1.9KB/s   00:00
root@ServerU1:~/.ssh#

 

root@ServerU2:~/.ssh# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
root@ServerU2:~/.ssh#

 

同理完成  U3、U4

 

这样就实现了  4台机器无密码互访。 

 

若要使用 主机名互机  修改:

vim /etc/hosts

 

127.0.0.1       localhost
192.168.1.110   ServerU1

 

#加入主机名解析列表

 

192.168.1.111   ServerU2

192.168.1.112   ServerU3

192.168.1.113   ServerU4

 

 

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
~

修改完成后保存

 

在 U1 中可以 

 ping ServerU1 

 ping ServerU2 

 ping ServerU3 

 ping ServerU4

 

都可以ping 通,

 

 

直接远程复制文件到各主机。

 

scp  /etc/hosts  root@192.168.1.111:/etc/hosts

 

scp  /etc/hosts  root@192.168.1.112:/etc/hosts

 

scp  /etc/hosts  root@192.168.1.113:/etc/hosts

 

 

这样以来,就可以使用  主机名登录了。

 

ssh  serveru3

 

root@ServerU1:~/.ssh# ssh serveru3
The authenticity of host 'serveru3 (192.168.1.112)' can't be established.
RSA key fingerprint is a1:c0:21:05:cf:f7:eb:4a:12:3b:94:11:62:31:55:aa.
Are you sure you want to continue connecting (yes/no)?

第一次联接时需要确认。之后就不用了。