LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2015, 12:12 PM   #1
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Rep: Reputation: Disabled
Not able to change SSH port


I am using the latest release of CentOS 7 on ovh.com VPS. I am going over Basic CentOS setup website, and it advises to change the SSH port (like many other websites that I've been reading now).

It looks like I am not able to do it thought, so I reversed everything to the original settings.

I was doing something like this:

Code:
vi /etc/ssh/sshd_config
#port 22 ---change to---> port 22000 (example number, 1025-65536 range) 
#PermitRootLogin yes ---> PermitRootLogin no
#UseDNS yes ---> UseDNS no
AllowUsers username (add as last line) 
Save and exit
After I did that I was getting something like this after running the journalctl -xn command

Code:
error: Bind to port 2750 on 0.0.0.0 failed: Permission denied. 
error: Bind to port 2750 on :: failed: Permission denied. 
Fatal: Cannot bind any address	
Sshd.service: main process exited, code=exited, status=255/n/a
Unit sshd.service entered failed state. 
Unit sshd.service cannot be reloaded because it is inactive.
I've seen some posts about it on the Internet, but I was not able to find a precise answer, especially based on CentOS7, that I have.

###

In general, I am at the default settings now, and what is advised in this article is good. When I do the changes sshd stops and it does not work, it is working currently, but it is root login at port 22.

How would I get this done?
 
Old 10-28-2015, 12:18 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What happens if you just uncomment the PermitRootLogin line and change it to no, eg:
Code:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
etc, and then run "service sshd restart"?

Changing the ssh port should be very straight forward, I do it all the time, CentOS 7 included. You can have multiple ports as well, just add new lines, eg:
Code:
Port 22
Port 22000
Port 9082
etc.
 
Old 10-28-2015, 12:21 PM   #3
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
And this ...
 
Old 10-28-2015, 12:35 PM   #4
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
What happens if you just uncomment the PermitRootLogin line and change it to no, eg:
Code:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
etc, and then run "service sshd restart"?
I am logged in now. If I will disable the root login and then restart the sshd.service, will it not kick me out of the server, or something like this?
 
Old 10-28-2015, 12:37 PM   #5
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
You could use reload instead of restart. However, if you are logged in as root it may kick you out after new setting takes effect.
 
Old 10-28-2015, 12:46 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by AdultFoundry View Post
I am logged in now. If I will disable the root login and then restart the sshd.service, will it not kick me out of the server, or something like this?
No it won't, I do it all the time. Though as Emerson said, I'm not sure if the new setting would kick off an existing root ssh session since the new rule explicitly forbids it, though I doubt it. You do have another user, yes? I assume so since you already tried to shut off root SSH login earlier.
 
Old 10-28-2015, 12:49 PM   #7
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Original Poster
Rep: Reputation: Disabled
I will try this now. Thanks for your answers so far. If anybody needs info about xxx websites niche, I can let you know some things over email or Skype.
 
Old 10-28-2015, 01:01 PM   #8
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
What happens if you just uncomment the PermitRootLogin line and change it to no, eg:
Code:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
etc, and then run "service sshd restart"?
Code:
vi /etc/ssh/sshd_config
--> changed to PermitRootLogin no
systemctl reload sshd.service
systemctl status sshd.service
It is running, and it looks like everything is ok.
 
Old 10-28-2015, 01:05 PM   #9
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Emerson View Post
And this ...
Simple question here - For key based authentication (something like an RSA key pair from puttygen) changing SSH port is 100% ok?
 
Old 10-28-2015, 01:18 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by AdultFoundry View Post
Code:
vi /etc/ssh/sshd_config
--> changed to PermitRootLogin no
systemctl reload sshd.service
systemctl status sshd.service
It is running, and it looks like everything is ok.
That's good, now how about adding a second listening port in addition to 22?
Just uncomment the "#Port 22" line, and add a second one below it with your desired port, then restarting sshd again?

When that's done you should be able to connect on either port, though you may need to play with firewall and SELinux rules. Once you verify the new port is working as expected, you can remove the "Port 22" line and restart again to limit your connections to just the new port, assuming you still want to after reading the article Emerson linked to.
 
Old 10-28-2015, 01:22 PM   #11
AdultFoundry
Member
 
Registered: Jun 2015
Posts: 282

Original Poster
Rep: Reputation: Disabled
Thanks for the info. I will leave it as is, and I will come back to it later on.
 
Old 10-28-2015, 06:44 PM   #12
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
You probably need to tell selinux that it's ok for sshd to use an alternate port. The default centos7 has selinux enabled. You can allow an alternate port with this command.

Quote:
semanage port -a -t ssh_port_t -p tcp 2750
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH is still listening on port 22 after change to a new port? boyla Linux - Server 6 04-08-2011 12:26 AM
change ssh port sandeepthug Linux - Server 9 03-11-2010 04:04 PM
ssh change port Longinus Linux - Newbie 14 12-16-2006 03:34 PM
can i change the host's ssh port? guardianx Linux - Software 7 07-11-2005 01:47 PM
Change SSH Port? flamesrock Linux - Software 6 10-02-2004 07:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration