很多同学刚学Linux,在命令行连接远程主机时,或多或少会有一些问题,以下是我总结的几点实用的方式。
1、ssh连接远程主机方式
ssh [-p port] user@hostname [command]
以上格式可以在终端中输入ssh -v得到更多选项信息,这里稍微解释一下:
-p port: 指定远程端口,一般默认是22号端口,除非你的远程主机改变了连接端口,此时需要用该选项指定
user : 指定用户,一般情况下我们默认没有写,因为我们习惯了用root管理员方式登录,连接远程主机时,也是root,那么此时可以不写,但,当远程主机用户权限控制的比较严,那么就得用管理员指定的账号登录了。以下是示例:
#连接远程主机192.168.0.8,用户为test,连接端口为2222 ssh -p 2222 test@192.168.0.8 #如果你当前登录的主机用户就是test的话,上面命令中test@可以不用写
再来一个,如果你只是想执行远程执行一条命令,不想登录太麻烦,可以这样操作:
登录远程主机并执行一个命令:python /root/getmac.py ssh -p 2222 test@192.168.0.8 "python /root/getmac.py"
2、sftp传输文件
以下是输入命令sftp -h的输出结果:
# sftp -h usage: sftp [-1246aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher] [-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit] [-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 -oPort=2222 test@192.168.0.8
那么我怎么知道要这样输入呢,我们来man sftp一下:
-o ssh_option
Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for specify-
ing options for which there is no separate sftp command-line flag. For example, to specify an
alternate port use: sftp -oPort=24. For full details of the options listed below, and their possi-
ble values, see ssh_config(5).-P port
Specifies the port to connect to on the remote host.
so,我们也可以使用如下命令进行连接:
sftp -P 2222 test@192.168.0.8
全剧终!!
- 微信扫码赞助
- 支付宝赞助