Linuxで一時的にゲストユーザーを作成して作業に必要なコマンドのみ実行できるように制限したい場合があります。
ちょっと調べたらbash shellなら簡単にできるんですね。
rbash – 制限付きのシェル (RESTRICTED SHELL)
https://linuxjm.osdn.jp/html/GNU_bash/man1/rbash.1.html
bashをrbashとして起動するだけでコマンド制限が機能します。
今回はCentOS Linux release 7.2.1511 (Core)で検証してみました。
- rbashの作成
 - /etc/shellsの編集
 - ユーザーの作成
 - .bash_profileのオーナー変更
 - .bash_profileを編集
 
bashにリンクを張ってrbashを作成します。
| 
					 1 2 3 4  | 
						# ln -s /bin/bash /bin/rbash # ll /bin/*bash -rwxr-xr-x. 1 root root 960392 Aug  3 01:00 /bin/bash lrwxrwxrwx. 1 root root      9 Sep 30 17:59 /bin/rbash -> /bin/bash  | 
					
/etc/shellsに/bin/rbashを追加します。
| 
					 1 2 3 4 5 6 7 8 9  | 
						# vi /etc/shells # cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/rbash  | 
					
ユーザーを作成します。
| 
					 1 2 3 4 5 6  | 
						# adduser -m -s /bin/rbash guest # passwd guest Changing password for user guest. New password: Retype new password: passwd: all authentication tokens updated successfully.  | 
					
制限ユーザーが.bash_profileを変更できないようにオーナーをrootに変更します。
| 
					 1 2 3 4 5  | 
						# ll /home/guest/.bash_profile -rw-r--r--. 1 guest guest 193 Aug  3 01:00 /home/guest/.bash_profile # chown root:root /home/guest/.bash_profile # ll /home/guest/.bash_profile -rw-r--r--. 1 root  root  193 Aug  3 01:00 /home/guest/.bash_profile  | 
					
.bash_profileを編集してPATHを利用できるコマンドの保存場所に変更します。
今回は/home/binにPATHを設定しました。
また、historyでのコマンド実行日時の追加とプロンプトを変更しています。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  | 
						# vi /home/guest/.bash_profile # cat /home/guest/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then         . ~/.bashrc fi # User specific environment and startup programs PATH=/home/bin export PATH export HISTTIMEFORMAT="%Y/%m/%d %T " export PS1="[\u@RootLinks]\\$ "  | 
					
ここでguestでログインしてテストしてみます。見事に制限されます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  | 
						[guest@RootLinks]$ cd -rbash: cd: restricted [guest@RootLinks]$ ls -rbash: ls: command not found [guest@RootLinks]$ cat -rbash: cat: command not found [guest@RootLinks]$ vi -rbash: vi: command not found [guest@RootLinks]$ ping 192.168.1.1 -rbash: ping: command not found [guest@RootLinks]$ export PATH=$PATH:/usr/bin -rbash: PATH: readonly variable [guest@RootLinks]$ echo $PATH /home/bin [guest@RootLinks]$ /bin/bash -rbash: /bin/bash: restricted: cannot specify `/' in command names  | 
					
それではguestでsshが実行できるように設定してみます。
| 
					 1 2 3  | 
						# ln -s /bin/ssh /home/bin/ssh # ll /home/bin/ lrwxrwxrwx. 1 root root 8 Sep 30 19:00 ssh -> /bin/ssh  | 
					
guestでsshを実行してみます。無事に他のLinuxサーバにログインできました。
| 
					 1 2 3 4 5 6 7 8 9  | 
						[guest@RootLinks]$ ssh usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]            [-D [bind_address:]port] [-E log_file] [-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]            [-Q cipher | cipher-auth | mac | kex | key]            [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]            [-w local_tun[:remote_tun]] [user@]hostname [command]  | 
					
ちょっとssh中継サーバを検討していたのでなかなか良い感じです。