ssh鍵をリモートサーバーに設置し、ローカルから作成した一般ユーザーでログインしたい。
sudo
権限を付与adduser vpsuser
passwd vpsuser
visudo
コマンドでsudoers
というファイルを編集・内容の確認を行う。
su -
visudo
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
%wheel ALL=(ALL) ALL
の行がコメントアウトされていれば外す。
(wheelグループにすべてのコマンドの実行権限を与えるようになる。)
※ # %wheel ALL=(ALL) NOPASSWD: ALL
をコメントアウトすると、
sudoの実行をパスワードなしで許可する設定となる。
usermod -aG wheel vpsuser
id vpsuser
groups
sudo su -
ローカルでssh用の秘密鍵と公開鍵を作成する。
ssh-keygen -t rsa -C "[email protected]"
chmod 600 test_vps_id_rsa
scp ~/.ssh/test_vps_rsa.pub [email protected]:~/
ssh [email protected]
mkdir .ssh
chmod 700 .ssh/
mv test_vps_id_rsa.pub .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
sudo su
vim /etc/ssh/sshd_config
PermitRootLogin no
でrootでアクセスできないように。
PasswordAuthentication no
でパスワードでログイン出来ないように設定する。
PermitRootLogin no
PasswordAuthentication no
設定を反映する。
systemctl restart sshd
Host example.com
HostName example.com
User vpsuser
Port 22
IdentityFile ~/.ssh/test_vps_id_rsa
TCPKeepAlive yes
ssh example.com
\(^o^)/