CentOS新建用户并使能密钥登录的方法 (centos配置ip地址)

教程大全 2025-07-18 23:32:23 浏览

CentOS 默认只有一个 root 用户,但是 root 用户的权限过大,而且不利于多人协作,基于权限管理和安全的原因,我们为系统新建一个用户,并且使能其 SSH 登录,同时禁止 root 用户的登录;

基于 CentOS Linux release 7.6.1810 (Core) 实践;

新建用户

在 CentOS 中,和没有区别

[root@centos_7_6_1810 ~]# ll /usr/sbin/ | grep userlrwxrwxrwx 1 root root 7 Jun 24 10:14 adduser -> useradd-rwxr-xr-x. 1 root root 33104 Aug 3 2017 fuser-rwxr-xr-x. 1 root root 15832 Apr 13 2018 lnewusers-rwxr-xr-x. 1 root root 15752 Apr 13 2018 luseradd-rwxr-xr-x. 1 root root 11576 Apr 13 2018 luserdel-rwxr-xr-x. 1 root root 19896 Apr 13 2018 lusermod-rwxr-xr-x 1 root root 76232 Mar 14 2019 newusers-rwxr-xr-x 1 root root 33072 Mar 14 2019 runuser-rwxr-xr-x. 1 root root 19720 Apr 11 2018 sasldblistusers2-rwxr-x— 1 root root 118224 Mar 14 2019 useradd-rwxr-x— 1 root root 80400 Mar 14 2019 userdel-rwxr-x— 1 root root 113856 Mar 14 2019 usermod-rwsr-xr-x. 1 root root 11376 Oct 31 2018 usernetctl

从上面的命令中可以看出:只不过是命令的一个软连接;

关于软连接,你可以暂时把它理解成 Windows 系统中的快捷方式;

使用命令创建新用户:

[root@centos_7_6_1810 ~]# useradd luizyao[root@centos_7_6_1810 ~]# ls /home/luizyao

在大多数 Linux 的发行版本中,命令并不会在下创建对应的用户目录,如果想要创建,需要在命令中添加 -m (--create-home) 选项;但是,CentOS 会为我们自动创建这个用户目录;

如果我们想要以这个用户名登录系统,必须为其设置一个密码:

[root@centos_7_6_1810 ~]# passwd luizyaoChanging password for user luizyao.New password:Retype new password:passwd: all authentication tokens updated successfully.

然后,我们就可以用这个用户登录系统:

[luizyao@centos_7_6_1810 ~]$ whoamiluizyao

为新用户授权

通常情况下,新用户在自己的用户目录(/home/luizyao/)下拥有完整的权限,其它目录需要他人授权;而我们最常用的就是 root 用户的权限,这时候命令就可以帮助到我们:它允许 信任 的用户以其他用户的身份去执行命令,默认使用的是 root 用户;

新用户并不在信任名单中,所以我们无法借用 root 用户身份去执行命令:

注意:此时,以新用户的身份登录系统的;

[luizyao@centos_7_6_1810 /]$ sudo whoami[sudo] password for luizyao:luizyao is not in the sudoers file. This incident will be reported.

在 CentOS 中,我们有两种方法把新用户添加到 Sudoers 列表中:

注意:此时,以 root 的身份登录系统;

方法一:把新用户添加到用户组中

基于 RedHat 分发版本的系统,如 CentOS 和 Fedora,用户组已经被授予 sudo 的权限;所以,我们可以通过把新用户添加到用户组中,来获取 sudo 的权限:

[root@centos_7_6_1810 ~]# groups luizyaoluizyao : luizyao[root@centos_7_6_1810 ~]# usermod -aG wheel luizyao[root@centos_7_6_1810 ~]# groups luizyaoluizyao : luizyao wheel

我们通过命令把新用户添加到用户组中,可以使用命令查看用户所属的用户组;

这个时候,新用户就可以借助 root 的权限执行命令了:

[luizyao@centos_7_6_1810 root]$ sudo whoami[sudo] password for luizyao:root

注意:

这种方法下,执行命令需要输入新用户的密码,因为这是用户组的默认配置,如下所示:

# /etc/sudoers

106 ## Allows people in group wheel to run all commands107 %wheel ALL=(ALL) ALL108109 ## Same thing without a password110 # %wheel ALL=(ALL) NOPASSWD: ALL

从用户组中删除用户。可以使用如下命令:

[root@centos_7_6_1810 ~]# gpasswd -d luizyao wheelRemoving user luizyao from group wheel[root@centos_7_6_1810 ~]# groups luizyaoluizyao : luizyao

方法二:把新用户添加到列表中

/etc/sudoers 文件中,可以配置用户和用户组的 sudo 权限,这种方式更加灵活一点;并且,有两种方法为新用户配置权限:

1.你可以直接在 /etc/sudoers 文件中配置新用户的权限,但是要注意这个文件的默认权限是只读的,所以你要先添加写入权限,编辑完以后,再恢复为只读;

请使用命令修改 /etc/sudoers 文件,因为它会帮你检查语法错误;

2.你也可以在 /etc/sudoers.d 目录下,为新用户添加一个专门的配置文件(推荐):

bash [root@centos_7_6_1810 ~]# echo “luizyao ALL=(ALL) NOPASSWD:ALL” | tee /etc/sudoers.d/luizyao luizyao ALL=(ALL) NOPASSWD:ALL [root@centos_7_6_1810 ~]# ll /etc/sudoers.d/luizyao -rw-r–r– 1 root root 32 Sep 17 17:51 /etc/sudoers.d/luizyao

上述命令表示:luizyao 可以在任何主机上(第一个ALL)以任何用户的身份(第二个ALL,默认为 root)执行任何命令(第三个ALL),并且不需要密码:

[luizyao@centos_7_6_1810 root]$ sudo whoamiroot

注意:文件的名字可以是任意的,只是通常我们会配置成用户名;

新用户使能 SSH 密钥登录

此时,以新用户的身份登录系统;

创建密钥对:

[luizyao@centos_7_6_1810 ~]$ ssh-keygen -t ecdsa # 椭圆曲线数字签名算法Generating public/private ecdsa key pair.Enter file in which to save the key (/home/luizyao/.ssh/id_ecdsa): # 选择密钥对存放的文件夹Created directory ‘/home/luizyao/.ssh’.Enter passphrase (empty for no passphrase): # 私钥的密码Enter same passphrase again: # 确认私钥密码Your identification has been saved in /home/luizyao/.ssh/id_ecdsa.Your public key has been saved in /home/luizyao/.ssh/id_ecdsa.pub.The key Fingerprint is:SHA256:FljQN9JFxB/C83Mv7N3rFNLCxXICRxaKzKDb+Tzsgwo luizyao@centos_7_6_1810The key’s randomart image is:+—[ECDSA 256]—+| .+.. B==. || .o* = X o || .. .* o B = || o .. . X .|| . oS = =.|| .+ = o|| E .= . +.|| . …. o o|| .. .. .o.|+—-[SHA256]—–+

下载私钥到本地:

基于 Mac OS 的实践;

使用命令下载私钥:

yaomengdeMacBook-Air:~ yaomeng$ scp luizyao@:/home/luizyao/.ssh/id_ecdsa ~/.ssh/

此时,我们仍然需要密码登录:

yaomengdeMacBook-Air:~ yaomeng$ ssh luizyao@Enter passphrase for key “/Users/yaomeng/.ssh/id_ecdsa”: # 输入私钥密码,登录失败[emailprotected]password: # luizyao 的用户密码Last login: Tue Sep 17 22:50:22 2019

SSH 免密登录

重命名公钥为 authorized_keys:

[luizyao@centos_7_6_1810 ~]$ mv ~/.ssh/id_ecdsa.pub ~/.ssh/authorized_keys[luizyao@centos_7_6_1810 ~]$ ll ~/.ssh/total 8-rw-r–r– 1 luizyao luizyao 185 Sep 17 22:58 authorized_keys-rw——- 1 luizyao luizyao 314 Sep 17 22:58 id_ecdsa

注意:

因为我之前并没有 authorized_keys 文件,所以这里我直接重命名;如果之前已经有 authorized_keys 文件,可以使用以下命令,把公钥添加到文件末尾:

cat >> ~/.ssh/authorized_keys < ~/.ssh/id_ecdsa.pub

注意 authorized_keys 文件、~/.ssh/ 目录、或者 用户的 home 目录(/home/luizyao/)对其他用户赋予了写入的权限,那么判断此文件已经不安全,将不会使用这个文件,除非你已经设置 StrictModes 为 no;

你可以通过命令查看帮助文档:

~/.ssh/authorized_keysLists the public keys (DSA, ECDSA, Ed25519, RSA) that can be used for logging in as this user. The format of this file is described above. The con‐tent of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.

If this file, the ~/.ssh directory, or the user’s home directory are writable by other users, then the file could be modified or replaced by unautho‐rized users. In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no”.

此时,我们就可以使用 SSH 免密登录:

yaomengdeMacBook-Air:~ yaomeng$ ssh[emailprotected]Enter passphrase for key “/Users/yaomeng/.ssh/id_ecdsa”: # 私钥密码Last login: Wed Sep 18 00:00:41 2019 from 49.65.108.161

去使能 SSH 密码登录

现在,我们仍然可以使用密码登录,这还是不安全的,现在我们就来禁止使用密码登录系统;

对于 CentOS 系统来说,只需要修改 SSH 配置文件 /etc/ssh/sshd_config 中的 PasswordAuthentication 为;

再重启 SSH 服务:

[luizyao@centos_7_6_1810 ~]$ sudo systemctl restart sshd

我们便禁止了 SSH 的密码登录,只能使用密钥登录;

其它

为了进一步提升系统的安全性,我们还可以做一些事情:

禁止 root 用户使用 SSH 登录

只需要修改 SSH 配置文件 /etc/ssh/sshd_config 中的 PermitRootLogin 为,再重启 SSH 服务;

使用非常规的 SSH 端口

默认的 SSH 端口是22,我们可以修改为不常用的端口:修改 SSH 配置文件 /etc/ssh/sshd_config 中的值(例如:10178),再重启 SSH 服务;

我们还需要修改防火墙中有关 sshd 的配置,CentOS 7 默认使用 firewalld 防火墙,我们对其做如下配置:

将firewalld 关于 ssh 的默认配置文件,复制到系统配置文件夹内:

[luizyao@centos_7_6_1810 ~]$ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

修改配置文件中的端口配置:

CentOS新建用户并使能密钥登录的方法

SSHScription>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.

重载 firewalld 配置:

