SSHによるリモート操作
Dockerコンテナ内から、ホストマシン上でシェルスクリプトの実行やアプリの起動をしたい場合、コンテナからSSH接続してコマンドを実行します。
https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
スクリプト実行
$ ssh operator@oracle1 '/scripts/backup.sh'
sudo+コマンドの実行
$ ssh -t user@hostname sudo command
$ ssh -t user@hostname 'sudo command1 arg1 arg2'
公開キー認証へ変更
SSH, or secure shell, is the most common way of administering remote Linux servers. Although the daemon allows password-based authentication, exposing a pas…
コンテナ内でキーペアを作成
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
公開キーをホストマシンへコピー
$ ssh-copy-id username@remote_host
ホストマシンのSSH設定変更
ポート番号も22から変更しましょう(AWSなどのクラウド側でポートを開放する必要があります。アクセスできるクライアント側のIP(範囲)も指定しましょう)。
$ sudo nano /etc/ssh/sshd_config
.....
PermitRootLogin no
.....
PubkeyAuthentication yes
....
PasswordAuthentication no
....
SSHサービス再起動
$ sudo service ssh restart
sshd config OpenBSD manual
https://man.openbsd.org/sshd_config#PermitRootLogin
PermitRootLogin
Specifies whether root can log in using ssh(1). The argument must be yes, prohibit-password, forced-commands-only, or no. The default is prohibit-password.
If this option is set to prohibit-password (or its deprecated alias, without-password), password and keyboard-interactive authentication are disabled for root.
If this option is set to forced-commands-only, root login with public key authentication will be allowed, but only if the command option has been specified (which may be useful for taking remote backups even if root login is normally not allowed). All other authentication methods are disabled for root.
If this option is set to no, root is not allowed to log in.
netstatコマンドにより不正な接続が確立されていないか確認します。
$ netstst -antup
Netcatによるリモート操作
Netcatサーバを起動、クライアントからコマンドを送信します。
Dockerホストマシンをncサーバ、コンテナ(ネットワークはホストと共通)をncクライアントとし、コンテナ内部からホストマシンへbashコマンドを送信します。
ホストマシン
$ nc -l -k localhost 4444 | bash
コンテナ側からコマンド送信
# echo ls -al | nc localhost 4444
コンテナからホストマシンのVLCプレーヤ起動・コントロール
# echo vlc music/AHHE.mp3 | nc localhost 4444
VLC起動後コントロールコマンド送信
# echo next | nc localhost 4444
# echo prev | nc localhost 4444
# echo stop | nc localhost 4444
# echo quit | nc localhost 4444
https://stackoverflow.com/questions/4739196/simple-socket-server-in-bash
ヘルプ
$ nc -h
OpenBSD netcat (Debian patchlevel 1.195-2)
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
[-m minttl] [-O length] [-P proxy_username] [-p source_port]
[-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
[-X proxy_protocol] [-x proxy_address[:port]] [destination] [port]
Command Summary:
-4 Use IPv4
-6 Use IPv6
-b Allow broadcast
-C Send CRLF as line-ending
-D Enable the debug socket option
-d Detach from stdin
-F Pass socket fd
-h This help text
-I length TCP receive buffer length
-i interval Delay interval for lines sent, ports scanned
-k Keep inbound sockets open for multiple connects
-l Listen mode, for inbound connects
-M ttl Outgoing TTL / Hop Limit
-m minttl Minimum incoming TTL / Hop Limit
-N Shutdown the network socket after EOF on stdin
-n Suppress name/port resolutions
-O length TCP send buffer length
-P proxyuser Username for proxy authentication
-p port Specify local port for remote connects
-q secs quit after EOF on stdin and delay of secs
-r Randomize remote ports
-S Enable the TCP MD5 signature option
-s source Local source address
-T keyword TOS value
-t Answer TELNET negotiation
-U Use UNIX domain socket
-u UDP mode
-V rtable Specify alternate routing table
-v Verbose
-W recvlimit Terminate after receiving a number of packets
-w timeout Timeout for connects and final net reads
-X proto Proxy protocol: "4", "5" (SOCKS) or "connect"
-x addr[:port] Specify proxy address and port
-Z DCCP mode
-z Zero-I/O mode [used for scanning]
Port numbers can be individual or ranges: lo-hi [inclusive]