在Amazon服务器里管理用户

本文介绍如何在 Amazon EC2 实例上设置多个用户账户,并通过 SSH 密钥进行安全登录。步骤包括创建新用户、设置权限、生成密钥对及测试登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载http://utkarshsengar.com/2011/01/manage-multiple-accounts-on-1-amazon-ec2-instance/

Step 0. Login by default user,“ec2-user”:


1


static-9:ec2_thelostlogbook utkarsh$ ssh -i my_key.pem ec2-user@111.111.11.111

Step 1. Create a new user, we will call ournew user “john”:


1

[ec2-user@ip-11-111-111-111 ~]$ sudo adduser john

Set password for “john” by:


1
2

[ec2-user@ip-11-111-111-111 ~]$ sudo su 
[root@ip-11-111-111-111 ec2-user]$ passwd john

Add “john” to sudoer’s list by:


1

[root@ip-11-111-111-111 ec2-user]$ visudo

and add this to the last line:


1


john   ALL = (ALL)    ALL

Alright! We have our new user created, now youneed to generate the key file which will be needed to login, likewe have my_key.pem in Step 0.

Now, exit and go back to ec2-user, out ofroot.

Step 2. Creating the public and privatekeys:


1

[ec2-user@ip-11-111-111-111 ~]$ su john

Enter the password you created for “john” inStep 1.


1
2
3
4
5
6
7

[john@ip-11-111-111-111 ec2-user]$ cd /home/john/ 
[john@ip-11-111-111-111 ~]$ ssh-keygen -b 1024 -f john -t dsa 
[john@ip-11-111-111-111 ~]$ mkdir .ssh 
[john@ip-11-111-111-111 ~]$ chmod 700 .ssh 
[john@ip-11-111-111-111 ~]$ cat john.pub > .ssh/authorized_keys 
[john@ip-11-111-111-111 ~]$ chmod 600 .ssh/authorized_keys 
[john@ip-11-111-111-111 ~]$ sudo chown john:ec2-user .ssh

In the above step, john is the user we createdand ec2-user is the default user group.


1

[john@ip-11-111-111-111 ~]$ sudo chown john:ec2-user .ssh/authorized_keys

Step 3. Now you just need to download the keycalled “john”

I use scp todownload/upload files from EC2, here is how you can doit:

You will still need to copy the file usingec2-user, since you only have the key for that user name. So, youwill need to move the key to ec2-user folder and chmod it to777.


1
2

[john@ip-11-111-111-111 ~]$ sudo cp john /home/ec2-user/ 
[john@ip-11-111-111-111 ~]$ sudo chmod 777 /home/ec2-user/john

Now come to local machine’s terminal, whereyou have my_key.pem file and do this:


1


static-9:ec2_thelostlogbook utkarsh$ scp -i my_key.pem ec2-user@111.111.11.111:/home/ec2-user/john john

The above command will copy the key “john” tothe present working directory on your local machine. Once you havecopied the key to your local machine, you should delete“/home/ec2-user/john”, since it’s a private key.

Now, one your local machine chmod john to600.


1


static-9:ec2_thelostlogbook utkarsh$ chmod 600 john

Step 4. Time totest your key:(111.111.11.111为服务器的IP


1


static-9:ec2_thelostlogbook utkarsh$ ssh -i john john@111.111.11.111

So, in this manner, you can setup multipleusers to use one EC2 instance!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值