ssh简单操作命令

1. 登录到远端计算机
在客户端登录到远端计算机使用ssh命令,常用的格式为:
ssh user_name@远端计算机IP
很多时候会忘记加上user_name,那么默认的就是使用本地账户登录到远端计算机。如果你运气足够好的话,远端和本地具有相同的账户,并且你知道远端该用户的密码,那么你能够登录成功,否则,还是老老实实的加上远端计算机的用户名吧。输入该命令后,会提示你数据一个密码,该密码就是远端计算机帐户的密码,输入正确后,登录成功。

2. 文件传输
ssh命令只是登录到远端计算机上,这样你可以操作远端计算机。但是有时候我们并不需要登录到远端计算机,只是在远端计算机和本地传输文件,有两个命令sftp和sch。

2.1 sftp
上传文件到远端计算机命令如下:
put 本地文件 远端计算机目录
下载文件到本地计算机命令如下:
get 远端文件 本地目录

2.2 scp
scp命令格式如下:
scp 文件 账户@远端计算机IP:目录名
scp 账户 @远端计算机IP:文件 本地目录

同cp命令一样,复制目录时可以加上-r选项。


// 2015.08.31 add
1. ssh配置无密码访问
首先使用ssh-keygen -t rsa生成密钥,然后将id_rsa.pub中的内容复制到远端计算机的.ssh/authorized_keys文件中,好了可以无密码访问远端计算机了。

2. 远端目录挂载到本地
有些时候,我们需要访问远端计算机,例如拷贝文件到远端计算机,或拷贝远端计算机中的文件到本地,当然可以使用scp命令,但是稍显麻烦,尤其是要经常访问的时候,那么我们有什么办法可以将远端目录挂载到本地呢,这里使用sshfs来挂载,当然nfs也是可以的,但这就需要对远端计算机做一些配置,这就需要有管理权限。

首先是安装sshfs,使用命令sudo apt-get install sshfs来安装,手动来挂载一下试试,命令用法如下:

sshfs [user@]host:[dir] mountpoint [options]

卸载的命令在下面:
fusermount -u mountpoint

每次去手动输入上面的命令有点麻烦,能开机自动挂载吗,这是可以的,需要下面几个步骤:
首先修改/etc/fstab,添加如下内容:
# <file system>       <mount point>         <type>  <options>
sshfs#myname@www.myhome.com:/home/myname    /mnt/sshfs/homebox    fuse    comment=sshfs,noauto,users,exec,uid=1000,gid=1000,allow_other,reconnect,transform_symlinks,BatchMode=yes 0 0
当然username、远端目录,挂载点都要设置成你自己的。

然后修改/etc/fuse.conf,如果没有这个文件就自己创建一个,添加下面的内容:
user_allow_other
同时将权限修改成755,例如:sudo chmod 755 /etc/fuse.conf。

可以使用下面的命令测试是否能够自动挂在:
mount /mnt/sshfs/homebox
执行上面的命令之后,再使用mount命令看一下是否能挂载成功。

然后再来设置当网络连接后自动挂载,新增一个文件/etc/network/if-up.d/mountsshfs,在mountsshfs中添加下面的内容:
#!/bin/sh

## http://ubuntuforums.org/showthread.php?t=430312
## The script will attempt to mount any fstab entry with an option
## "...,comment=$SELECTED_STRING,..."
## Use this to select specific sshfs mounts rather than all of them.
SELECTED_STRING="sshfs"

# Not for loopback
[ "$IFACE" != "lo" ] || exit 0

## define a number of useful functions

## returns true if input contains nothing but the digits 0-9, false otherwise
## so realy, more like isa_positive_integer 
isa_number () {
    ! echo $1 | egrep -q '[^0-9]'
    return $?
}

## returns true if the given uid or username is that of the current user
am_i () {
	[ "$1" = "`id -u`" ] || [ "$1" = "`id -un`" ]
}

## takes a username or uid and finds it in /etc/passwd
## echoes the name and returns true on success
## echoes nothing and returns false on failure 
user_from_uid () {
    if isa_number "$1"
    then
		# look for the corresponding name in /etc/passwd
    	local IFS=":"
    	while read name x uid the_rest
    	do
        	if [ "$1" = "$uid" ]
			then 
				echo "$name"
				return 0
			fi
    	done </etc/passwd
    else
    	# look for the username in /etc/passwd
    	if grep -q "^${1}:" /etc/passwd
    	then
    		echo "$1"
    		return 0
    	fi
    fi
    # if nothing was found, return false
   	return 1
}

## Parses a string of comma-separated fstab options and finds out the 
## username/uid assigned within them. 
## echoes the found username/uid and returns true if found
## echoes "root" and returns false if none found
uid_from_fs_opts () {
	local uid=`echo $1 | egrep -o 'uid=[^,]+'`
	if [ -z "$uid" ]; then
		# no uid was specified, so default is root
		echo "root"
		return 1
	else
		# delete the "uid=" at the beginning
		uid_length=`expr length $uid - 3`
		uid=`expr substr $uid 5 $uid_length`
		echo $uid
		return 0
	fi
}

# unmount all shares first
sh "/etc/network/if-down.d/umountsshfs"

while read fs mp type opts dump pass extra
do
    # check validity of line
    if [ -z "$pass" -o -n "$extra" -o "`expr substr ${fs}x 1 1`" = "#" ]; 
    then
        # line is invalid or a comment, so skip it
        continue
    
    # check if the line is a selected line
    elif echo $opts | grep -q "comment=$SELECTED_STRING"; then
    	
    	# get the uid of the mount
        mp_uid=`uid_from_fs_opts $opts`
        
        if am_i "$mp_uid"; then
			# current user owns the mount, so mount it normally
			{ sh -c "mount $mp" && 
				echo "$mp mounted as current user (`id -un`)" || 
				echo "$mp failed to mount as current user (`id -un`)"; 
			} &
		elif am_i root; then
			# running as root, so sudo mount as user
			if isa_number "$mp_uid"; then
				# sudo wants a "#" sign icon front of a numeric uid
				mp_uid="#$mp_uid"
			fi 
			{ sudo -u "$mp_uid" sh -c "mount $mp" && 
				echo "$mp mounted as $mp_uid" || 
				echo "$mp failed to mount as $mp_uid"; 
			} &
		else
			# otherwise, don't try to mount another user's mount point
			echo "Not attempting to mount $mp as other user $mp_uid"
		fi
    fi
    # if not an sshfs line, do nothing
done </etc/fstab

wait

再新增一个文件/etc/network/if-down.d/umountsshfs,内容如下:
#!/bin/bash

# Not for loopback!
[ "$IFACE" != "lo" ] || exit 0

# comment this for testing
exec 1>/dev/null # squelch output for non-interactive

# umount all sshfs mounts
mounted=`grep 'fuse.sshfs\|sshfs#' /etc/mtab | awk '{ print $2 }'`
[ -n "$mounted" ] && { for mount in $mounted; do umount -l $mount; done; }

修改权限:sudo chmod 755 /etc/network/if-up.d/mountsshfs /etc/network/if-down.d/umountsshfs

重启之后,远端目录自动挂载到了本地,也可以使用命令sudo ifconfig eth0 up命令来测试是否能自动挂载,但是执行sudo ifconfig eth0 down时不能卸载,这是个问题。

参考文档: http://ubuntuforums.org/showthread.php?t=430312

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值