[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd –reloadsuccess

禁 ping

为防火墙添加如下规则,并重载配置:

[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd –permanent –add-icmp-block=echo-reply[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd –permanent –add-icmp-block=echo-request[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd –reload

总结

以上所述是小编给大家介绍的CentOS新建用户并使能密钥登录的方法,大家如有疑问可以留言,或者联系站长。感谢亲们支持!!!如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

香港服务器首选树叶云,2H2G首月10元开通。树叶云(shuyeidc.com)提供简单好用,价格厚道的香港/美国云 服务器 和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。


linux 如何编译安装mysql?

展开全部不同linux版本,安装不同版本的mysql方法各不相同,下面以CentOS 6.5编译安装MySQL 5.6.16介绍:一.准备工作安装各个依赖包[root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automake* zlib* libxml* ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bison bison-devel unzip 2.新建mysql组和用户[root@WebServer ~]# groupadd mysql[root@WebServer ~]# useradd -g mysql -s /sbin/nologin mysql 3.目录准备[root@WebServer ~]# mkdir -p /webserver/mysql/etc[root@WebServer ~]# mkdir -p /webserver/mysql/data[root@WebServer ~]# mkdir -p /webserver/mysql/tmp[root@WebServer ~]# chown -R /webserver/mysql 4.解压MySQL并进入安装目录[root@WebServer ~]# tar zxvf [root@WebServer ~]# cd mysql-5.6.16 二.编译并安装1.编译MySQL[root@WebServer mysql-5.6.16]# cmake -DCMAKE_INSTALL_PREFIX=/webserver/mysql \> -DSYSCONFDIR=/webserver/mysql/etc \> -DMYSQL_DATADIR=/webserver/mysql/data \> -DMYSQL_TCP_PORT=3306 \> -DMYSQL_UNIX_ADDR=/webserver/mysql/tmp/ \> -DMYSQL_USER=mysql \> -DEXTRA_CHARSETS=all \> -DWITH_READLINE=1 \> -DWITH_SSL=system \> -DWITH_Embedded_SERVER=1 \> -DENABLED_LOCAL_INFILE=1 \> -DWITH_INNOBASE_STORAGE_ENGINE=1 \> -DENABLE_DOWNLOADS=1 2.如果出现以下错误则需要[root@WebServer mysql-5.6.16]# cd source_downloads/[root@WebServer source_downloads]# unzip [root@WebServer source_downloads]# ./configure[root@WebServer source_downloads]# make[root@WebServer source_downloads]# cd .. 然后删除mysql-5.6.16目录下的重新使用cmake进行编译安装2.安装[root@WebServer mysql-5.6.16]# make && make install 编译过程会比较漫长,编译时间取决于机器的性能三配置1.客户端库文件的链接(未执行此步骤的话,在PHP编译时会提示找不到MySQL文件)[root@WebServer mysql-5.6.16]# ln -s /webserver/mysql/lib/.18 /usr/lib64/.18 ps:我使用的是64位CentOS,如果使用的是32位请使用ln -s /webserver/mysql/lib/.18 /usr/lib/.18 启动文件的配置[root@WebServer mysql-5.6.16]# cp support-files/ /etc/init.d/mysqld[root@WebServer mysql-5.6.16]# chmod +x /etc/init.d/mysqld[root@WebServer mysql-5.6.16]# vim /etc/init.d/mysqldbasedir=/webserver/mysqldatadir=/webserver/mysql/data//修改以上这2行的路径即可 3.设置MySQL配置文件[root@WebServer mysql-5.6.16]# cp support-files/ /webserver/mysql/etc/[root@WebServer mysql-5.6.16]# rm -rf /etc/[root@WebServer mysql-5.6.16]# ln -s /webserver/mysql/etc/ /etc/ 4.初始化数据库[root@WebServer mysql-5.6.16]# cd /webserver/mysql[root@WebServer mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/webserver/mysql --datadir=/webserver/mysql/data 5.链接MySQL命令(这一步可以让我们方便很多,可以不用写出命令的路径,也可以写入/etc/profile的PATH中)[root@WebServer ~]# cd /usr/sbin[root@WebServer sbin]# ln -s /webserver/mysql/bin/* . 6.启动MySQL并设置为开机启动[root@WebServer sbin]# service mysqld start ;chkconfig mysqld onStarting MySQL.. SUCCESS! 7.设置MySQL root用户的密码[root@WebServer ~]# mysqladmin -u root password 这里将MySQL的root密码设置为,强烈不建议将数据库的密码设的简单8.删除用户密码为空的用户[root@WebServer ~]# mysql -u root -pEnter password:mysql> use mysql;mysql> select Host,User,Password from user;+-----------+------+-------------------------------------------+| Host| User | Password|+-----------+------+-------------------------------------------+| localhost | root | *6BB4837EBEE4568DDA7DC67ED2CA2AD9 || webserver | root ||| 127.0.0.1 | root ||| ::1| root ||| localhost |||| webserver |||+-----------+------+-------------------------------------------+6 rows in set (0.00 sec)mysql> delete from user where password=;Query OK, 5 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> select Host,User,Password from user;+-----------+------+-------------------------------------------+| Host| User | Password|+-----------+------+-------------------------------------------+| localhost | root | *6BB4837EBEE4568DDA7DC67ED2CA2AD9 |+-----------+------+-------------------------------------------+1 row in set (0.00 sec)mysql> exit;完毕。

网桥和中继器有哪些不同?

中继器。 物理层的设备,分有源和无源两种,有源中继器起信号放大和整形的作用,无源中继器作用纯粹就是连接两个网段(或者两台设备)的物理链路,它不提供信号整形放大的功能,用无源中继设备和用一根网线组网没什么区别。 现在市面上的中继器已经被HUB所淘汰。 网桥。 英文名Bridge,链路层设备。 主要作用是用来分割冲突域,减少网内的广播流量。 通常在早期的一些大网络中,当HUB数量过多,冲突域过大,就会造成广播风暴,这时在网络中间适当的放置网桥就能够分割冲突域,减少广播风暴的可能。 重点要学习网桥的工作原理,即MAC地址表的学习、维护功能。 因为它比HUB和中继器更高级,它能够对数据帧进行处理,网桥能够判断一个数据帧到底是属于网络A还是网络B,然后进行智能的转发或者丢弃。 网桥是将2个不同或相同的网络进行桥接可以实现互访中继器是网络传输中的设备,作用是远距离传输信号的放大中继都是网络的连接桥梁,只是其作用分工不同

Centos系统怎么添加用户和组

用户文件目录/etc/passwd和/etc/shadow组文件目录/etc/group和/etc/gshadow/etc/passwd文件中的用户信息用户名:加密口令:UID:GID:个人信息描述:用户主目录:登陆Shell创建新用户useradd user添加/更改用户口令passwd user删除用户userdel –r user创建新组groupadd group删除组groupdel group添加用户进组创建新的用户并添加到组useradd user -g group创建用户后添加用户进组usermod -g group user-G 附加到组(创建与用户名相同的组) -g 添加到组(不创建与用户名相同的组) -s 指定用户的shell名称 -u uid 指定用户的uid,数字不可为负值 -g uid 指定组的uid,数字不可为负值 -d 指定用户目录 -d 新用户账号的注释说明

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