您的位置:首页 > 编程语言 > PHP开发

在ubuntu中配置SSH(问题的解决)sftp的使用

2016-04-13 09:04 681 查看
配置hadoop时必须的一步就是保证ssh是联通的。 如果你的 ubuntu 中没有安装 ssh ,具体的安装步骤是这样的 :

 

1 到 https://launchpad.net/ubuntu/lucid/i386/openssh-client/1:5.3p1-3ubuntu3 

下载文件 ” openssh-client_5.3p1-3ubuntu3_i386.deb   ” 并在终端中安装。

sudo dpkg -i openssh-client_5.3p1-3ubuntu3_i386.deb   

2 到 https://launchpad.net/ubuntu/lucid/i386/openssh-server/1:5.3p1-3ubuntu3 

下载文件 ” openssh-server_5.3p1-3ubuntu3_i386.deb” 并在终端中安装。 

sudo dpkg -i openssh-server_5.3p1-3ubuntu3_i386.deb

 

3 到 https://launchpad.net/ubuntu/lucid/i386/ssh/1:5.3p1-3ubuntu3 

下载文件 ”ssh_5.3p1-3ubuntu3_all.deb” ” 并在终端中安装。 

sudo dpkg -i ssh_5.3p1-3ubuntu3_all.deb

 

 

 

复杂地说

 

我尝试了很多错误路线,最后终于找到了正确方式,现在把我走过的弯路也记录一下 :

 

在 hadoop 配置的过程中,前提条件之一是保证 ssh 是配置好的。使用这个命令, hadoopr@ubuntu:~$ ssh localhost 

得到了如下的信息

ssh: connect to host localhost port 22: Connection refused 

这意味着 ssh 不能联通成功。不联通的理由可以有很多,如防火墙设置问题,如 ssh 没有被安装。我的连防火墙都没有,所以应该是 ssh 安装的问题。

 

于是现在安装 ssh

网上搜到信息说,安装 ssh 应该使用这样的命令,如下

hadoopr@ubuntu:~$ sudo apt-get install openssh-server 

Reading package lists... Done 

Building dependency tree         

Reading state information... Done 

Some packages could not be installed. This may mean that you have 

requested an impossible situation or if you are using the unstable 

distribution that some required packages have not yet been created 

or been moved out of Incoming. 

The following information may help to resolve the situation: 

The following packages have unmet dependencies: 

  openssh-server: Depends: openssh-client (= 1:5.3p1-3ubuntu3) but 1:5.3p1-3ubuntu5 is to be installed 

E: Broken packages 

仔细阅读上面这对错误信息。发现  openssh-server 是需要在 openssh-client 安装后才能安装的。并且看来指定得openssh-client 的版本是 1:5.3p1-3ubuntu3.

 

于是,自然而然地想到要去安装 1:5.3p1-3ubuntu ,

hadoopr@ubuntu:~$ sudo apt-get install 1:5.3p1-3ubuntu3 

Reading package lists... Done 

Building dependency tree         

Reading state information... Done 

E: Couldn't find package 1:5.3p1-3ubuntu3 

最后一句话说明, sudo apt-get install 此路不通啊,于是换一条路。

 

在百度上搜索 5.3p1-3ubuntu3 得到的有用信息非常少,在 google 上搜索 5.3p1-3ubuntu3 发现了一个地址,https://launchpad.net/ubuntu/lucid/i386/ssh/1:5.3p1-3ubuntu3 ,网页方式直接下载了一个对应文件。 ”ssh_5.3p1-3ubuntu3_all.deb” 

本来我觉得只要安装它应该就可以了,于是

hadoopr@ubuntu:~/Downloads$ sudo dpkg -i ssh_5.3p1-3ubuntu3_all.deb  

但是,让人吐血的一步来了,

Selecting previously deselected package ssh. 

(Reading database ... 127434 files and directories currently installed.) 

Unpacking ssh (from ssh_5.3p1-3ubuntu3_all.deb) ... 

dpkg: dependency problems prevent configuration of ssh: 

 ssh depends on openssh-server; however: 

  Package openssh-server is not installed. 

dpkg: error processing ssh (--install): 

 dependency problems - leaving unconfigured 

Errors were encountered while processing: ssh 

