LoginSignup
0
0

More than 1 year has passed since last update.

SSHの始め方

Last updated at Posted at 2021-10-11

Summary

最初に入れるやつら2021に付け足すsshのパートが長いので分離。

ローカルのユーザー名はhoge
リモートのユーザー名はhuga、アドレスはxxx.xxx.xxx.xxx、サーバー名はbar

基本操作

ログイン

ssh huga@xxx.xxx.xxx.xxx

リモートからローカルにコピー

scp huga@xxx.xxx.xxx.xxx:/remote/file.txt /local/directory/

ローカルからリモートにコピー

scp /local/file.txt huga@xxx.xxx.xxx.xxx:/remote/directory/

リモートからローカルに同期

rsync -auv huga@xxx.xxx.xxx.xxx:/remote/directory/ /local/directory/

-uが有るとローカルより新しいもののみ上書きするので、必要の応じて抜く。
/remote/directory//remote/directoryにすると、/local/directory/の下にフォルダを作って同期する。

GUIが使いたい場合

-Yをつけてログインすると、sshがX11を使ったGUIをサポートする。
eogを使って画像を見たりできる。

ssh -Y huga@xxx.xxx.xxx.xxx
eog /remoe/image.png

xauthでエラーが出る場合

ssh -Y bar
> Warning: No xauth data; using fake authentication data for X11 forwarding.

設定を書き足す。

~/.ssh/config
Host *
   XAuthLocation /path/to/xauth

/path/to/xauth/opt/X11/bin/xauthとか。
わからない場合は、whichで確かめる。

which xauth
> /path/to/xauth

パスワードの変更

初期パスワードは、変更すること。

passwd
> Changing password for user huga.
> Changing password for huga.
> (current) UNIX password:
> New password:
> Retype new password:
> passwd: all authentication tokens updated successfully.

Public key authentication

Key pairをローカルで作成

ssh-keygen
> Enter file in which to save the key (/path/to/.ssh/id_rsa): /path/to/.ssh/bar
> Enter passphrase (empty for no passphrase):

/path/to/.ssh は、/Users/hoge/.sshとか。
key pairの名前はbarでもいいけど、どのサーバーに対応するかわかりやすい方が便利。
passphraseは、くれぐれも忘れないように。

疎通確認

ssh -i /path/to/.ssh/bar user@xxx.xxx.xxx.xxx
> Enter passphrase for /path/to/.ssh/bar:

passphraseがログイン時に必要。

alias

~/.ssh/config
# OS名
Host bar
    HostName xxx.xxx.xxx.xxx
    User huga
    Port 22
    IdentityFile /path/to/.ssh/bar
    UseKeychain yes
    AddKeysToAgent yes

この設定をすると、huga@xxx.xxx.xxx.xxxの代わりに、barでリモートを指定できる。
Keychainを使うと、passphraseも省略できる。

References

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0