Changing the hostname on an Ubuntu system can be done in a few simple steps. Here's how you can do it:
For Ubuntu 16.04 and Later
Open a Terminal: You can use Ctrl + Alt + T to open a terminal.
Change the Hostname:
To change the hostname temporarily (it will revert back after a reboot), use the following command:
User
how to change port ssh to 22022 in ubuntu 24.04
Assistant
Changing the SSH port in Ubuntu 24.04 involves editing the SSH configuration file and then adjusting the firewall settings to allow connections through the new port. Here's a step-by-step guide to change the SSH port to 22022:
User
ubuntu 24.04 how to add user and add it to sudoers, if i have root ssh
Assistant
To add a new user and grant them sudo privileges on Ubuntu 24.04 while logged in as the root user via SSH, you can follow these steps:
sudo hostname new-hostname
To change the hostname permanently, you will need to edit the following files:
Update the /etc/hostname file:
sudo nano /etc/hostname
Replace the existing hostname with your new hostname, then save and exit (in nano, press CTRL + X, Y, then Enter).
Update the /etc/hosts file:
sudo nano /etc/hosts
Look for the line that says:
127.0.1.1 old-hostname
Replace old-hostname with your new hostname. It should look like this after your changes:
127.0.1.1 new-hostname
Again, save and exit the editor.
Apply the changes: To apply the changes, reboot your system:
sudo reboot
For Ubuntu 14.04 and Older
Open a Terminal.
Edit the Hostname file:
Edit the /etc/hostname file:
sudo nano /etc/hostname
Replace the existing hostname with your new hostname, then save and exit.
Edit the Hosts file:
Edit the /etc/hosts file:
sudo nano /etc/hosts
As before, update the 127.0.1.1 line to reflect your new hostname.
Reboot your system to apply the changes:
sudo reboot
Checking the New Hostname
After rebooting, you can check that your hostname has changed successfully by running:
hostname
This command should return your new hostname.
Note
You should choose a hostname that is valid according to DNS standards. Avoid spaces and special characters (except hyphens).
You may need administrative privileges (use sudo) to edit these files.
Step 1: Open the SSH Configuration File
Open a terminal (Ctrl + Alt + T).
Edit the SSH configuration file using a text editor (you can use nano, vim, or any text editor you prefer). Here is how you can do it with nano:
sudo nano /etc/ssh/sshd_config
Step 2: Change the Port Number
In the configuration file, look for the line that says #Port 22. This line might be commented out (with a # in front).
Uncomment this line (remove the # at the beginning) and change 22 to 22022. It should look like this:
Port 22022
Save the file and exit the editor (for nano, press CTRL + X, then Y, followed by Enter).
Step 3: Adjust the Firewall
If you are using ufw (Uncomplicated Firewall), you need to allow the new SSH port:
Allow the new port (22022):
sudo ufw allow 22022/tcp
(Optional) If you want to remove the old port entry (22) for security reasons, run:
sudo ufw delete allow 22/tcp
To check the status of your firewall and the new rules, run:
sudo ufw status
Step 4: Restart the SSH Service
After making changes to the SSH configuration, restart the SSH service to apply the changes:
sudo systemctl restart ssh
Step 5: Test the New SSH Port
Before closing your terminal, test the new port with the following command (open a new terminal window or tab):
ssh -p 22022 username@your-server-ip
Replace username with your actual username and your-server-ip with the IP address or hostname of your server.
Important Notes
Test Before Closing the Session: Make sure to open another terminal and test the new SSH connection before you terminate your current session. If something goes wrong, you may be locked out.
Firewall Considerations: If you're managing a different firewall (e.g., iptables, firewalld), make sure to allow the port there as well.
Remember to Update Any Automation: If you have any scripts or configurations that connect via SSH, remember to update them to use the new port.
Following these steps will successfully change your SSH port to 22022 on Ubuntu 24.04.
Step 1: Create a New User
Create the User: Use the adduser command to create a new user. Replace newusername with the desired username:
adduser newusername
This command will prompt you to set a password and provide some additional information (like full name, room number, etc.). You can press Enter to skip the optional fields.
Step 2: Add the User to the Sudo Group
To grant the new user sudo privileges, you can add them to the sudo group. This allows users in the group to execute commands as the root user.
Add the User to the Sudo Group:
usermod -aG sudo newusername
Step 3: Verify User Addition to Sudo Group
To confirm that the new user has been added to the sudo group, you can use the following command:
getent group sudo
This command will list all users in the sudo group, and you should see newusername included in the list.
Step 4: Testing the New User
Switch to the New User: Now, log in as the new user:
su - newusername
Alternatively, you can log out and log back in as the new user via SSH.
Test Sudo Access: Run a command that requires sudo privileges, such as updating the package lists:
sudo apt update
Enter Password: You will be prompted to enter the password for the new user. After entering the correct password, the command should execute successfully if the user has been added correctly to the sudo group.
Important Notes
Password for Third Party Apps: If you're using SSH keys for authentication and not requiring passwords for root access, this won't affect the sudo password requirement. Each user will still need to provide their password when executing a sudo command.
Use Caution with Root Access: While logged in as root, be cautious with the commands you execute, as you could inadvertently make system-wide changes.
Security Best Practices: It's generally better for security to create user accounts with limited access and grant specific privileges as necessary rather than using the root account frequently.
By following these steps, you can successfully add a new user and grant them the ability to execute commands with sudo privileges on Ubuntu 24.04.