今日のテスト
apacheユーザーをログインできるユーザーへの変更(2023/08/24)
→目次

さくらVPSでRocky Linux9でapacheユーザーをログインできるユーザーへ変更してみました。 apacheをログインできるユーザーにさせれば、/var/www/htmlディレクトリへ/からのファイル転送はしやくなります。 ファイルを転送するたびには、パーミッションや所有者の変更合戦をする必要はなくなります。
事前準備:さくらVPSにRocky Linux9 LAMPをインストールし、必要な設定をしておきます。 

1.apacheユーザーをログインできるユーザーへの変更
◆usermodコマンドでapacheユーザーをログインできるユーザーに変更します。
[root@tk2-000-00000 httpd]# usermod -s /bin/bash apache
[root@tk2-000-00000 httpd]# passwd apache
◆apacheでログインして、ホームディレクトリを確認します。
(※ここで、すぐにrootやrockyからapacheユーザーへ切り替えることができないようです。切り替えるには、一旦rootやrockyをログアウトさせ、再ログインする必要です。)
tk2-000-00000 login: apache
Password: xxxxxxxx
SAKURA internet [Virtual Private Server SERVICE]
[apache@tk2-000-00000 ~]$ 
[apache@tk2-000-00000 ~]$ pwd
/usr/share/httpd
◆sudoできるようにします。
[root@tk2-000-00000 httpd]# usermod -aG wheel apache
◆ホームディレクトリ配下のファイルとサブディレクトリの所有者やグループをapacheに変更します。
[apache@tk2-000-00000 ~]$ sudo chown -R apache:apache /usr/share/httpd
[apache@tk2-000-00000 ~]$ ll
total 16
drwxr-xr-x 3 apache apache 4096 Aug 22 19:49 error
drwxr-xr-x 3 apache apache 4096 Aug 22 19:49 icons
drwxr-xr-x 2 apache apache 4096 Aug 22 19:49 noindex
drwxr-xr-x 2 apache apache 4096 Aug 22 19:49 server-status

2.鍵交換方式SSH接続のための設定
◆クライアント側のWindows11でrsa鍵ファイルを作成します。
apache_id_rsa
apache_id_rsa.pub
◆apacheのホームディレクトリ/usr/share/httpdの直下で、.sshディレクトリを作成します。
.ssh/authorized_keysファイルを作成して、apache_id_rsa.pubの内容をコピー/ペーストしてauthorized_keysファイルに保存します。sshdをrestartします。
[apache@tk2-000-00000 ~]$ mkdir .ssh
[apache@tk2-000-00000 ~]$ vi .ssh/authorized_keys
[apache@tk2-000-00000 ~]$ chmod 700 .ssh
[apache@tk2-000-00000 ~]$ chmod 600 .ssh/authorized_keys
[apache@tk2-000-00000 ~]$ sudo systemctl restart sshd
◆Windows11のコンソール画面でSSH接続確認
C:\Users\user1>ssh -o StrictHostKeyChecking=no -l apache 123.456.789.012 -i C:\Users\user1\Documents\apache_id_rsa
SAKURA internet [Virtual Private Server SERVICE]
Last login: Tue Aug 22 18:59:14 2023
[apache@tk2-000-00000 ~]$
◆Windows11のファイル転送ツールWinSCPから接続確認
WinSCPのバージョンは5.21です。
【転送プロトコル】:scp
【ホスト名】:123.456.789.012
【ポート番号】:22
【設定】→【設定】→【SSH-認証】→【秘密鍵】:apache_id_rsa ファイルを指定
【OK】→【ログイン】→警告ダイアログ【更新】
【ユーザ名】←「apache」
【OK】
◆セキュリティを高めるためにパスワード認証によるssh接続は禁止します。
[root@tk2-000-00000 httpd]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org
[root@tk2-000-00000 httpd]# vi /etc/ssh/sshd_config

#PasswordAuthentication yes
↓
PasswordAuthentication no
[root@tk2-000-00000 httpd]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
以上です。
◆apacheユーザーをログインできないユーザーに戻らせるには
[root@tk2-000-00000 httpd]# usermod -s /sbin/nologin apache