注意一下,它说  ssh depends on openssh-server; however:Package openssh-server is not installed. 

也就是说,刚才安装 openssh-server 时它说需要 openssh-client ,现在安装 openssh-client 了,它又说需要先装openssh-server 。这样岂不是死循环了。

 

于是又去了 https://launchpad.net/ubuntu/lucid/i386/ssh/1:5.3p1-3ubuntu3 这个网址,仔细阅读。

发现,其实网上说得很明白,我刚才自己没有看清楚。要成功地装 ssh 需要三步。

第一步,在 https://launchpad.net/ubuntu/lucid/i386/openssh-client/1:5.3p1-3ubuntu3

             或(https://launchpad.net/ubuntu/lucid/amd64/openssh-client/1:5.3p1-3ubuntu3 )下载文件并安装。

第二步,在 https://launchpad.net/ubuntu/lucid/i386/openssh-server/1:5.3p1-3ubuntu3

             或(https://launchpad.net/ubuntu/lucid/amd64/openssh-server/1:5.3p1-3ubuntu3 )下载文件并安装。

第三步,在 https://launchpad.net/ubuntu/lucid/i386/ssh/1:5.3p1-3ubuntu3

             或( https://launchpad.net/ubuntu/lucid/amd64/ssh/1:5.3p1-3ubuntu3 )下载文件并安装。 

剩下的事儿就好办了。下载安装即可。

下载并安装第一个文件

hadoopr@ubuntu:~/Downloads$ sudo dpkg -i openssh-client_5.3p1-3ubuntu3_i386.deb   

dpkg: warning: downgrading openssh-client from 1:5.3p1-3ubuntu5 to 1:5.3p1-3ubuntu3. 

(Reading database ... 127436 files and directories currently installed.) 

Preparing to replace openssh-client 1:5.3p1-3ubuntu5 (using openssh-client_5.3p1-3ubuntu3_i386.deb) ... 

Unpacking replacement openssh-client ... 

Setting up openssh-client (1:5.3p1-3ubuntu3) ... 

Processing triggers for man-db ... 

下载并安装第二个文件

hadoopr@ubuntu:~/Downloads$ sudo dpkg -i openssh-server_5.3p1-3ubuntu3_i386.deb   

Selecting previously deselected package openssh-server. 

(Reading database ... 127436 files and directories currently installed.) 

Unpacking openssh-server (from openssh-server_5.3p1-3ubuntu3_i386.deb) ... 

Setting up openssh-server (1:5.3p1-3ubuntu3) ... 

Creating SSH2 RSA key; this may take some time ... 

Creating SSH2 DSA key; this may take some time ... 

 * Stopping OpenBSD Secure Shell server sshd                                                              [ OK ]   

ssh start/running, process 3822 

Processing triggers for ureadahead ... 

ureadahead will be r
4000
eprofiled on next reboot 

Processing triggers for ufw ... 

Processing triggers for man-db ... 

下载并安装第三个文件

hadoopr@ubuntu:~/Downloads$ sudo dpkg -i ssh_5.3p1-3ubuntu3_all.deb   

(Reading database ... 127454 files and directories currently installed.) 

Preparing to replace ssh 1:5.3p1-3ubuntu3 (using ssh_5.3p1-3ubuntu3_all.deb) ... 

Unpacking replacement ssh ... 

Setting up ssh (1:5.3p1-3ubuntu3) ... 

最后试了试,成功。

hadoopr@ubuntu:~/Downloads$ ssh localhost 

The authenticity of host 'localhost (::1)' can't be established. 

RSA key fingerprint is 0f:f5:b7:32:06:a3:3c:4c:de:fb:04:aa:8a:f2:2d:ee. 

Are you sure you want to continue connecting (yes/no)? yes 

Warning: Permanently added 'localhost' (RSA) to the list of known hosts.

 

然后确认sshserver是否启动了:

ps -e |grep ssh

如果看到sshd那说明ssh-server已经启动了。

如果没有则可以这样启动:sudo /etc/init.d/ssh start

ssh-server配置文件位于/ etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。

然后重启SSH服务:

sudo /etc/init.d/ssh stop

sudo /etc/init.d/ssh start

然后使用以下方式登陆SSH:

ssh username@192.168.1.112 username为192.168.1.112 机器上的用户,需要输入密码。

将 文件/文件夹 从远程 Ubuntu 机拷至本地(scp)

scp -r username@192.168.0.1:/home/username/remotefile.txt .

将 文件/文件夹 从本地拷至远程 Ubuntu 机(scp)

scp -r localfile.txt username@192.168.0.1:/home/username/

将 文件/文件夹 从远程 Ubuntu 机拷至本地(rsync)

rsync -v -u -a --delete --rsh=ssh --stats username@192.168.0.1:/home/username/remotefile.txt .

将 文件/文件夹 从本地拷至远程 Ubuntu 机(rsync)

rsync -v -u -a --delete --rsh=ssh --stats localfile.txt username@192.168.0.1:/home/username/

断开连接:exit

 

Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件

在Windows下可以使用SecureCRT远程登录Ubuntu,并使用rz、sz实现上传、下载文件。

如果在Ubuntu下使用ssh命令远程登录另一台Ubuntu,如何实现上传、下载呢?答案是:sftp。

sftp是secure file transfer program的缩写,是隶属于SSH的一个应用,在安装好SSH之后就已包括此命令,其使用方式类似于ftp而更加安全。

Ubuntu 10.04中语法如下:

 sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config] [-o ssh_option] [-P sftp_server_path] [-R num_requests]

      [-S program] [-s subsystem | sftp_server] host

 sftp [user@]host[:file ...]

 sftp [user@]host[:dir[/]]

 sftp -b batchfile [user@]host

Ubuntu 10.10中语法如下:

 sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher] [-D sftp_server_path] [-F ssh_config] [-i identity_file]

      [-o ssh_option] [-P port] [-R num_requests] [-S program] [-s subsystem | sftp_server] host

 sftp [user@]host[:file ...]

 sftp [user@]host[:dir[/]]

 sftp -b batchfile [user@]host

以下是其用法举例:
登录(登录成功后,命令行提示符将变为“sftp>”) 

sftp chen@100.130.100.99  #以chen用户身份远程登录100.130.100.99主机,默认端口号为22

sftp -o Port=222 chen@100.130.100.99  #指定SSH端口号来远程登录主机

sftp -o Port=222 chen@100.130.100.99:/home/chen/version/  #指定SSH端口号来远程登录主机,并切换到/home/chen/version/目录

sftp -P 222 chen@100.130.100.99  #指定SSH端口号来远程登录主机(此方式只能在Ubuntu 10.10中使用)

注:登录到远程主机后才可执行如下命令。

交互命令: 

上传

put new.txt /home/chen/#将本地当前目录下的new.txt上传到远程主机的/home/chen/目录

下载

get /home/chen/new.txt #将远程主机的/home/chen/new.txt文件下载到本地当前目录

get /home/chen/new.txt /home/zhao/#将远程主机的/home/chen/new.txt文件下载到本地/home/zhao/目录

远程命令: 

切换目录

cd pro #切换到pro目录

更改权限

chmod 755 my-shell.sh #将my-shell.sh文件的权限改为755

查看磁盘使用情况

df -h #查看磁盘使用情况

列出目录内容

ls -l #以详细列表的方式显示文件和目录

新建目录

mkdir newdir #在当前目录下新建newdir目录

显示当前目录的完整路径

pwd#显示当前目录的完整路径

重命名

rename my-shell.sh new-shell.sh #将my-shell.sh文件重命名为new-shell.sh

删除文件

rm new-shell.sh#删除new-shell.sh文件

删除空目录

rmdir pro#删除空目录pro

创建链接

ln abc.txt abc.ln   #为abc.txt建立链接文件abc.ln

本地命令:(本地命令一般会在命令前添加l以代表local) 

切换目录

lcd pro #将本地目录切换到pro目录

列出目录内容

lls -l#以详细列表的方式显示本地的文件和目录

新建目录

lmkdir newdir #在本地当前目录下新建newdir目录

显示当前目录的完整路径

lpwd#显示本地当前目录的完整路径

执行任何本地命令

!date #使用本地shell来执行命令,!之后可以是任何本机能够识别的命令

!#切换到本地shell,以便执行其他命令,如果想退出本地shell,可以执行exit以返回远程环境

退出: 

bye #退出sftp

exit #同上

quit #同上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: