linuxsu命令免密

fiy 其他 63

回复

共3条回复 我来回复
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    Linux中使用ssh命令可以实现免密登录。具体步骤如下:

    1. 在本机上生成公钥和私钥。在终端中输入以下命令:
    “`
    ssh-keygen -t rsa
    “`
    然后会提示你选择保存公钥和私钥的位置,默认是~/.ssh/id_rsa的位置,也可以根据需要选择其他位置。

    2. 将公钥传输到远程服务器。在终端中输入以下命令:
    “`
    ssh-copy-id user@remote_host
    “`
    其中,user是远程服务器的用户名,remote_host是远程服务器的地址。

    如果ssh-copy-id命令不存在,可以使用以下命令手动将公钥传输到远程服务器:
    “`
    cat ~/.ssh/id_rsa.pub | ssh user@remote_host ‘cat >> ~/.ssh/authorized_keys’
    “`

    3. 输入远程服务器的密码,将公钥添加到远程服务器的authorized_keys文件中。

    4. 设置权限。在终端中输入以下命令:
    “`
    ssh user@remote_host “chmod 700 ~/.ssh; chmod 640 ~/.ssh/authorized_keys”
    “`
    这样可以保证只有你有读写权限,其他人没有访问权限。

    5. 现在你可以通过ssh命令免密登录远程服务器了。在终端中输入以下命令:
    “`
    ssh user@remote_host
    “`
    如果一切顺利,你将会直接登录远程服务器,而不需要输入密码。

    6. (可选)如果你使用的是远程服务器的管理员账户,为了安全起见,你可以禁用密码登录,只允许公钥登录。编辑/etc/ssh/sshd_config文件,找到`#PasswordAuthentication yes`这一行,将注释符号#去掉,并将yes改为no,然后重启ssh服务。

    通过以上步骤,你就可以在Linux中使用ssh命令免密登录远程服务器了。记住要保护好你的私钥,不要将私钥泄露给其他人,以确保系统的安全性。

    2年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    To enable passwordless sudo in Linux, you can follow the steps below:

    1. Open a terminal or SSH into your Linux system as a user with sudo access.

    2. Use the visudo command to open the sudoers file in the system editor:

    “`
    sudo visudo
    “`

    Note: visudo is a tool specifically designed for editing the sudoers file, which helps prevent syntax errors that could lock you out of your system.

    3. In the sudoers file, locate the line that grants sudo privileges to the user you want to allow passwordless sudo. It will look something like this:

    “`
    username ALL=(ALL) ALL
    “`

    Replace “username” with the actual username.

    4. Add the NOPASSWD option to the line. The NOPASSWD option allows the user to run sudo commands without being prompted for a password. The modified line should look like this:

    “`
    username ALL=(ALL) NOPASSWD: ALL
    “`

    5. Save and exit the sudoers file. In most cases, you can do this by pressing Ctrl+X, followed by Y, and then Enter.

    6. Test the configuration by running a sudo command without entering a password:

    “`
    sudo
    “`

    Replace “” with any valid sudo command. If the command runs without prompting for a password, then passwordless sudo has been successfully enabled for the specified user.

    Note: Enabling passwordless sudo can be a security risk, as it allows the user to execute privileged commands without authentication. Therefore, it is important to only grant passwordless sudo access to trusted users and to consider the potential risks before proceeding.

    2年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Linux系统中,可以使用ssh免密码登录远程服务器。以下是免密登录的详细步骤:

    Step 1:生成公钥和私钥
    首先,您需要在本地计算机上生成公钥和私钥对。这对密钥将用于验证您的身份并允许您无需密码登录远程服务器。

    打开终端,并使用以下命令生成公钥和私钥对:

    “`
    ssh-keygen -t rsa
    “`

    在生成密钥对的过程中,您可能会被要求输入密钥的存储位置和密码。您可以选择使用默认选项或自定义这些设置。不过,在这一步中,您可以留空直接回车,使用默认选项。

    成功生成密钥对后,您将在主目录下的`.ssh`目录中看到两个文件:`id_rsa`(私钥)和`id_rsa.pub`(公钥)。

    Step 2:将公钥复制到远程服务器
    接下来,您需要将您生成的公钥复制到远程服务器。可以使用`ssh-copy-id`命令来完成这个操作。

    “`
    ssh-copy-id username@remote_host
    “`

    其中,`username`是您在远程服务器上的用户名,`remote_host`是远程服务器的IP地址或域名。

    当您执行此命令时,您将会被要求输入远程服务器的密码。输入密码后,系统会将您的公钥复制到远程服务器的`.ssh/authorized_keys`文件中。

    Step 3:测试免密登录
    现在,您可以尝试使用SSH命令登录远程服务器,而不需要输入密码。

    “`
    ssh username@remote_host
    “`

    如果一切设置正确,您将可以直接登录到远程服务器,而无需输入密码。

    Step 4(可选):禁用密码登录
    为了增加安全性,您可以禁用密码登录,只允许密钥登录。这样,只有持有正确私钥的用户才能登录到远程服务器。

    打开远程服务器上的SSH配置文件:

    “`
    sudo nano /etc/ssh/sshd_config
    “`

    定位到`#PasswordAuthentication yes`行,并将其改为`PasswordAuthentication no`。保存并关闭文件。

    最后,重新加载SSH服务以使更改生效:

    “`
    sudo service ssh restart
    “`

    现在,密码登录将被禁用,只允许使用密钥登录。

    总结
    通过生成公钥和私钥,并将公钥复制到远程服务器的`.ssh/authorized_keys`文件中,您可以实现Linux系统中的免密登录。这不仅方便了您的操作,还提高了系统的安全性。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部