参考文献

SSH

1
2
3
4
5
6
7
8
9
10
ssh [options] [user@]host [command]
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]

  • -l 指定登入用户
  • -p 设置端口号
  • -f 后台运行,并推荐加上 -n 参数
  • -n 将标准输入重定向到 /dev/null,防止读取标准输入.如果在后台运行ssh的话(-f选项),就需要这个选项.
  • -N 不执行远程命令,只做端口转发
  • -q 安静模式,忽略一切对话和错误提示
  • -T 禁用伪终端配置
  • -t (tty)为远程系统上的ssh进程分配一个伪tty(终端).如果没有使用这个选项,当你在远程系统上运行某条命令的时候,ssh不会为该进程分配tty(终端).相反,ssh将会把远端进程的标准输入和标准输出附加到ssh会话上去,这通常就是你所希望的(但并非总是如此).这个选项将强制ssh在远端系统上分配tty,这样那些需要tty的程序就能够正常运行.
  • -v verbose)显示与连接和传送有关的调试信息.如果命令运行不太正常的话,这个选项就会非常有用.

SSH生成访问公钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ssh-keygen -o -t rsa -b 2048 -C "example@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa):
Created directory '/c/Users/xxx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/xxxx/.ssh/id_rsa
Your public key has been saved in /c/Users/xxxx/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:zvJW279itA31y6czy1jqJDToXtM/26EDuVoMW3xHLbY example@163.com
The key's randomart image is:
+---[RSA 2048]----+
| |
| .|
| o..|
| .. .oo |
| S.oo.oEo |
| + .*=+ . .|
| . +o+*=+o..|
| +..o==B*+o|
| .o..o++XOo|
+----[SHA256]-----+

1
cat $HOME/.ssh/id_rsa.pub

SSH隧道

本地端口转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ssh  -L [bind_address:]port:host:hostport
-L [bind_address:]port:remote_socket
-L local_socket:host:hostport
-L local_socket:remote_socket

# 将远程服务器的8080端口供本地使用
ssh -NL 8080:localhost:8080 remote_server_host_name

# 将本地端口号可在远程服务器进行访问
ssh -R [bind_address:]port:host:hostport
-R [bind_address:]port:local_socket
-R remote_socket:host:hostport
-R remote_socket:local_socket
-R [bind_address:]port

# 将将本地8080端口映射到服务器的8080端口上
ssh -NR 8080:localhost:8080 remote_server_host_name

Linux Shell远程执行命令(命令行与脚本方式)

前提条件

  • 配置ssh免密码登陆

执行简单命令

1
ssh user@remoteNode "cd /home ; ls"
  • 基本能完成常用的对于远程节点的管理了,几个注意的点
    • 双引号,必须有.如果不加双引号,第二个ls命令在本地执行
    • 分号,两个命令之间用分号隔开

执行复杂命令

1
2
3
4
5
6
7
#!/bin/bash
ssh user@remoteNode > /dev/null 2>&1 << eeooff
cd /home
touch abcdefg.txt
exit
eeooff
echo done!
  • 远程执行的内容在“<< eeooff ” 至“ eeooff ”之间,在远程机器上的操作就位于其中,注意的点:
    • << eeooff,ssh后直到遇到eeooff这样的内容结束,eeooff可以随便修改成其他形式.
    • 重定向目的在于不显示远程的输出了
    • 在结束前,加exit退出远程节点

Ubuntu 18.04离线安装openssh

  • 下载deb

    1
    2
    3
    4
    5
    6
    7
    -rwx------  1 holelin  staff   1.5M  1 23 10:18 libssl-dev_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
    -rwx------ 1 holelin staff 1.2M 1 23 10:18 libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
    -rwx------ 1 holelin staff 596K 1 23 10:16 openssh-client_7.6p1-4ubuntu0.7_amd64.deb
    -rwx------ 1 holelin staff 324K 1 23 10:16 openssh-server_7.6p1-4ubuntu0.7_amd64.deb
    -rwx------ 1 holelin staff 44K 1 23 10:16 openssh-sftp-server_7.6p1-4ubuntu0.7_amd64.deb
    -rwx------ 1 holelin staff 599K 1 23 10:19 openssl_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
    -rwx------ 1 holelin staff 6.8K 1 23 10:19 perl-openssl-defaults_3build1_amd64.deb
  • 先安装依赖

    1
    2
    3
    dpkg -i perl-openssl-defaults_3build1_amd64.deb
    dpkg -i libssl-dev_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
    dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
  • 再安装应用

    1
    dpkg -i openssh*

SSH别名

  1. 打开本地SSH配置文件

    1
    vim ~/.ssh/config
  2. 添加别名及相关设置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Host alias_name
    Hostname target_host
    User username
    Port port_number
    IdentityFile path_to_private_key
    # 示例
    Host dev
    HostName 192.168.11.110
    User holelin
    Port 22
    • alias_name: 替代复杂主机名的简洁别名.
    • target_host: 目标主机的主机名或IP地址.
    • username: 连接远程主机时使用的用户名.
    • port_number(可选): 目标主机的SSH端口号,默认为22.
    • path_to_private_key(可选): 指定用于连接的私钥文件路径.
  3. 测试SSH别名

    1
    2
    3
    ssh  alias_name
    # 示例
    ssh dev

修改服务器SSH配置

修改默认端口

  1. 备份原始sshd配置文件

    1
    cp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak
  2. 修改sshd服务的端口号

    1
    vim /etc/ssh/sshd_config
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #       $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $

    # This is the sshd server system-wide configuration file. See
    # sshd_config(5) for more information.

    # This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

    # The strategy used for options in the default sshd_config shipped with
    # OpenSSH is to specify options with their default value where
    # possible, but leave them commented. Uncommented options override the
    # default value.

    #Port 22
    # 修改对应需要开放的端口
    Port 1022
    #AddressFamily any
    #ListenAddress 0.0.0.0
    #ListenAddress ::

    #HostKey /etc/ssh/ssh_host_rsa_key
    #HostKey /etc/ssh/ssh_host_ecdsa_key
    #HostKey /etc/ssh/ssh_host_ed25519_key
  3. 重启sshd服务

    1
    systemctl restart sshd

禁止root登录

  1. 修改/etc/ssh/sshd_config

    1
    2
    3
    4
    5
    6
    7
    8
    # Authentication:

    #LoginGraceTime 2m
    #PermitRootLogin prohibit-password
    PermitRootLogin no
    #StrictModes yes
    #MaxAuthTries 6
    #MaxSessions 10
  2. 重启sshd服务

    1
    systemctl restart sshd